0

I have created a custom post type named 14kgold. Under this I defined two categories: Symphony and Noir. Now I added items/ products to each category. When I open a product under one category i.e. symphony for example, It takes me to single.php. Till now everything is working fine. But when I do next, It shows me the next item of noir. How can I make the pagination specific to symphony only?

    /*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);
    }

Is there anything I can add to the above code which gives me a unique value which can help me in differentiating the products under two categories?

2
  • 1
    What are you using in single.php for the next/prev links?
    – BA_Webimax
    May 29 '15 at 12:37
  • I am using <?php next_posts_link(); ?> <?php previous_posts_link(); ?> for pagination
    – Disha
    May 29 '15 at 15:18
0

Since I'm not sure if you are using single.php for anything else, I am going to suggest that you copy single.php to single-14kgs.php in the theme directory.

Once you have done that, modify the following:

<?php next_posts_link(); ?>

becomes...

<?php next_post_link('%link','%title',TRUE) ?>

and

<?php previous_posts_link(); ?>

becomes...

<?php previous_post_link('%link', '%title', TRUE); ?>

The third argument to those functions tells WP to generate a link and stay in the same category.

For further reading:

https://codex.wordpress.org/Function_Reference/previous_post_link https://codex.wordpress.org/Function_Reference/next_post_link

4
  • Thank u BA_Webimax. I tried the above option but it doesnot show anything on the page.
    – Disha
    May 29 '15 at 18:22
  • Are you saying that the product does not appear or that the link is empty?
    – BA_Webimax
    Jun 1 '15 at 17:14
  • I meant when I use the code you wrote, I do not see any next or prev links for pagination.
    – Disha
    Jun 1 '15 at 17:56
  • I want the single.php file to show posts from symphony only. If i create single-14kgs.php, it will again show the posts under both symphony and noir. I have two categories under 14kgs "symphony" and "noir". How can i filter the posts for single category?
    – Disha
    Jun 5 '15 at 14:07
0

Found the solution.

I am using get_next_post() and get_previous_post() functions which works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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