Wordpress how differentiate thumbnails post from even to odd? - wordpress

I've done a wordpress script that displays a grid of thumbnails posts making this code:
<div class="col-sm-12 col-md-12" style="background-color: #ccc; padding-top: 30px;">
<article <?php post_class( 'article' ); ?> id="post-<?php the_ID(); ?>">
<?php
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => 17, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
'order_by' => 'title',
'order' => 'asc',
) );
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
?>
<div class="col-md-6 thumbnail-frontpage clearfix">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<a href = "<?php the_permalink(); ?>" > <img src = "<?php echo $image[0]; ?>" onmouseover="this.src='<?php the_field('rollover_image'); ?> ' " onmouseout="this.src='<?php echo $image[0]; ?>'" /> <?php the_title(); ?> </a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<!-- <p class=""><?php the_content( ); ?></p> -->
</article>
</div>
Now I'd like to differentiate the color of the thumbnails border separating odds from evens.
How can i do it using while conditional code?

If "border color differentiation" is your only purpose here, I suggest you use CSS, not PHP.
.thumbnail-frontpage:nth-child( even ) {
border: 1px solid red;
}
.thumbnail-frontpage:nth-child( odd ) {
border: 1px solid blue;
}
You can look it up here: http://www.w3schools.com/cssref/sel_nth-child.asp

you want like this
<?php
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => 17, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
'order_by' => 'title',
'order' => 'asc',
) );
$i=0;
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
if($i%2==0){
$thumbnail = 'thumbnail';
}else{
$thumbnail = 'single-post-thumbnail';
}
?>
<div class="col-md-6 thumbnail-frontpage clearfix">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $thumbnail ); ?>
<a href = "<?php the_permalink(); ?>" > <img src = "<?php echo $image[0]; ?>" onmouseover="this.src='<?php the_field('rollover_image'); ?> ' " onmouseout="this.src='<?php echo $image[0]; ?>'" /> <?php the_title(); ?> </a>
</div>
<?php
$i++;
endwhile;
endif;
wp_reset_postdata();
?>
<!-- <p class=""><?php the_content( ); ?></p> -->
</article>
</div>

Related

Posts list with their taxonomy

I would like to display list of post for a wordpress post type but I don't know how to show taxonomy term linked to the post.
My code :
<ul class="list-group">
<?php
$args = array(
'post_type' => 'faq',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
)
;$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="list-group-item">
<span class="label label-default">xxxxx taxonomy xxxxxx</span>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Could you help me please.
<?php query_posts(array('post_type'=>'faq', 'posts_per_page'=>5, 'order' => 'DESC', )); ?>
<?php if(have_posts()) { while(have_posts()) { the_post();?>
<div class="col-md-6 col-sm-6">
<?php if ( has_post_thumbnail() ) {?>
<div class="blog_image">
<?php the_post_thumbnail( 'full' ); ?>
</div>
<?php } else{ ?>
<?php }?>
<div class="entry-content">
<h3 class="blog-title">
<?php the_title(); ?>
</h3>
<span class="post-date"><?php echo get_the_date('d. M Y'); ?></span>
<p><?php echo wp_trim_words( get_the_content(), 50, ' (...)' );?></p>
</div>
</div>
<?php }
}
wp_reset_query();
?>
for image display <?php the_post_thumbnail( 'full' ); ?>
for title display <?php the_title_attribute(); ?> for title display
for content display <?php the_content();?>
change HTML as you want
<?php
/* FIRST
* Note: This function only returns results from the default “category” taxonomy. For custom taxonomies use get_the_terms().
*/
$categories = get_the_terms( $post->ID, 'taxonomy' );
// now you can view your category in array:
// using var_dump( $categories );
// or you can take all with foreach:
foreach( $categories as $category ) {
echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
}

woocommerce products by category repeated first 10 products on each page so how to display all product?

I created custom code for woocommerce products show as per selected category, first 10 product display as per the category but on the second page of pagination same 10 products are displayed, also at all page the same product are display, when products are order by rand then result is correct but i want result by title so please give me the solution for that my code is as below.
<ul class="products">
<?php $cat_slug=get_queried_object()->slug; ?>
<?php
$args = array( 'post_type' => 'product', 'product_cat' => $cat_slug, 'orderby' => 'title' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product_cust ">
<div class="woo_100">
<div id="woo_c">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<div class="woo_thumb">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
</div>
</a>
</div>
<div id="woo_d">
<div class="woo_desc">
<div class="nil_desc">
<a href="<?php echo get_permalink( $loop1->post->ID ) ?>" title="<?php echo esc_attr($loop1->post->post_title ? $loop1->post->post_title : $loop1->post->ID); ?>">
<h3><?php the_title(); ?></h3>
</a>
<span class="price"><?php echo $product->get_price_html(); ?></span><br>
</div>
<div class="nil_cart"><?php woocommerce_template_loop_add_to_cart( $loop1->post, $product ); ?>
</div>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
You have not passed any variable to paginate it.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'product_cat' => $cat_slug, 'orderby' => 'title', 'paged'=>$paged);
Also after your loop use nav.php to get pagination control links.

How to get permalink for every post in this shortcode function

I want to get the permalink of the images that shown on the home page, so that when someone clicks on the image redirects to its full post.
add_shortcode( 'zee_recent_works', function( $atts, $content= null ){
ob_start();
$atts = shortcode_atts(array(
'slides' => 2,
'title' => '',
'description' => ''
), $atts);
extract($atts);
$item_per_slide = 4;
$args = array(
'numberposts' => $item_per_slide*$slides,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'zee_portfolio'
);
$portfolios = get_posts( $args );
$i = 1;
$j = 1;
$count = count($portfolios);
if ($count>0) {
?>
<div class="col-md-12">
<div id="scroller" class="carousel slide">
<div class="carousel-inner">
<?php
foreach( $portfolios as $key=>$value ) {
if( (($key+1)%($item_per_slide)==0) || $j== $count) {
$lastContainer= true;
} else {
$lastContainer= false;
}
if($i==1){
?>
<div class="item <?php echo ($key==0)? 'active': ''; ?>">
<div class="row">
<?php
}
?>
<div class="col-md-<?php echo round(12/$item_per_slide) ?>">
<div class="portfolio-item">
<div class="item-inner">
<?php
echo get_the_post_thumbnail( $value->ID, array(400,400), array(
'class' => "img-responsive",
'alt' => trim(strip_tags( $value->post_title )),
'title' => trim(strip_tags( $value->post_title ))
));
?>
<h5>
<?php echo $value->post_title; ?>
</h5>
<div class="overlay">
<?php
$full_img = wp_get_attachment_image_src( get_post_thumbnail_id($value->ID), 'full');
$img_src= $full_img[0];
?>
<a class="" title="<?php echo $value->post_title; ?>" href="<?php the_permalink(); ?>" rel="prettyPhoto"></a>
</div>
</div><!--.item-inner-->
</div><!--.portfolio-item-->
</div>
<?php
if(($i == $item_per_slide) || $lastContainer) {
?>
</div><!--/.row-->
</div><!--/.col-xs-->
<?php
$i=0;
}
$i++;
$j++;
}
?>
</div>
</div>
</div><!--/.col-md-12-->
<?php
}
return ob_get_clean();
});
and the link is there:
<a class="" title="<?php echo $value->post_title; ?>" href="<?php the_permalink(); ?>" rel="prettyPhoto"></a>
where the permalink should work but its not working.
Instead of the_permalink() you can try get_permalink($value->ID)
final line should be
<a class="" title="<?php echo $value->post_title; ?>" href="<?php echo get_permalink($value->ID); ?>" rel="prettyPhoto" ></a>
Hope it helps :)
Thanks.

Duplicated posts while using Infinite Scroll + random order

I'm using wp_query with infinite scroll to display posts in a specific cpt archive page. When i set the 'orderby' to 'date', everything works ok, but when i change it to 'rand' the query returns the correct number of posts, but some of them are duplicated.
This is the code i'm using:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loopb = new WP_Query( array( 'post_type' => 'my_post_type', 'posts_per_page' => 10, 'paged' => $paged, 'order' => 'rand' ) );
$value = get_field('thumbnail_sizing');
?>
<?php while ( $loopb->have_posts() ) : $loopb->the_post();
$thumb_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'small-size' );
$full_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full-size' );
$thumb_img_ratio = 70;
if (isset($thumb_img[1]) && isset($thumb_img[2]) && $thumb_img[1] > 0) {
$thumb_img_ratio = $thumb_img[2] * 100/$thumb_img[1];
} ?>
<div class="mosaic">
<a href="#" class="grayscale">
<div class="mosaic__image" style="padding-top: <?php echo $thumb_img_ratio;?>%;">
<img src="<?php echo $thumb_img[0]; ?>" data-src="<?php echo $full_img[0]; ?>" alt="<?php the_title();?>">
</div>
<div class="meta">
<div class="flex">
<div class="flex_item">
<h2 class="meta_title"><?php the_field('name1');?> <span class="divider">&</span> <?php the_field('name2');?></h2>
<hr class="separator">
<span class="cat">view image</span>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
Carry a random seed with you i don't know if WP_Query supports it but it would be RANDOM(SEED) (e.g. RANDOM(1234)) in straightmysql

Wordpress page won't paginate

Okay I am ripping my hair out over this and I know it is super simple and is staring me in the face but I just can't see it. In essence what is happening is that my pagination isn't working. I use the plugin wp_pagenavi (which is not the issue already checked) It gives me a list of links of each page in the pagination. That all works fine, in fact all the code does what it is supposed to do. The problem is when I click on the pagination link such as "2" or "3" it attempts to go to the corresponding pagination ie www.yousitehere.com/somepage/page/2 but instead the site just reloads the current page without the /page/2.
I think somewhere the page either doesn't realize it needs to paginate and therefore ignores all instances of /page or something is resetting the pagination before the page loads. (its not the reset_query).
This is a Wordpress site. Unfortunately I am unable to show the site link which makes it harder for you bu hopefully someone has a suggestion.
here is my code
<div id="content" class="clearfix">
<div id="main" class="col960 left first clearfix" role="cont_detail">
<?php if (have_posts()) : while (have_posts()) : the_post();
$id = get_the_ID();
$array = metadata($id);
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="cont_detail" >
<header>
<div id="header_bg_top"></div>
<div id="header_bg">
<?php if($array['custom_facebook'] !="" && $array['custom_twitter'] !="" && $array['custom_linkedin'] != ""&& $array['custom_website'] != "" && $array['custom_blog'] != "" && $array['custom_email'] != ""){?>
<div class="social_link_container">
<div class="width">
<?php if(isset($array['custom_facebook']) && $array['custom_facebook'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-facebook.png">
<?php }
if(isset($array['custom_twitter']) && $array['custom_twitter'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-twitter.png">
<?php }
if(isset($array['custom_linkedin']) && $array['custom_linkedin'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-in.png">
<?php }
if(isset($array['custom_website']) && $array['custom_website'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-web.png">
<?php }
if(isset($array['custom_blog']) && $array['custom_blog'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-blog.png">
<?php }
if(isset($array['custom_email']) && $array['custom_email'] !=""){?>
<img src="<?php echo get_template_directory_uri(); ?>/images/author-email.png"><?php }?>
</div>
</div>
<?php } ?>
<h1 class="cont_detail_title" itemprop="headline"><?php the_title(); ?></h1>
<?php if(isset($array['custom_description']) && $array['custom_description'] !="")
{
?>
<span class="title_description"><?php echo $array['custom_description']; ?></span>
<?php
}
?>
</div><!-- header-bg -->
</header> <!-- end article header -->
<section>
<div class="content-left">
<?php if(isset($array['featured_thumb']) && $array['featured_thumb'] !="")
{
?>
<div class="cont_detail_image">
<?php print_thumbnail($array['featured_thumb'], "medium");?>
</div>
<?php
}
?>
</div>
<div class="content-right">
<?php the_content(); ?>
</div>
</section> <!-- end article section -->
</article> <!-- end article -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1>Not Found</h1>
</header>
<section class="post_content">
<p>Sorry, but the requested resource was not found on this site.</p>
</section>
<footer>
</footer>
</article>
<?php endif;
wp_reset_query();
//Sort queries, most viewed, alphabetical, date
echo '<section><div id="content_bg" class="group">';
$row_num = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if( $_GET['sort'] == 'date' ) {
$sortOrder = getSortOrder( $_GET['dsort_order'] );
$meta_queries[] = array(
'key' => 'custom_authorid',
'value' => $post->ID,
'compare' => 'LIKE',
);
query_posts(array(
'post__not_in' => array($post->ID),
'post_type' => 'post',
'meta_query' => $meta_queries,
'order'=>$sortOrder,
'orderby'=> 'date',
'posts_per_page' => 9,
'paged' => $paged
));
}
elseif( $_GET['sort'] == 'alpha' ) {
$sortOrder = getSortOrder( $_GET['asort_order'] );
$meta_queries[] = array(
'key' => 'custom_authorid',
'value' => $post->ID,
'compare' => 'LIKE',
);
query_posts(array(
'post__not_in' => array($post->ID),
'post_type' => 'post',
'meta_query' => $meta_queries,
'order'=>$sortOrder,
'orderby'=> 'title',
// adding posts_per_page, set to 9 might be different then global preference
'posts_per_page' => 9,
'paged' => $paged
));
}
elseif( $_GET['sort'] == 'mviewed' ) {
}
else{
$meta_queries[] = array(
'key' => 'custom_authorid',
'value' => $post->ID,
'compare' => 'LIKE',
);
query_posts(array(
'post__not_in' => array($post->ID),
'post_type' => 'post',
'posts_per_page' => 9,
'meta_query' => $meta_queries,
'paged' => $paged
));
}
?>
<div class="paginate_nav">
<?php wp_pagenavi(); // use the page navi function ?>
<div class="sort_nav">
<?php contrPaginationLinks( $sortOrder ) ?>
</div><!--sort_nav-->
</div><!--paginate_nav-->
<?php
if( $_GET['sort'] == 'mviewed' ) {
}
else{
if (have_posts()) : while (have_posts()) : the_post();
$item = metadata($post->ID);
$containsVideos = false;
foreach ($item['cat'] as $value) {
if (strpos($value->cat_name,'Videos') !== false) {
$containsVideos = true;
}
}
?>
<div class="story-container" >
<div class="story_left">
<div class="story_image">
<input type="hidden" name="vidID" value="<?php echo $item['media_link']; ?>" />
<div class="about_latest_image <?php echo $item['media_link']; ?>">
<a href="<?php echo $item['link']; ?>">
<?php
print_thumbnail($item['featured_thumb'],"thumbnail", $item['youtube_link'], $item['vimeo_link'], $item['media_link']);
?>
<?php if($containsVideos == true){echo '<span class="videoIndicator"></span>';} ?>
</a>
</div>
</div>
<?php if($_GET['sort'] == 'date'){ ?>
<div class="story_view">
<?php echo $item['date']; ?>
</div>
<?php } ?>
</div>
<div class="story_title">
<a href="<?php echo $item['link']; ?>" title="<?php echo $item['title']; ?>">
<span class="story_title"><?php echo $item['title']; ?></span>
</a>
</div>
<?php if(isset($item['custom_description']) && $item['custom_description'] !="") {
?>
<div class="story_description"><?php echo $item['custom_description']; ?></div>
<?php
}
?>
<div class="story_content"></div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h1><?php _e("No Posts Yet", "hooplahatheme"); ?></h1>
<p><?php _e("Sorry, What you were looking for is not here.", "hooplahatheme"); ?></p>
<?php endif;
} // end display else default
echo '</div></section>';
//
// reset query after display is done
//
wp_reset_query();
?>
<footer>
</footer> <!-- end article footer -->
</div> <!-- end #main -->
Update
It turns out that there is a 302 redirect occurring which is what I am trying to find out the next step.
Christopher's answer probably works for most people.
But..
If you want to stop the redirect from also affecting /pagename/page/1, this will help:
add_filter('redirect_canonical', 'my_redirect_canonical', 10, 2);
function my_redirect_canonical($redirect_url, $requested_url) {
$do_redirect = true;
if(preg_match('/page/',$requested_url)){
$do_redirect = false;
}
return $do_redirect;
}
Found here.
So for anyone with a similar question the answer is simple. You can't paginate single post pages by default in wordpress. It will just redirect back to the root URL of the page. What you need to do to stop this is add this to your themes functions.php
add_filter('redirect_canonical','pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url) {
if( is_singular() && in_category('PLACE CAT ID HERE') ) {
$redirect_url = false;
}
return $redirect_url;
}
for more info on this you can look here http://wordpress.org/support/topic/home-pagination-page2-redirect-to-a-post

Resources