WordPress.org

Ready to get started?Download WordPress

Forums

How can i call a sticky to the top of this loop (5 posts)

  1. l r
    Member
    Posted 5 years ago #

    I have the following on my home page which does all I need -
    Except:
    My sticky posts now fall to the bottom of the loop. Does anyone know the neatest way to regain my stickies at the top of the page.. many thanks
    Thanks!
    <?php $my_query = new WP_Query('category_name=featured&showposts=5');
    $counter=0; while ($my_query->have_posts()) : $my_query->the_post(); $counter++;
    $do_not_duplicate = $post->ID;?>
    <div <?php post_class(); id="post-<?php the_ID(); ?>">
    <?php if ($counter > 1) ?>
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>
    <div class="post" id="post-<?php the_ID(); ?>">
    <?php
    if ($counter < 1) {
    the_content('');
    } else {
    the_excerpt('');
    }
    ?>

  2. MichaelH
    Member
    Posted 5 years ago #

    In addition to displaying sticky post(s) first, that's the goal here?

    Here's my guess:
    1. Get 5 posts in the category=featured, and the sticky posts.
    2. On the first of those posts display the_content and on the rest display the_excerpt.

    Question:
    When displaying the sticky posts, what displays--the_content or the_excerpt?

  3. l r
    Member
    Posted 5 years ago #

    Hi Sorry - I was not very clear.

    Thanks for getting back to me.

    1. Yes I want 5 posts from category=featured, some will be stickies, the stickies should be at the top.

    2. All posts display either
    full content or shortened content via the more tag
    or if excerpt exists show a bespoke excerpt.

    I am hard coding the 'read more of this entry >>'
    It's not ideal but I couldn't get it to link to the full content consistently.
    When the sticky displays - I have the excerpt or the content whichever exists.

  4. MichaelH
    Member
    Posted 5 years ago #

    Well you can play with this:

    <?php
    $cat=get_cat_ID('featured');
    $posts=get_posts(array('post__in'=>get_option('sticky_posts')));  // get sticky posts
    if ($posts) {
      foreach($posts as $post) {
        setup_postdata($post);
        if ( in_category($cat) ) { // only sticky posts in selected category
          ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
          the_content('Read the rest of this entry &raquo;');
        }
      }
    }
    $posts=get_posts(array('post__not_in'=>get_option('sticky_posts'),'cat'=>$cat,'showposts'=>5));  //get posts in our category, exclude sticky posts
    if ($posts) {
      foreach($posts as $post) {
        setup_postdata($post);
        ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        the_content('Read the rest of this entry &raquo;');
      }
    }
    ?>

    Needed two loops, one to process the sticky posts in your category, then another to process the non-sticky posts.

  5. l r
    Member
    Posted 5 years ago #

    Hi Michael...
    quick test ..it kind of does it - need to mess around... will get there..
    Thank you..

Topic Closed

This topic has been closed to new replies.

About this Topic