在Wordpress中使用WP_Query显示某些页面的数组

时间:2010-04-08 09:05:56

标签: wordpress arrays

我想要实现的只是在循环中显示某些页面。 3,某些页面,而不是更多,而不是更少。

我尝试了很多东西,但我无法完成它。

<?php
    $special_tabs = new WP_Query(array ('post_type'=>'page','post__in'=>array(100,102,104)))
?>

根据我的理解,它的作用是显示一系列页面,然后包含那些ID。

<?php
    $special_tabs = new WP_Query('page_id=100');
?>

这只显示一个特定的页面,它不支持显示不同ID的数组。

我确信我不是第一个有这种特殊需求的人,我确信有一种相对简单的方法来实现我想要实现的目标,但我无法想出一个解决方案或者找一个。有人可以帮帮我吗?

非常感谢提前!

2 个答案:

答案 0 :(得分:1)

只是想知道使用像get_pages()这样的东西而不是创建一个新的wp_query()会更好吗?

使用这一小段代码,您可以生成所需页面列表。

<ul>
<?php $special_tabs = get_pages ('sort_column=post_title&echo=0&include=2,3,4&title_li='); ?>
<?php 
foreach ($special_tabs as $tab) {
  $title = $tab->post_title;
  echo "<li>".$title."</li>";
}
?>
</ul>

如果你在$ special_tab变量上执行print_r,你将得到以下数组

</php
echo"<pre>";
echo print_r($special_tabs);
echo"</pre>";
?>


    Array
(
    [0] => stdClass Object
        (
            [ID] => 2
            [post_author] => 1
            [post_date] => 2010-03-24 15:26:18
            [post_date_gmt] => 2010-03-24 15:26:18
            [post_content] => This is an example of a WordPress page.
            [post_title] => About
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => about
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-24 15:26:18
            [post_modified_gmt] => 2010-03-24 15:26:18
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/about/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [1] => stdClass Object
        (
            [ID] => 3
            [post_author] => 1
            [post_date] => 2010-03-27 10:48:29
            [post_date_gmt] => 2010-03-27 10:48:29
            [post_content] => 
            [post_title] => testpage1
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => testpage1
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-27 10:48:29
            [post_modified_gmt] => 2010-03-27 10:48:29
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/testpage1/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [2] => stdClass Object
        (
            [ID] => 4
            [post_author] => 1
            [post_date] => 2010-03-27 10:56:26
            [post_date_gmt] => 2010-03-27 10:56:26
            [post_content] => 
            [post_title] => testpage2
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => testpage2
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-27 10:56:26
            [post_modified_gmt] => 2010-03-27 10:56:26
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/testpage2/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)

希望这是你可以使用的东西...... :) 只记得在你的get_pages()包含部分中包含你的页面ID,而不是它们已经存在的2,3,4 ...

参考:http://codex.wordpress.org/Function_Reference/get_pages

答案 1 :(得分:0)

以下是2个月以上问题的答案:

您想要显示某些页面,那么为foreach中的每个页面调用WP_Query怎么样:

$certain_pages = array(100,102,104);
foreach( $certain_pages as $a_page ) {
    $special_tabs = new WP_Query('page_id='.$a_page);
    /* (sample) loop goes here */
    if ($special_tabs->have_posts()) : while ($special_tabs->have_posts()) : $special_tabs->the_post();
        the_content();
    endwhile; endif;
    /* loop ends */
}

不要担心多次调用循环,它的效果甚至无法在此范围内被注意到。