spot_img
HomeJoomla教程Joomla教程菜鸟教程 获取所有已注册帖子类型的列表

Joomla教程菜鸟教程 获取所有已注册帖子类型的列表

JasperAI 10000字免费额度试用

我想获得我注册的所有帖子类型的列表(数组)。

准确地说,我想取回他们的鼻涕虫。

有人可以帮我吗? 谢谢!

@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.
    }
}
?>

5分钟生成10篇英文软文article forge软件试用
siteground 小红书电商教程
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES
spot_img