如何在wordpress中根据自定义类别获取自定义帖子?

时间:2012-04-20 07:44:53

标签: php wordpress

我尝试使用以下代码来创建自定义帖子和自定义类别

<?php

/*** added for browse plugin custom post - start ***/
add_action('init', 'demoplugin_register');
    function demoplugin_register() {
          $labels = array(
            'name' => _x('Demoplugin', 'post type general name'),
            'singular_name' => _x('Demoplugin Entry', 'post type singular name'),
            'add_new' => _x('Add New Demoplugin', 'demoplugin'),
            'add_new_item' => __('Add New Demoplugin Entry'),
            'edit_item' => __('Edit Demoplugin Entry'),
            'new_item' => __('New Demoplugin Entry'),
            'view_item' => __('View Demoplugin Entry'),
            'search_items' => __('Search Demoplugin Entries'),
            'not_found' =>  __('No Demoplugin Entries found'),
            'not_found_in_trash' => __('No Demoplugin Entries found in Trash'), 
            'parent_item_colon' => ''
          );

        $slugRule = get_option('category_base');
        //if($slugRule == "") $slugRule = 'category';

        global $paged;

        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_ui' => true,
            '_builtin' => false,
            'rewrite' => array('slug'=>'demoplugin','with_front'=>false),
            'capability_type' => 'post',
            'hierarchical' => false,
            'show_in_nav_menus'=> false,
            'query_var' => true,
            'paged' => $paged,          
            'menu_position' => 5,
            'supports' => array('title','thumbnail','excerpt','editor','comments')
        );

        register_post_type('demoplugin' , $args);


        register_taxonomy("demoplugin_entries", 
                            array("demoplugin"), 
                            array(  "hierarchical" => true, 
                                    "label" => "Demoplugin Categories", 
                                    "singular_label" => "Demoplugin Categories", 
                                    'rewrite' => array('slug' => 'demoplugin-category'),
                                    "query_var" => true,
                                    'paged' => $paged
                                ));  
flush_rewrite_rules( false );   
    }
    function demoplugin_taxonomies() {

    register_taxonomy(
        'plugtag',
        'demoplugin',
        array(
            'hierarchical' => false,
            'label' => 'Demoplugin Tags',
            'query_var' => true,
            'rewrite' => array( 'slug' => 'plugtag' ),
        )
    );

}
add_action('init', 'demoplugin_taxonomies', 0);

    add_action('admin_init', 'add_demoplugin');
    flush_rewrite_rules(false);

    add_action('save_post', 'update_demoplugin');
    function add_demoplugin(){
        add_meta_box("demoplugin_details", "Demoplugin Options", "demoplugin_options", "demoplugin", "normal", "low");
    }
    function demoplugin_options(){
        global $post;
        $custom = get_post_custom($post->ID);
        $demoplugin_path_url = $custom["demoplugin_path_url"][0];
        $download_url = $custom["download_url"][0];
        $demoplugin_video_url = $custom["demoplugin_video_url"][0];

        //$demoplugin_excerpt = $custom["demoplugin_excerpt"][0];
        $demoplugin_radiogroup = $custom["demoplugin_radiogroup"][0];
        if ($demoplugin_radiogroup == '') $demoplugin_radiogroup = 'demoplugin_post_action';

/*** added for browse plugin custom post - stop ***/

?>

上面的代码来自functions.php。我的要求是根据自定义类别列出自定义帖子。

我已经尝试了很多方法来实现我的要求,但它失败了。

善意的建议。

1 个答案:

答案 0 :(得分:1)

为什么会有paged个参数?没有。
query_var => true无效,应该为false或设置为字符串,我建议您完全跳过它 对于<{1}}数组 singular_label },分类标准也没有singular_name

首先阅读文档并验证您的代码
http://codex.wordpress.org/Function_Reference/register_post_type
http://codex.wordpress.org/Function_Reference/register_taxonomy