【问题标题】:What is the best method for creating your own Wordpress loops?创建自己的 Wordpress 循环的最佳方法是什么?
【发布时间】:2010-10-08 21:09:43
【问题描述】:

使用其内置函数从 Wordpress 输出内容似乎有三种主要方式,推荐使用 WP_Query

它们之间有什么区别? (我理解WP_Query是类,另外两个是方法)。

在同一页面上拥有多个循环而不相互干扰的最简洁方法是什么?

我正在寻找有关如何编写 WP 循环的示例; 例如按类别输出 2 个单独的帖子列表,包括附件、元数据等。

这是迄今为止我找到的最好的参考资料:

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    我已经使用了 WP_Query 和 get_posts。在我的一个侧边栏模板上,我使用以下循环显示来自特定类别的帖子,方法是使用自定义字段,其键为“category_to_load”,其中包含类别标签或类别名称。真正的区别在于这两种方法的实现。

    get_posts 方法在我的一些模板中看起来像这样:

    <?php    
        global $post;
        $blog_posts = get_posts( $q_string );
        foreach( $blog_posts as $post ) : 
        setup_postdata( $post );
    ?>
        <div class="blog_post">
            <div class="title">
                <h2>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
                <span class="date"><?php the_time( 'F j, Y' ); ?> by <?php the_author(); ?></span>
            </div>
            <?php the_excerpt(); ?>
        </div>
        <?php endforeach; 
    ?> 
    

    WP_Query 的实现如下所示:

    $blog_posts = new WP_Query( 'showposts=15' );
    
    while ( $blog_posts->have_posts() ) : $blog_posts->the_post(); ?>
    
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>" class="blog_post">
            <div class="title">
                <h2>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </h2>
                <span class="date"><?php the_time( 'F jS, Y' ) ?> <!-- by <?php the_author() ?> --></span>
            </div>
            <div class="entry">
                <?php the_content(); ?>
            </div>
            <p class="postmetadata"><?php the_tags( 'Tags: ', ', ', '<br />' ); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
        </div>
    
    <?php endwhile; ?>
    

    主要区别在于您不必重置全局 $post 变量,也不必在使用 WP_query 时通过在每个帖子对象上调用 setup_postdata($post) 来设置帖子数据。您还可以在 WP_Query 函数上使用可爱的 have_posts() 函数,而使用 get_posts() 则无法使用。

    您不应该使用 query_posts() 函数,除非您真的有意这样做,因为它会修改页面的主循环。请参阅docs。因此,如果您正在构建一个特殊页面来显示您的博客,那么调用 query_posts 可能会打乱页面的循环,因此您应该使用 WP_Query。

    这只是我的两分钱。我的最终建议,您的首选应该是 WP_Query。

    -克里斯

    【讨论】:

    【解决方案2】:

    WP 使用一个名为$wp_query 的对象作为主循环。我们通常看不到这个对象,因为它隐藏在 have_posts()the_post() 后面,它们只是 $wp_query-&gt;have_posts()$wp_query-&gt;the_post()

    如果你想修改主循环,你应该在循环之前使用query_posts()

    如果您想要另一个循环,您可以在新循环之前使用query_posts() 重新利用$wp_query 对象。如果需要,可以多次执行此操作。

    如果出于某种原因您需要保留 $wp_query 对象,那么您应该使用WP_Query。当然,因为have_posts()the_post()$wp_query 对象的包装器,所以您不能将它们与WP_Query 一起使用。你应该使用$your_query_obj-&gt;have_posts(),即

    $sidebar= WP_Query('category_name= sidebar');
    
    while( $sidebar->have_posts() ): $sidebar->the_post();
      the_title();
      the_content();
    endwhile;
    

    WP_Query 可能比query_posts() 更好的一个很好的例子是左侧边栏。 由于侧边栏的代码循环可能会放在主循环的顶部,query_posts() 调用将更改 $wp_query 对象并更改主循环。在这种情况下,要在边栏代码中使用query_posts(),您还需要在主循环之前使用query_posts() 来查询该循环的正确内容。

    因此在这种情况下使用 WP_Query 将保持 $wp_query 并因此保持主循环不变。

    但同样,对于一个常见的情况,query_posts() 是一种查询您的内容的好方法:

    query_posts('category_name=blog');
    
    while( have_posts() ): the_post();
      the_title();
      the_content();
    endwhile;
    

    【讨论】:

      【解决方案3】:

      来自 get_posts 的 WP 文档:

      get_posts() 也可以使用 query_posts() 可以使用的参数,因为这两个函数现在在内部使用相同的数据库查询代码。

      这两个函数之间的唯一区别是get_posts返回一个包含post记录的数组,而query_posts将记录存储在查询对象中以供模板函数(has_posts、the_post等)检索。

      它们都使用 WP_Query 对象来执行查询。

      Wordpress docs 中介绍了创建第二个循环。那里有一些链接可用于多个循环的其他示例。您会注意到每个人的做法都不同,但他们似乎都对自己的结果感到满意。

      【讨论】:

        猜你喜欢
        • 2020-09-22
        • 2017-04-21
        • 2023-01-26
        • 1970-01-01
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        相关资源
        最近更新 更多