我想通过get\\u post\\u types()参数筛选所有帖子类型。
public static function get_post_type()
{
if(!$post_type = wp_cache_get(\'post_type\', \'dsad\'))
{
$post_type = get_post_types(array(
\'public\' => true,
\'show_ui\' => true
));
wp_cache_set(\'post_type\', $post_type, \'dsad\');
}
return $post_type;
}
但是,使用此代码,它将返回以下值:
array(3) {
["post"]=>
string(4) "post"
["page"]=>
string(4) "page"
["attachment"]=>
string(10) "attachment"
}
如何获取所有帖子类型?
SO网友:Priyanka Modi
用于获取所有注册帖子类型的内置WordPress函数:get_post_types()
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually fine.
add_action( \'init\', \'wpse34410_init\', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], \'objects\' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you\'ll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
?>
SO网友:Andrea Somovigo
这个答案可以给你一个重要的建议:action init hook and get_post_types
您必须在init
这可能是用来注册CPT的钩子,所以我认为:
add_action(\'wp_loaded\', \'get_post_type\');
function get_post_type()
{
if(!$post_type = wp_cache_get(\'post_type\', \'dsad\'))
{
$post_type = get_post_types(array(
\'public\' => true,
\'show_ui\' => true
));
wp_cache_set(\'post_type\', $post_type, \'dsad\');
}
return $post_type;
}
将起作用