简体   繁体   English

主页上的多个循环

[英]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). 我是自定义WP主题的相对较新的人员,因此我想通过在此部分中进行以下操作来实现此功能:提取最新的帖子(有效),从类别ID 35中提取最近的3个帖子(无效),以及传统的最近帖子(工作)。

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. 每个循环后,您都需要使用wp_reset_query()重置查询。 If not the second and so on queries will not work. 如果不是第二个,依此类推,查询将无法进行。

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

Try using the WP_Query() function instead of query_posts() 尝试使用WP_Query()函数而不是query_posts()

Even WordPress says to not use that function 甚至WordPress也表示不使用该功能

Note that you'll need to use the wp_reset_postdata() function to be able to restore original post data 请注意,您需要使用wp_reset_postdata()函数才能还原原始的帖子数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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