无法区分Single.php上自定义帖子类型下的两个类别

时间:2015-05-29 作者:Disha

我已经创建了一个自定义帖子类型,如中所述my older question

当我尝试使用get\\u the\\u category()函数来检索帖子的类别(如果是symphony或noir)。它返回“Array”。我怎样才能使两个类别下的帖子保持独立,而不干扰一个类别的分页。My custom post type with a taxonomy 14kgoldsilver

The section where I select the category for each post

     /*Custom post type 14K Gold and Silver*/
   function my_custom_post_14kgs() {
       $labels = array(
    \'name\'               => _x( \'14k Gold & Silver\', \'post type general name\' ),
    \'singular_name\'      => _x( \'14k Gold & Silver\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add New\', \'book\' ),
    \'add_new_item\'       => __( \'Add New Item\' ),
    \'edit_item\'          => __( \'Modify Item\' ),
    \'new_item\'           => __( \'New Item\' ),
    \'all_items\'          => __( \'All Items\' ),
    \'view_item\'          => __( \'View Item\' ),
    \'search_items\'       => __( \'Search Items\' ),
    \'not_found\'          => __( \'No Products found\' ),
    \'not_found_in_trash\' => __( \'No products found in trash\' ),
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'14k Gold & Silver\'
);
$args = array(
    \'labels\'        => $labels,
    \'description\'   => \'\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
    \'has_archive\'   => true,
    \'hierarchical\'  => true,
    \'rewrite\'       => array(\'slug\' => \'14k-gold-silver/%14kgscollection%\',\'with_front\' => false),
    \'query_var\'     => true,
    //\'rewrite\'     => true,
    //\'publicly_queryable\' => false,
);
register_post_type( \'14kgs\', $args );
   }
   add_action( \'init\', \'my_custom_post_14kgs\' );

   function my_taxonomies_product_14kgs() {
$labels = array(
    \'name\'              => _x( \'14kgscollection\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'14kgscollection\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search Product Categories\' ),
    \'all_items\'         => __( \'All Product Categories\' ),
    \'parent_item\'       => __( \'Parent Product Category\' ),
    \'parent_item_colon\' => __( \'Parent Product Category:\' ),
           \'edit_item\'         => __( \'Edit Product Category\' ),
           \'update_item\'       => __( \'Update Product Category\' ),
           \'add_new_item\'      => __( \'Add New Product Category\' ),
           \'new_item_name\'     => __( \'New Product Category\' ),
           \'menu_name\'         => __( \'14kgscollection\' ),
       );
       $args = array(
                  \'labels\' => $labels,
                  \'hierarchical\'    => true,
                  \'public\'      => true,
                  \'query_var\'       => \'14kgscollection\',

                  \'rewrite\'     =>  array(\'slug\' => \'14k-gold-silver\' ),
                  \'_builtin\'        => false,
       );
       register_taxonomy( \'14kgscollection\', \'14kgs\', $args );
   }
   add_action( \'init\', \'my_taxonomies_product_14kgs\', 0 );


   /*Filter permalink structure*/
   add_filter(\'post_link\', \'collection14kgs_permalink\', 1, 3);
   add_filter(\'post_type_link\', \'collection14kgs_permalink\', 1, 3);

   function collection14kgs_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, \'%14kgscollection%\') === FALSE) return $permalink;
           // Get post
           $post = get_post($post_id);
            if (!$post) return $permalink;

            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, \'14kgscollection\');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
                $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = \'no-collection\';

        return str_replace(\'%14kgscollection%\', $taxonomy_slug, $permalink);
    }

2 个回复
SO网友:Brad Dalton

检查前一个和第四个参数的第三个和第四个参数next_post_link:

in_same_term(布尔)(可选)指示下一篇文章是否必须与当前文章在相同的分类术语内。如果设置为“true”,则仅显示当前分类术语中的帖子。如果帖子同时位于父类和子类中,或多个术语中,则下一个帖子链接将指向这些术语中的下一个帖子。truefalseDefault:false

excluded_terms(字符串/数组)(可选)数组或逗号分隔的数字术语ID列表,其中不应列出下一篇文章。例如,数组(1,5)或“1,5”。此参数用于接受由“and”分隔的ID列表,WordPress 3.3中不推荐使用此参数

默认值:无

此外,您可能会发现创建自定义分类法类型比创建类别更好地用于CPT。

add_action( \'init\', \'video_type_taxonomy\' );
function video_type_taxonomy() {

    register_taxonomy( \'video-type\', \'video\',
        array(
            \'labels\' => array(
                \'name\'          => _x( \'Types\', \'taxonomy general name\', \'executive\' ),
                \'add_new_item\'  => __( \'Add New Video Type\', \'$text_domain\' ),
                \'new_item_name\' => __( \'New Video Type\', \'$text_domain\' ),
            ),
            \'exclude_from_search\' => true,
            \'has_archive\'         => true,
            \'hierarchical\'        => true,
            \'rewrite\'             => array( \'slug\' => \'video-type\', \'with_front\' => false ),
            \'show_ui\'             => true,
            \'show_tagcloud\'       => false,
        )
    );

}

SO网友:Disha

get_next_post ( )get_previous_post ( ) 帮助我得到了我想要的东西。

谢谢你的时间

结束

相关推荐

Custom taxonomy pagination

我正在为自定义分类法存档页创建自定义编号分页。因此,我在显示分页、显示正确的链接等方面没有问题。但是,当我单击第2页上方的链接时(例如第3页,如:my-website.com/page/3/?my_category=some-term我得到404-页面不存在。该类别包含60多篇帖子,应该有8页,但只有第一页和第二页正确显示my-website.com/?my_category=some-term和my-website.com/page/2/?my_category=some-term但以上所有的都是404