0

I have created A custom post type as mentioned in my older question

When I try to use get_the_category() function to retrieve the category of the post(if it is symphony or noir). It returns "Array". How can I keep the posts under the two categories separate which do not interfere in the pagination of one category.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
  • 1
    get_the_category returns an array of categories, that's not unexpected. the documentation explains how to output them. I'm not sure what your question is otherwise though.
    – Milo
    Commented May 29, 2015 at 17:01
  • @Milo Thankx for your reply. My question is I create a product under my custom post type "14kgoldsilver", and assign the category "Symphony" to it. Now when I open this product using single.php on frontend, I have pagination which should take me to next product under the same category"symphony". But Instead the pagination takes me to all the products which are under symphony as well as noir(another category created under 14kgoldsilver). So I want to restrict the pagination to only one category on single.php
    – Disha
    Commented May 29, 2015 at 18:06

2 Answers 2

1

Check the 3rd and 4th parameters for previous and next_post_link:

in_same_term (boolean) (optional) Indicates whether next post must be within the same taxonomy term as the current post. If set to 'true', only posts from the current taxonomy term will be displayed. If the post is in both the parent and subcategory, or more than one term, the next post link will lead to the next post in any of those terms. true false Default: false

excluded_terms (string/array) (optional) Array or a comma-separated list of numeric terms IDs from which the next post should not be listed. For example array(1, 5) or '1,5'. This argument used to accept a list of IDs separated by 'and', this was deprecated in WordPress 3.3

Default: None

Also, you may find creating custom taxonomy types better for use with CPT's than categories.

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,
        )
    );

}
1
  • Thanx Brad for ur reply but I am using custom taxonomy only not the category thats already exists in wp. I have tried in_same_term but no luck and excluded_terms is not required as it has to be dynamic... Please help!!
    – Disha
    Commented May 30, 2015 at 11:50
0

get_next_post ( ) and get_previous_post ( ) helped in getting what I was looking for.

Thanx all for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.