获取已注册的自定义邮政类型以获取所有自定义分类

时间:2014-03-20 作者:M Ali Salim

我正在尝试通过主题或插件注册所有自定义帖子类型,但没有任何方法有效。

$postargs = array(
   \'public\'   => true,
   \'_builtin\' => false
);

$postoutput = \'names\';
$postoperator = \'and\';    
$post_types = get_post_types( $postargs, \'names\', \'and\' ); 
/// Main Loop Start
foreach ($post_types as $post_type){  
//and then i want to Get all the Custom Taxonomies for those post types in a loop
$taxonomies = get_object_taxonomies(\'$post_type\', \'objects\');    
// Loop 
foreach ($taxonomies as $taxonomy){
// Do Stuff with taxonomy
}    // Loop




    }    /// Main Loop Ends

But the problem is only WordPress Defaults Post Types are showing up if i do

\'_builtin\' => true
发布第页附件

这是注册帖子类型的代码我没有任何帖子类型,我目前在主题中有8个自定义帖子类型。该帖子已使用此代码注册。。。

$args = array(            
            \'labels\' => array(
                \'name\' => __( \'Faq\', \'yit\' ),
                \'singular_name\' => __( \'Faq\', \'yit\' ),
                \'plural_name\' => __( \'Faqs\', \'yit\' ),
                \'item_name_sing\' => __( \'Faq\', \'yit\' ),
                \'item_name_plur\' => __( \'Faqs\', \'yit\' ),
                \'add_new\' => __( \'Add New Faq\', \'yit\' ),
                \'add_new_item\' => __( \'Add New Faq\', \'yit\' ),
                \'edit\' => __( \'Edit\', \'yit\' ),
                \'edit_item\' => __( \'Edit Faq\', \'yit\' ),
                \'new_item\' => __( \'New Faq\', \'yit\' ),
                \'view\' => __( \'View Faq\', \'yit\' ),
                \'view_item\' => __( \'View Faq\', \'yit\' ),
                \'search_items\' => __( \'Search Faqs\', \'yit\' ),
                \'not_found\' => __( \'No Faqs\', \'yit\' ),
                \'not_found_in_trash\' => __( \'No Faqs in the Trash\', \'yit\' ),
            ),            
            \'hierarchical\' => false,
            \'public\' => true,
            //\'menu_position\' => 30,
            //\'icon_menu\' => ,
            \'has_archive\' => \'faq\',
            \'rewrite\' => array( \'slug\' => apply_filters( \'yit_faqs_rewrite\', \'faq\' ) ),
            \'supports\' => array( \'title\', \'editor\', \'cats\'),
            \'description\' => "Faq"

        );
        register_post_type(\'faq\', $args);   

3 个回复
最合适的回答,由SO网友:M Ali Salim 整理而成

好的,我发现问题是在wordpress访问任何注册的帖子类型之前执行了var\\u dump。因此,我在其优先级中添加了199,它起到了作用谢谢大家的帮助。。。

function test(){
var_dump( get_post_types(array(\'_builtin\' => false)));
}
add_action (\'admin_init\',\'test\', 199);

SO网友:MBL

请尝试下面的(已测试)代码。我认为问题在于$post_type 参数到get_object_taxonomies (也就是说,你用引号将其传递为\'$post_type\'. 如果取消对两者的注释var_dump\'您将看到输出了什么信息。

<?php
    $postargs = array(
       \'public\'   => true,
       \'_builtin\' => false
    );

    $post_types = get_post_types( $postargs, \'names\', \'and\' ); 

    // Main Loop Start
    foreach ($post_types as $post_type){  
        //var_dump($post_type); 
        $taxonomies = get_object_taxonomies($post_type, \'objects\');    
        foreach ($taxonomies as $taxonomy){
                //var_dump($taxonomy);

                // Do Stuff with taxonomy here


            }//foreach taxonomy
    }//foreach post_type
?>
注意:我已经用主题注册的自定义帖子类型测试过了,但没有用插件注册的自定义帖子类型测试过,但这应该也适用于那些

SO网友:mindlogixtech

我发现问题不在于使用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();
    // More custom code will go here
}
使用非常简单。

结束

相关推荐