WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] I have the code I need - but problems inserting it (2 posts)

  1. Caneval
    Member
    Posted 1 year ago #

    I have two columns - one for latest posts, and the other for posts in a 'featured' category.
    When I publish a new featured post, I want it to appear as a latest post, but not also appear on the featured posts column at the same time. To do this, I believe I need to use/adapt code from one of the following links:

    http://www.wpbeginner.com/wp-themes/how-to-avoid-duplicate-post-display-with-multiple-loops-in-wordpress/
    http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts
    http://pastebin.com/1vKfW12p

    The only problem is, I don't understand the code enough in order to make in work on my template/website. I'm sure it's simple to install and that makes it very frustrating. Can someone please point me in the right direction?

    Here are the relevant loops on my index.php page:

    Latest posts loop:

    <div class="latestpost">
    
    <?php if (have_posts()) : ?>
    <?php query_posts("showposts=1"); // show one latest post only ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php do_atomic( 'before_entry' ); // origin_before_entry ?>
    <div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
    <?php do_atomic( 'open_entry' ); // origin_open_entry ?>
    
    -----content goes here-----
    
    <?php do_atomic( 'close_entry' ); // origin_close_entry ?>
    </div><!-- .hentry -->
    <?php do_atomic( 'after_entry' ); // origin_after_entry ?>
    <?php endwhile; ?>
    <?php query_posts("showposts=5&offset=1"); // show 4 latests posts excluding the latest ?>
    <?php else : ?>
    <?php get_template_part( 'loop-error' ); // Loads the loop-error.php template. ?>
    <?php endif; ?>
    </div><!-- .latestpost -->

    Featured posts loop:

    <div class="ifeed"; /*third column*/ >
    <?php
     $topbox = get_posts('numberposts=3&category=8');
     foreach($topbox as $post) :
        setup_postdata($post);
     ?>
    
    -----content goes here------
    
     <?php endforeach; ?>
    </div><!-- .ifeed -->
  2. Caneval
    Member
    Posted 1 year ago #

    Okay, solved my own problem - finally!

    I adapted code from the following:
    http://wp-snippets.com/avoid-duplicate-posts-in-multiple-loops/
    And it works beautifully so far.

    My index.php now looks like:

    Latest posts:

    div class="latestpost">
    
    <?php if (have_posts()) : ?>
    <?php query_posts("showposts=1"); // show one latest post only ?>
    <?php
    $ids = array();
    while (have_posts()) : the_post();
    ?>
    
    <?php do_atomic( 'before_entry' ); // origin_before_entry ?>
    <div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
    <?php do_atomic( 'open_entry' ); // origin_open_entry ?>
    
    ---content---
    
    <?php do_atomic( 'close_entry' ); // origin_close_entry ?>
    </div><!-- .hentry -->
    
    <?php $ids[]= $post->ID; ?>
    
    <?php do_atomic( 'after_entry' ); // origin_after_entry ?>
    <?php endwhile; ?>
    <?php query_posts("showposts=5&offset=1"); // show 4 latests posts excluding the latest ?>
    <?php else : ?>
    <?php get_template_part( 'loop-error' ); // Loads the loop-error.php template. ?>
    <?php endif; ?>
    </div><!-- .latestpost -->

    Featured posts:

    <div class="ifeed"; /*third column*/ >
    
    <?php
    query_posts("category_name=featured&showposts=3");
    while (have_posts()) : the_post();
    if (!in_array($post->ID, $ids)) {
    ;?>
    
    ---content---
    
    <?php }
    endwhile; ?>
    <?php wp_reset_query(); ?>
    </div><!-- .ifeed -->

    Hope that helps someone!

Topic Closed

This topic has been closed to new replies.

About this Topic