【问题标题】:How to display a set of combined posts and pages using Wordpress' query_posts() method如何使用 Wordpress 的 query_posts() 方法显示一组组合的帖子和页面
【发布时间】:2010-10-28 02:35:27
【问题描述】:

我有一个正在使用的模板,它可以选择在首页的特色部分中显示一组帖子,或者在同一特色区域中显示一组指定的页面。我找到了显示非此即彼的代码,但是 我不确定如何将两者结合在一起并获得一组帖子和页面的列表。

据我了解,query_posts() 会覆盖 Wordpress 在页面上显示的任何项目集,因此在这里,根据主题所处的模式,它会将参数传递给 query_posts() 以获取特定类别的帖子,或者传入一个页面数组:

<div id="slides">
    <?php global $ids;
    $ids = array(); 

    $featured_cat = get_option('mytemplate_feat_cat'); 
    $featured_num = get_option('mytemplate_featured_num'); 

    if (get_option('mytemplate_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
    else {
        global $pages_number;

        if (get_option('mytemplate_feat_pages') <> '') $featured_num = count(get_option('mytemplate_feat_pages'));
        else $featured_num = $pages_number;

        query_posts(array
                        ('post_type' => 'page',
                        'orderby' => 'menu_order',
                        'order' => 'ASC',
                        'post__in' => get_option('mytemplate_feat_pages'),
                        'showposts' => $featured_num
                    ));
    } ?>
            <!-- Start my loop to display everything-->
    <?php if (have_posts()) : while (have_posts()) : the_post();
    global $post; ?>

到目前为止,我已经使它更简洁了,但无法克服最后一点如何组合参数说 query_posts(getMyPostsArray().AddList(ohINeedACouplePagesToo())) //Yes ,我知道这看起来像 C# 什么的...我不是 PHP 人..

这里的代码更易读,更接近我想要的:

            $featured_cat = get_option('mytemplate_feat_cat'); 
                //I combined featured_num to get the total number of featured items to display
        $featured_num = get_option('mytemplate_featured_num') + count(get_option('mytemplate_feat_pages'));; 

query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));

            //I think this second line overwrites the first query_posts() :-/
            query_posts(array
                            ('post_type' => 'page',
                            'orderby' => 'menu_order',
                            'order' => 'ASC',
                            'post__in' => get_option('mytemplate_feat_pages'),
                            'showposts' => $featured_num
                        ));

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    Ryan,你为什么不直接进行两个查询和两个循环?

    query_posts("post_type=post&showposts=3");
    while (have_posts()) { the_post(); }
    
    query_posts(array(
        "post_type" => "page",
        "post__in" => get_option("mytemplate_feat_pages"),
        "showposts" => 5
    ));
    while (have_posts()) { the_post(); }
    

    如果你需要一定数量的帖子和页面来填充空间,你也可以使用$wp_query-&gt;found_posts来实际计算你有什么和你需要什么。这可能是最简单的解决方案,因为即使您可以在 SQL 中获取帖子和页面,您也可能很难对它们进行排序,因为帖子没有菜单顺序,而您不喜欢按发布日期排序的页面.

    希望有所帮助, 干杯!

    【讨论】:

    • 运行两次就像一个魅力。出于某种原因,我在想 query_posts 每页只能运行一次。我只运行了两次,一次用于帖子,然后是页面。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多