主页上的多个循环

时间:2014-02-06 19:41:43

标签: php wordpress

我对自定义WP主题相对较新,我试图在这里通过一个部分提取最新帖子(工作),从类别ID 35(不工作)拉出最近3个帖子,而不是最近的传统帖子(工作)。

如何实现这些功能的方向非常棒,谢谢!

<?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>

2 个答案:

答案 0 :(得分:1)

您需要在每次循环后使用wp_reset_query()重置查询。如果不是,第二个等等查询将无效。

请参阅http://codex.wordpress.org/Function_Reference/wp_reset_query

答案 1 :(得分:0)

尝试使用WP_Query()功能代替query_posts()

WordPress says to not use that function

请注意,您需要使用wp_reset_postdata()功能才能恢复原始帖子数据

编辑:不确定您是否需要以下一行:

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

您应该可以使用$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>