在Wordpress中使用WP_查询显示特定页面的数组
我试图实现的是在循环中只显示某些页面。3页,不多不少 我尝试过很多事情,但就是做不完在Wordpress中使用WP_查询显示特定页面的数组,wordpress,arrays,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'); ?&g
<?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的数组
我肯定我不是第一个有这种特殊需求的人,我肯定有一个相对简单的方法来实现我想要实现的目标,但我就是想不出一个解决方案或找到一个。有人能帮我吗
非常感谢 只是想知道使用诸如get_pages()之类的东西是否比创建新的wp_查询()更好 使用这段代码,您可以生成所需页面的列表
<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\u tab变量上进行打印,将得到以下数组
</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
)
)
排列
(
[0]=>stdClass对象
(
[ID]=>2
[post_author]=>1
[发布日期]=>2010-03-2415:26:18
[发布日期格林尼治标准时间]=>2010-03-2415:26:18
[post_content]=>这是WordPress页面的一个示例。
[post_title]=>关于
[帖子摘录]=>
[发布状态]=>发布
[评论\u状态]=>打开
[ping_状态]=>打开
[发布密码]=>
[post_name]=>关于
[致吴平]=>
[ping]=>
[修改后]=>2010-03-2415:26:18
[修改后格林尼治标准时间]=>2010-03-2415:26:18
[发布内容过滤]=>
[post_parent]=>0
[guid]=>http://example.com/about/
[菜单顺序]=>0
[发布类型]=>第页
[后置mime类型]=>
[注释计数]=>0
[过滤器]=>原始
)
[1] =>stdClass对象
(
[ID]=>3
[post_author]=>1
[发布日期]=>2010-03-27 10:48:29
[发布日期格林尼治标准时间]=>2010-03-27 10:48:29
[发布内容]=>
[post_title]=>testpage1
[帖子摘录]=>
[发布状态]=>发布
[评论\u状态]=>打开
[ping_状态]=>打开
[发布密码]=>
[post_name]=>testpage1
[致吴平]=>
[ping]=>
[修改后]=>2010-03-27 10:48:29
[修改后的格林尼治标准时间]=>2010-03-27 10:48:29
[发布内容过滤]=>
[post_parent]=>0
[guid]=>http://example.com/testpage1/
[菜单顺序]=>0
[发布类型]=>第页
[后置mime类型]=>
[注释计数]=>0
[过滤器]=>原始
)
[2] =>stdClass对象
(
[ID]=>4
[post_author]=>1
[发布日期]=>2010-03-27 10:56:26
[发布日期格林尼治标准时间]=>2010-03-27 10:56:26
[发布内容]=>
[post_title]=>testpage2
[帖子摘录]=>
[发布状态]=>发布
[评论\u状态]=>打开
[ping_状态]=>打开
[发布密码]=>
[post_name]=>testpage2
[致吴平]=>
[ping]=>
[修改后]=>2010-03-27 10:56:26
[修改后的格林尼治标准时间]=>2010-03-27 10:56:26
[发布内容过滤]=>
[post_parent]=>0
[guid]=>http://example.com/testpage2/
[菜单顺序]=>0
[发布类型]=>第页
[后置mime类型]=>
[注释计数]=>0
[过滤器]=>原始
)
)
希望这是您可以使用的东西:)
只需记住在get_pages()include部分中包含您的页面ID,而不是2,3,4这已经是它们的
参考:以下是2个多月前问题的答案: 您希望显示某些页面,那么如何为foreach中的每个页面调用WP_查询:
不必担心多次调用循环,它的效果在这种规模下甚至无法被注意到。这个特定解决方案的答案其实很简单:我有一个输入错误:我有post_,但实际上它是post_,两个下划线。我错过了。我最好睁大眼睛。谢谢你,马蒂。它看起来确实有效。但是我认为创建wp_查询似乎比您给出的解决方案要简单一些。但我仍然非常感谢你的帮助。每当我处理页面时,我都会记住这个解决方案。问题是,我需要在几个不同的地方显示几个页面。其中一个是特别选项卡部分,我还有2-3个其他位置。
$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 */
}