简体   繁体   中英

Multiple Loops on Home Page

I'm relatively new to customizing WP themes and I'm trying to do so here by having a section that pulls the latest post (working), pulls recent 3 posts from category ID 35 (not working), and than the traditional recent posts (working).

Direction on how to achieve these features would be great, thanks!

<?php get_header(); ?>

<!-- Row for featured post -->
    <?php
        $args = array( 'numberposts' => '1');
        $recent_posts = wp_get_recent_posts( $args );
        $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

        foreach( $recent_posts as $recent ){
            $post_author = get_user_by( 'id', $recent['post_author'] );
            echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';

            echo '<div class="row featured-post-meta"><div class="small-8 columns">';
            echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a></h2>';
            echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
            echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
            echo '</div></div></div>';
        }
    ?>

<!-- Row for widgets -->
<div class="teal-band-stripes">
    <div class="row collapse">
        <div class="large-5 columns">
            <?php dynamic_sidebar("Home Widget Area 1"); ?>
        </div>
        <div class="large-5 large-offset-2 columns">
            <?php dynamic_sidebar("Home Widget Area 2"); ?>
            <p class="follow-link" >Follow us on <a href="http://twitter.com/bazaarvoice" target="_blank">Twitter</a></p>
        </div>
    </div>
</div>

<!-- Row for featured posts -->
<div class="row">
    <div class="small-12 columns" id="featured-content">
        <h5 style="margin-left:15px;">Featured Posts</h5>

        <?php
            $feature_content = get_template_part( 'content', get_post_format() );
            query_posts('cat=35','posts_per_page=3');

            while (have_posts()) : the_post();
                echo $feature_content;
            endwhile;
        ?>
    </div>
</div>

<!-- Row for widget ad -->
<div class="teal-band-stripes">
    <div class="row collapse">
        <div class="small-10 small-centered columns" id="ad-home">
            <?php dynamic_sidebar("Home Ad"); ?>
        </div>
    </div>
</div>

<!-- Row for main content area -->
<div class="row">
    <div class="small-12 columns" id="content" role="main">
        <h5 style="margin-left:15px;">Latest Posts</h5>
        <?php rewind_posts(); ?>
    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>

        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>

    <?php endif; // end have_posts() check ?>

    </div>
</div>

<!-- Row for widgets -->
<div class="teal-band-stripes" id="widget-home-footer">
    <div class="row collapse">
        <div class="large-5 columns">
            <?php dynamic_sidebar("Home Widget Area 3"); ?>
        </div>
        <div class="large-5 large-offset-2 columns">
            <?php dynamic_sidebar("Home Widget Area 4"); ?>
        </div>
    </div>
</div>

You need to reset the query with wp_reset_query() after each loop. If not the second and so on queries will not work.

See http://codex.wordpress.org/Function_Reference/wp_reset_query

Try using the WP_Query() function instead of query_posts()

Even WordPress says to not use that function

Note that you'll need to use the wp_reset_postdata() function to be able to restore original post data

EDIT: Not sure if you need the following line:

$feature_content = get_template_part( 'content', get_post_format() );

You should be able to use $the_query->the_content();

<!-- Row for featured posts -->
<div class="row">
    <div class="small-12 columns" id="featured-content">
        <h5 style="margin-left:15px;">Featured Posts</h5>

        <?php
            $feature_content = get_template_part( 'content', get_post_format() );

            $the_query = new WP_Query( array( 'cat' => '35', 'posts_per_page' => '3' ) );

            while ($the_query->have_posts()) : $the_query->the_post();
                echo $feature_content;
            endwhile;
            wp_reset_postdata();
        ?>
    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM