gpt4 book ai didi

wordpress - 在 WordPress 中使用 WP_Query 显示某些页面的数组

转载 作者:行者123 更新时间:2023-12-02 12:28:26 25 4
gpt4 key购买 nike

我想要实现的目标是仅循环显示某些页面。 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的数组。

我确信我不是第一个有这种特定需求的人,并且我确信有一种相对简单的方法可以实现我想要实现的目标,但我就是想不出解决方案或找到一个。有人可以帮我解决这个问题吗?

非常感谢!

最佳答案

只是想知道使用 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

关于wordpress - 在 WordPress 中使用 WP_Query 显示某些页面的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com