当我在WordPress中创建新页面时,我可以选择从我的主题中指定要使用的模板(从界面右侧的下拉列表中)。
我需要找到哪些可用模板未使用,以便我可以删除它们。
请问这是怎么做的?
WP版本为4.2.2
当我在WordPress中创建新页面时,我可以选择从我的主题中指定要使用的模板(从界面右侧的下拉列表中)。
我需要找到哪些可用模板未使用,以便我可以删除它们。
请问这是怎么做的?
WP版本为4.2.2
您需要做的是比较元字段的值_wp_page_template
, 其中包含为具有可用页面模板的单个页面选择的页面模板。
为此,您需要构建一个已使用模板的数组,因为您希望所有页面都使用模板,如下所示:
使用array_unique
获取唯一值。然后需要获取可用的页面模板,如下所示:
更新时间:
array_intersect_assoc()
已从中删除WP_Theme::get_page_templates()
方法因此,我们可以使用theme_page_templates
过滤器,无需使用javascript或一些巧妙的对象缓存技巧here by @MikeSchinkel 或here by @gmazzap.
下面是一个演示(PHP 5.4+):
add_filter( \'theme_page_templates\', function( $page_templates, $obj, $post )
{
// Restrict to the post.php loading
if( ! did_action( \'load-post.php\' ) )
return $page_templates;
foreach( (array) $page_templates as $key => $template )
{
$posts = get_posts(
[
\'post_type\' => \'any\',
\'post_status\' => \'any\',
\'posts_per_page\' => 10,
\'fields\' => \'ids\',
\'meta_query\' => [
[
\'key\' => \'_wp_page_template\',
\'value\' => $key,
\'compare\' => \'=\',
]
]
]
);
$count = count( $posts );
// Add the count to the template name in the dropdown. Use 10+ for >= 10
$page_templates[$key] = sprintf(
\'%s (%s)\',
$template,
$count >= 10 ? \'10+\' : $count
);
}
return $page_templates;
}, 10, 3 );
Example:
在这里,我们可以看到它的样子,使用计数信息添加到模板名称中:希望您可以根据自己的需要进行调整!我正在用wordpress和画布主题(来自WooThemes)构建一个网站。我使用顶部导航在用户登录时显示项目。当用户未登录时,您只能看到“成为成员”和“登录”。当用户登录时,他/她会看到另一个带有其他菜单项的导航。我想到了以下解决方案:注册新导航应用钩子检查用户是否登录根据结果,为注册用户应用导航,否则显示其他我将此代码放在我的函数中。php:add_action( \'init\', \'register_top_menu_myisa\', 10 ); function regist