WordPress.org

Ready to get started?Download WordPress

Forums

sticky post for archive pages (1 post)

  1. bellilint
    Member
    Posted 7 years ago #

    My need was to modify the code so that the archived pages for a specific category always display one particular post at the top. Three different categories, three different archives, three different sticky posts.

    I have modified the code so that this mostly works. It breaks once the maximum number of posts per page is exceeded. Once the archive is split amongst more than one page, the sticky post will be bumped to the back page, although it will remain at the top of that page.

    I need the sticky post to stay at the top of the first page of archived posts.

    Here is the modified code in archive.php:

    <?php get_header(); ?>
    
    <!-- Sticky post modification -->
    	<?php $do_not_duplicate = get_sticky_post_ID(get_query_var('cat')); ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post();
    			if( $post->ID == $do_not_duplicate ){ ?>
    
    	<!-- start a post -->
    	<div class="posthead"><hr width="100%" /><br /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></div>
    	<div class="thepostbox clearfix">
    		<div class="thepost"><?php the_content('Read the rest of this entry &raquo;'); ?></div>
    	</div>
    	<!--end a post -->
    
    		<?php }
    			else
    			continue;
    			endwhile; ?>
    
    	<?php endif; ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post();
    			if( $post->ID == $do_not_duplicate )
    			continue;
    			else { ?>
    	<!-- start a post -->
    
    	<div class="posthead"><hr width="100%" /><br /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></div>
    	<div class="thepostbox clearfix">
    		<div class="thepost"><?php the_content('Read the rest of this entry &raquo;'); ?></div>
    	</div>
    	<!--end a post -->
    		<?php }
    			endwhile; ?>
    
    <!--  End Sticky post modification -->
    
    		<div class="next_prev">
    
                    <table width="570">
                      <tr>
                        <td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php next_posts_link('&laquo; Previous Entries') ?></td>
                        <td align="right"><?php previous_posts_link('Next Entries &raquo;') ?></td>
                      </tr>
                    </table>
    
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    
    	<?php endif; ?>
    
    <?php get_footer(); ?>

    NOTE: the function 'get_sticky_post_ID' returns the ID number of the post that is sticky in the category being displayed.

    Any thoughts on what I've done wrong?

    Thanks,
    Andrea

Topic Closed

This topic has been closed to new replies.

About this Topic