如何在WordPress中检索post_类型的查询列表?

如何在WordPress中检索post_类型的查询列表?,wordpress,Wordpress,我正在开发一个插件,需要WordPress后端中存在的帖子类型列表,我查看了WordPress codex的页面,但找不到解决方案您可以尝试此代码 <?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', 'wpse3441

我正在开发一个插件,需要WordPress后端中存在的帖子类型列表,我查看了WordPress codex的页面,但找不到解决方案

您可以尝试此代码

<?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;
    }
}
}
您可以尝试此代码

<?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;
    }
}
}