WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom home page. One sticky post. Four latest posts. (4 posts)

  1. theatereleven
    Member
    Posted 11 months ago #

    My WordPress 3.6 site has a custom home page, and blog posts are on a separate page. On the custom home page I want to include one featured post along with the four latest post. Ideally I'd like the featured post to be sticky, and have the second loop never duplicate it, but I don't know how to do that.

    My current code below works, but has two problems:

    1) the featured post is pulled by slug
    2) it is repeated in the new posts loop below it

    If anyone can shed some light on how to do this correctly, that would ROCK!!!!

    <?php
    $the_slug = 'featured-post';
    $args=array(
            'name' => $the_slug,
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 1
    );
    $postslist = get_posts( $args );
    foreach ( $postslist as $post ) :
      setup_postdata( $post ); ?>
            <div class="home-primary-post">
                    <div class="home-primary-title"><?php the_title(); ?></div>
                    <div class="home-primary-info">By  <?php the_author(); ?> on <?php the_date(); ?></div>
                    <div class="home-primary-content">
                              <div class="home-blog-date">
                                    <div class="home-blog-day"><?php the_time('j') ?></div>
                                    <div class="home-blog-month-year">
                                           <div class="home-blog-month"><?php the_time('M') ?></div>
                                           <div class="home-blog-year"><?php the_time('Y') ?></div>
                                    </div>
                              </div>                                                    
    
                              <?php the_content(); ?>
                    </div>
            </div>
    <?php
    endforeach;
    wp_reset_postdata();
    ?>
    
    <div style="clear:both;background:#dedede;height:1px;width:60%;margin:0 auto 2em auto;"></div>
    
    <?php
         $args = array( 'posts_per_page' => 4, 'order'=> 'ASC', 'orderby' => 'title' );
         $postslist = get_posts( $args );
         foreach ( $postslist as $post ) :
           setup_postdata( $post ); ?>
                 <div class="home-secondary-post">
                    <div class="home-secondary-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></div>
                    <div class="home-secondary-date"> <?php the_date(); ?></div>
                    <div class="home-secondary-excerpt"><?php the_excerpt(); ?></div>
                  </div>
         <?php
         endforeach;
         wp_reset_postdata();
    ?>eset_postdata();
                                ?>
  2. vtxyzzy
    Member
    Posted 10 months ago #

    I think this will do what you want. Use this code to show one sticky post:

    $sticky = get_option('sticky_posts');
    $not_in = array();
    if ( $sticky ) {
       $args = array(
          'posts_per_page' => 1,
          'ignore_sticky_posts' => 1,
          'post__in' => $sticky,
       );
       $sticky_posts = get_posts($args);
       if ($sticky_posts) {
          foreach ($sticky_posts as $post) {
             setup_postdata($post);
             $not_in[] = $post->ID;
             echo "<h2>Sticky Post</h2><p>$post->ID ";the_title();echo '</p>';
          }
       }
    }
    wp_reset_postdata();

    Then if you want to exclude the one sticky, add this parameter in the second list:

    'post__not_in' => $not_in,

    If you want to exclude all stickies, add this:

    'post__not_in' => $sticky,
  3. theatereleven
    Member
    Posted 10 months ago #

    Very cool - thank you for clearing the fog on this!

  4. theatereleven
    Member
    Posted 10 months ago #

    Btw - for anyone wanting to see the exact code that worked for me based on the above, here it is:

    <?php 
    
                            $sticky = get_option('sticky_posts');
                              $not_in = array();
                              if ( $sticky ) {
                                 $args = array(
                                    'posts_per_page' => 1,
                                    'ignore_sticky_posts' => 1,
                                    'post__in' => $sticky,
                                 );
                                 $sticky_posts = get_posts($args);
                                 if ($sticky_posts) {
                                    foreach ($sticky_posts as $post) {
                                       setup_postdata($post);
                                       $not_in[] = $post->ID;
                                       echo '<div class="home-sticky-post"><div class="home-secondary-title"><a href="<?php the_permalink() ?>" rel="bookmark">';
                                       the_title();
                                       echo '</a><div class="home-sticky-date">';
                                       the_date();
                                       echo ' by ';
                                       the_author();
                                       echo '</div></div><div class="home-secondary-excerpt">';
                                       the_excerpt();
                                       echo '</div></div>';
    
                                    }
                                 }
                              }
                              wp_reset_postdata();
    
                            ?>  
    
                            <?php
                                 $args = array( 'posts_per_page' => 3, 'order'=> 'ASC', 'orderby' => 'title', 'post__not_in' => $sticky );
                                 $postslist = get_posts( $args );
                                 foreach ( $postslist as $post ) :
                                   setup_postdata( $post ); ?>
                                         <div class="home-secondary-post">
                                            <div class="home-secondary-title">
                                                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                                                <div class="home-secondary-date"> <?php the_date(); ?> by <?php the_author(); ?></div>
                                            </div>
                                            <div class="home-secondary-excerpt"><?php the_excerpt(); ?></div>
                                        </div>
                                 <?php
                                 endforeach;
                                 wp_reset_postdata();
                            ?>

Reply

You must log in to post.

About this Topic