WordPress.org

Ready to get started?Download WordPress

Forums

Display Sticky posts only from specific category (5 posts)

  1. Alisdairb
    Member
    Posted 1 year ago #

    I have been working on a personal project to learn more advanced techniques in WordPress and one of the features i require is to display the latest three sticky posts from a specific category. I have some code however its only displaying the latest three posts that not the latest three sticky posts from a specific category.

    <?php $sticky=get_option('sticky_posts');
    $query_args=array(
    'post__in' => $sticky,
    'category__in'=>array($category)
     );
    $the_query = new WP_Query($query_args); ?>
    <?php $count = 0; ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
    <?php $count++; ?>  
    
    	<?php if ($count == 1) : ?>
    		<div class="featurethumb"><?php the_post_thumbnail(array(306,306), array ('class' => 'featurethumb')); ?>
    	<div class="featuretitle-bg"><div class="featuretitle"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></div>
    	<div class="featured-desc"><?php the_excerpt(__('(more…)')); ?></div></div>
    	</div>
    	<?php elseif ($count == 2) : ?>
    		<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft1')); ?></div>
    <div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
    <?php the_excerpt(__('(more…)')); ?>
    	<?php else : ?>
    		<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft2')); ?></div>
    <div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
    <?php the_excerpt(__('(more…)')); ?>
    	<?php endif; ?>
    
    <?php endwhile; ?>
  2. Where is your $category variable being defined? Is it a single category being returned? Or an array?

  3. Alisdairb
    Member
    Posted 1 year ago #

    Hi Josh, sorry I included the wrong code, that was from a previous attempt. I have added the code which i have been using to basically get the latest posts from a specific category however im not experience enough to realize how i can make it so that code only picks up the sticky posts from that category.

    <?php $the_query = new WP_Query( 'category_name=things-to-do&showposts=3' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft1')); ?></div>
    <div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
    <?php the_excerpt(__('(more…)')); ?>
    <?php endwhile;?>
  4. Okay.. try this:

    <?php 
    
    $args = array(
    			'category_name' => 'things-to-do',
    			'post__in' => get_option( 'sticky_posts' ),
    			'showposts' => '3'
    		);
    
    $the_query = new WP_Query( $args );
    
    	while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
            <div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft1')); ?></div>
            <div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
            <?php the_excerpt(__('(more…)'));
    	endwhile;
    
    ?>
  5. coyotecosmic
    Member
    Posted 6 months ago #

    Hi Josh!!! ur amazing!!! thnx for the code, some changes in my divs and works very nice.

    best regards!

Topic Closed

This topic has been closed to new replies.

About this Topic