My first idea was to query sticky post. But i learned, sticky post doesn’t work with category.
Actually, your first thought was correct. Whilst sticky posts are not treated differently by default on any page other than the main posts page, you can use the sticky option to selectively pull posts onto pages elsewhere using a custom query such as WP_Query().
Thanks esmi
But that idea didn’t work. I don’t get sticky post under category than latest post.
Used this –
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
Where’s the rest of the Loop to display the returned post?
<?php
if( is_category() ) :
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query('$args&showposts=2&cat='.get_query_var('cat'));
else : $the_query = new WP_Query('showposts=2&orderby=post_date&order=desc');
endif;
?>
<?php
if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post()
?>
*updated
I still can’t see any code to actually display the returned post. You need something like:
<?php
if( is_category() ) :
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
else :
$args = array(
'posts_per_page' => 2,
'orderby' => 'post_date',
'order' => 'desc'
);
endif;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
oh,yes. I used this.
<?php
if( is_category() ) :
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query('$args&showposts=2&cat='.get_query_var('cat'));
else : $the_query = new WP_Query('showposts=1&orderby=post_date&order=desc');
endif;
?>
<?php
if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post()
?>
<div class="film singleBox first tallBox" style="width:100%">
<div class="filmInside">
<a href="<?php the_permalink(); ?>">
<?php
if( has_post_thumbnail() )
{
the_post_thumbnail('small-thumb', array( 'class' => 'layout-2-thumb' ) );
echo '<div class="struggleTitle midGrey">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
echo '<div class="struggleDescription whiteColor">' .'<p>'. get_post(get_post_thumbnail_id())->post_content . '</p>'. '</div>';
}
else{
echo '<img src="'.get_template_directory_uri().'/images/no-image.png" class="layout-2-thumb">';
}
?>
</a>
</div>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Try comparing your code to mine. Can you see that you are not actually using the &args that you set up in your custom query?
Thanks. I modified my code according to your suggestion.
<?php
if( is_category() ) :
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
else :
$args = array(
'posts_per_page' => 2,
'orderby' => 'post_date',
'order' => 'desc'
);
endif;
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="film singleBox first tallBox" style="width:100%">
<div class="filmInside">
<a href="<?php the_permalink(); ?>">
<?php
if( has_post_thumbnail() )
{
the_post_thumbnail('small-thumb', array( 'class' => 'layout-2-thumb' ) );
echo '<div class="struggleTitle midGrey">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
echo '<div class="struggleDescription whiteColor">' .'<p>'. get_post(get_post_thumbnail_id())->post_content . '</p>'. '</div>';
}
else{
echo '<img src="'.get_template_directory_uri().'/images/no-image.png" class="layout-2-thumb">';
}
?>
</a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
With this i get only latest one sticky post. Same sticky post in every category.
I wish to get sticky post at top according to category and another post under that sticky video. Just like as my image.