我想获得我注册的所有帖子类型的列表(数组)。
准确地说,我想取回他们的鼻涕虫。
有人可以帮我吗? 谢谢!
@EAMann 的回答是正确的,但是已经有一个内置的 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;
}
}
}
最简单的方法是使用 WordPress 函数 get_post_types();
<?php
$get_cpt_args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $get_cpt_args, 'object' ); // use 'names' if you want to get only name of the post type.
// see the registered post types
echo '<pre>';
print_r($post_types);
echo '</pre>';
// do something with array
if ( $post_types ) {
foreach ( $post_types as $cpt_key => $cpt_val ) {
// do something.
}
}
?>