我在WordPress中创建了几个自定义帖子类型,并希望获得在网站上创建的所有自定义帖子类型的列表,但是,您认为这个功能的函数不会返回自定义帖子类型一点都不。
get_post_types();
这不会返回自定义帖子类型名称。
自定义帖子的注册方式如下:
function custom_post_type_test() {
$args = array( 'public' => true, 'label' => 'Test' );
register_post_type( 'testtype', $args );
}
add_action( 'init', 'custom_post_type_test' );
我在互联网上到处搜索但无法在任何地方找到解决方案! 有人有这方面的任何信息吗?
由于
答案 0 :(得分:1)
使用get_post_types()
,但将_builtin
参数设置为false,如下所示:
$args = array( '_builtin' => false );
$post_types = get_post_types( $args );
将_builtin
设置为false会告诉WordPress排除内置帖子类型并仅返回自定义帖子类型。
答案 1 :(得分:0)
add_action('admin_init', function(){
$args = array( '_builtin' => false );
$post_types = get_post_types( $args );
var_dump($post_types);
});
答案 2 :(得分:-1)
我发现问题不在于get_post_types函数。这是我们用来调用这个函数的问题。我清楚地描述了它。
创建一个功能
`function get_all_custom_post_types(){
$args = array('public' => true ,'_builtin' => false );
$output = 'objects';//'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$custom_post_types = get_post_types( $args, $output, $operator );
$post_types = array();
foreach ( $custom_post_types as $k => $post_type ) {
$post_types = $post_type;
}
return $post_types ;
}`
使用此自定义函数调用此函数,但仅在任何wordpress操作或过滤器回调函数中使用。比如在创建回调函数add_metabox动作时,则调用此函数。 `add_action(' add_meta_boxes',' add_mb_option');
function add_mb_option(){
add_meta_box('mb-option', 'custom metabox detail', 'add_mb_option_element',
'custom_metabox', 'normal', 'high');
}
function add_mb_option_element(){
$ cpts = get_all_custom_post_types(); //更多自定义代码将在这里 }`
使用非常简单。
快乐的编码。
答案 3 :(得分:-1)
是的,你可以在管理员和前端获得它
add_action('admin_init', function(){
$post_types = get_post_types( array( 'public' => true ), 'names' );
var_dump($post_types);
});