创建自己的Wordpress循环的最佳方法是什么? [英] What is the best method for creating your own Wordpress loops?

查看:71
本文介绍了创建自己的Wordpress循环的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There seem to be three main ways to output content from Wordpress using its built-in functions, with WP_Query being the recommended one:

  • WP_Query
  • query_posts
  • get_posts

它们之间有什么区别? (我知道WP_Query是类,另外两个是方法).

What are the differences between them? (I understand that WP_Query is the class, and the other two are methods).

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

What is the cleanest way to have multiple loops on the same page, without any of them interfering with each other?

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

I'm looking for examples of how you program your WP loops; e.g. output 2 separate post lists by category, with attachments, meta data etc.

这是到目前为止我找到的最好的参考:

This is the best reference I found so far:

推荐答案

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

I've used both WP_Query and get_posts. On one of my sidebar templates, I use the following loop to display posts from a particular category by using custom fields with a key of 'category_to_load' which contains the category slug or category name. The real difference comes in the implementation of either method.

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

The get_posts method looks like so in some of my templates:

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

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

The main difference is that you don't have to reset the global $post variable and you also don't have to set up the post data by calling setup_postdata($post) on each post object when you use WP_query. You can also use the lovely have_posts() function on the WP_Query function, which is not available using get_posts().

除非您确实要这么做,否则不应使用query_posts()函数,因为它会修改页面的主循环.请参见文档.因此,如果您要构建一个特殊的页面来显示您的博客,则调用query_posts可能会弄乱页面的循环,因此您应该使用WP_Query.

You shouldn't use the query_posts() function unless you really mean to because it modifies the main loop for the page. See the docs. So if you're building a special page to display your blog on, then calling query_posts may mess up the page's loop, so you should use WP_Query.

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

That's just my two cents. My ultimate suggestion, your first choice should be WP_Query.

-克里斯

这篇关于创建自己的Wordpress循环的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆