8

使用 Wordpress 的内置功能从 Wordpress 输出内容似乎有三种主要方法,WP_Query推荐的一种方法是:

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

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

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

这是迄今为止我找到的最佳参考:

4

3 回答 3

6

我已经使用了 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() 函数,除非你真的想这样做,因为它修改了页面的主循环。请参阅文档。因此,如果您正在构建一个特殊页面来显示您的博客,那么调用 query_posts 可能会打乱页面的循环,因此您应该使用 WP_Query。

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

-克里斯

于 2009-02-21T17:33:34.120 回答
3

来自 get_posts 的 WP 文档:

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

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

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

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

于 2009-02-20T22:13:20.897 回答
1

WP 使用一个称为$wp_query主循环的对象。我们通常看不到这个对象,因为它隐藏在后面have_posts()the_post()并且只是和的包装 $wp_query->have_posts()$wp_query->the_post()

如果要修改主循环,则应query_posts()在循环之前使用。

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

如果由于某种原因您需要保留 $wp_query 对象,那么您应该使用WP_Query. 当然,因为have_posts()the_post()是对象的包装器,$wp_query所以不能将它们与WP_Query. 你应该使用$your_query_obj->have_posts()ie

$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;
于 2009-12-23T19:33:44.900 回答