我在开发插件时遇到过这个问题。 我要遍历网站的所有页面并打印标题和ID。 当我做简单循环时,它只是不打印任何东西。
有没有办法在我的插件选项中做一个循环?
答案 0 :(得分:0)
找到解决方案。
我不知道插件选项页面中是否存在某种块,但我们可以使用一小部分代码遍历每个自定义帖子类型:
<?php
//Search only for custom post type which is public.
$args = array('public'=>true);
//Get all custom post type name
$allMyCustomPostTypes = get_post_types($args);
foreach ($allMyCustomPostTypes as $myCustomPostType) {
//Create a filter for get_posts with every custom post type
$filter = array('numberposts' => 5, 'post_type' => $myCustomPostType);
//Pass the value to *get_posts*
$theposts = get_posts($filter);
foreach ($theposts as $thepost) {
//Easy print the name of the current post of the current custom post type
echo $thepost->post_title;
};
};
?>
我们在这里,希望这有助于某人。