• Resolved Rodrigo

    (@rghedin)


    Hi,

    I have a custom home page where usually there is a sticky post followed by regular posts, and between them, a static content to indicate where regular posts start. It’s something like this:

    
    STICKY POST
    
    <h3>Regular posts</h3>
    
    Regular posts
    

    To achieve this structure, I used a conditional is_sticky() in the loop and wrote the <h3>Regular… inside it.

    It works like a charm when there is a sticky post, but it’s not always the case.

    Is there a way to have a h3 title/any content before regular posts in the loop, but below the sticky post when it exists?

    Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Since a user can have more than one sticky post, you would need to be a little more complicated than a single check of is_sticky().
    I would set a variable to count the sticky posts, and then use that information when you hit ! is_sticky() for whether you need a title or not (and reset it once it is output).

    Thread Starter Rodrigo

    (@rghedin)

    Thanks for your reply, @joyously!

    You’re right. In my case, however, I never keep two or more sticky posts active, so this scenario is a non-issue. My problem is when there is no sticky posts.

    What I am suggesting would handle both cases.

    Thread Starter Rodrigo

    (@rghedin)

    Oh, I didn’t notice. Do you mind sharing a code example of what you’re suggesting? (I have some HTML and vaguely WordPress theming skills.)

    Something like this:

    $sticky_count = 0;
    while ( have_posts() ) : the_post(); ?>
    	if ( is_sticky() ) {
    		$sticky_count += 1;
    	}
    	else if ( $sticky_count ) {
    		echo 'static content';
    		$sticky_count = 0;
    	} ?>
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php get_template_part( 'content' ); ?>
    	</article>
    <?php
    endwhile; // End of the loop.
    Thread Starter Rodrigo

    (@rghedin)

    It’s working great when there are sticky posts, @joyously — no matter if only one, two or more. Thanks!

    However, I don’t know why the static content doesn’t show up when there is no sticky post. Any idea?

    Well, that was the point of it. There is no need to label them if there aren’t two kinds.

    You can adjust the if statement to put it out regardless, but you’ll need to make sure it’s only once, so you need another variable or another (negative) value for the count variable.

    Thread Starter Rodrigo

    (@rghedin)

    In my site, non-sticky posts are asides, while sticky posts are special weekly issues/posts — they are sticky for a week, then they are “moved” to a separate archive page. Even when there is no sticky post, I’d like to tell readers about what they see — in this case, asides, non-special posts.

    Do you mind show me how to do this, @joyously?

    This simplifies things so the static part is always output, once.

    $static_output = false;
    while ( have_posts() ) : the_post(); ?>
    	if ( ! is_sticky() && ! $static_output ) {
    		echo 'static content';
    		$static_output = true;
    	} ?>
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php get_template_part( 'content' ); ?>
    	</article>
    <?php
    endwhile; // End of the loop.
    Thread Starter Rodrigo

    (@rghedin)

    Thank you very much, @joyously!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Static content before posts in the loop, but after a sticky one when it exists’ is closed to new replies.