CPT Category Display
-
Hello,
Due to multiple pages having the same name (About, About, About), we are trying to add the category to the end (About – Category1, About – Category 2, etc.).
When using the following shortcode, the category is not being recognized and displayed:
[wpp post_type='products'stats_views="1" order_by="views" stats_taxonomy="1" stats_category="1" post_html='<li> <a href="{url}">{text_title} - {category} </a>{taxonomy}</li>']We are using multiple CPT’s and would like this to work on all of them. How can we get the shortcode to recognize the CPT category (it seems as though it is only looking for the “category” category used for posts)?
-
Hi @jaredguiles,
I’m not sure I understand what you mean. If you can provide reproduction steps (other than just the shortcode) I can look into this in more detail.
-
This reply was modified 5 years, 10 months ago by
Hector Cabrera. Reason: Improved wording for clarity
-
This reply was modified 5 years, 10 months ago by
Hector Cabrera.
Hi Hector,
We have set up a custom post type called ‘products’ as well as a custom post category called ‘product_categories’.
When using
post_type='products' stats_category="1"in the shortcode as well aspost_html='<li> <a href="{url}">{text_title} - {category}</a></li>'it does not display the product category.If we use that same shortcode attributes in ‘posts’ to display the default post title and category, ‘category’, it works fine.
How can we display the ‘product’ title as well as the assigned category from ‘product_categories’?
Also please note that we are not using WooCommerce, this is not a store product but a custom post type called products.
Thanks!
Alright, a few more questions for you in that case:
- Are you using a plugin to register/manage your custom post type?
- If you’re not using a plugin, could you please share the code you used to register your custom post type (and taxonomies)?
- If you’re using a plugin, could you please name it and share the settings you’re using with it?
Remember, I’m looking to reproduce what you’re seeing over there in order to be able to help you out so I’ll need you to be as detailed as possible with this.
Thanks in advance!
Hi Hector,
We are not using a plugin. Below is the code used to create the CPT and category:
function register_cpts() { /** * Post Type: Product Pages */ $labels = array( "name" => __( "Products", "custom-post-type-ui" ), "singular_name" => __( "Product Page", "custom-post-type-ui" ), "menu_name" => __( "Products", "custom-post-type-ui" ), "all_items" => __( "All Product Pages", "custom-post-type-ui" ), "add_new_item" => __( "Add New Product Page", "custom-post-type-ui" ), "edit_item" => __( "Edit Product Page", "custom-post-type-ui" ), "new_item" => __( "New Product Page", "custom-post-type-ui" ), "view_item" => __( "View Product Page", "custom-post-type-ui" ), "view_items" => __( "View Product Pages", "custom-post-type-ui" ), "search_items" => __( "Search Product Pages", "custom-post-type-ui" ), "not_found" => __( "No product pages found", "custom-post-type-ui" ), "not_found_in_trash" => __( "No product pages found in Bin", "custom-post-type-ui" ), "parent_item_colon" => __( "Parent Product Page", "custom-post-type-ui" ), "featured_image" => __( "Featured image for this product page", "custom-post-type-ui" ), "set_featured_image" => __( "Set featured image for this product page", "custom-post-type-ui" ), "remove_featured_image" => __( "Remove featured image for this product page", "custom-post-type-ui" ), "use_featured_image" => __( "Use featured image for this product page", "custom-post-type-ui" ), "parent_item_colon" => __( "Parent Product Page", "custom-post-type-ui" ), ); $args = array( "label" => __( "Products", "custom-post-type-ui" ), "labels" => $labels, "description" => "Internal Product Pages", "public" => true, "publicly_queryable" => true, "show_ui" => true, "delete_with_user" => false, "show_in_rest" => true, "rest_base" => "", "rest_controller_class" => "WP_REST_Posts_Controller", "has_archive" => false, "show_in_menu" => true, "show_in_nav_menus" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => false, "rewrite" => array( "slug" => "products", "with_front" => true ), "query_var" => true, "menu_position" => '', "menu_icon" => "dashicons-store", "supports" => array( "title", "editor", "revisions", "author" ), ); register_post_type( "products", $args ); } add_action( 'init', 'register_cpts' );function register_taxes() { /** * Taxonomy: Product Categories */ $labels = array( "name" => __( "Product Categories", "custom-post-type-ui" ), "singular_name" => __( "Product Category", "custom-post-type-ui" ), "menu_name" => __( "Categories", "custom-post-type-ui" ), "all_items" => __( "All Categories", "custom-post-type-ui" ), "edit_item" => __( "Edit Category", "custom-post-type-ui" ), "view_item" => __( "View Category", "custom-post-type-ui" ), "update_item" => __( "Update Category", "custom-post-type-ui" ), "add_new_item" => __( "Add New Category", "custom-post-type-ui" ), "new_item_name" => __( "New Category Name", "custom-post-type-ui" ), "parent_item" => __( "Parent Category", "custom-post-type-ui" ), "parent_item_colon" => __( "Parent Category:", "custom-post-type-ui" ), "search_items" => __( "Search Categories", "custom-post-type-ui" ), "separate_items_with_commas" => __( "Separate Categories with commas", "custom-post-type-ui" ), "add_or_remove_items" => __( "Add or remove categories", "custom-post-type-ui" ), "not_found" => __( "No categories found", "custom-post-type-ui" ), "no_terms" => __( "No categories", "custom-post-type-ui" ), ); $args = array( "label" => __( "Product Categories", "custom-post-type-ui" ), "labels" => $labels, "public" => true, "publicly_queryable" => true, "hierarchical" => true, "show_ui" => true, "show_in_menu" => true, "show_in_nav_menus" => true, "query_var" => true, "rewrite" => array( 'slug' => 'product-categories', 'with_front' => true, ), "show_admin_column" => true, "show_in_rest" => true, "rest_base" => "product-categories", "rest_controller_class" => "WP_REST_Terms_Controller", "show_in_quick_edit" => true, ); register_taxonomy( "product-categories", array( "products" ), $args ); } add_action( 'init', 'register_taxes' );Thanks @jaredguiles, that helped.
Alright, so it’s not exactly obvious but you need another parameter:
taxonomy.The
taxonomyparameter sets the taxonomy (or taxonomies) WPP should look for when filtering/building the popular posts list. WPP defaults tocategorywhen thetaxonomyparameter isn’t set.In your case, WPP tries to fetch your products’ taxonomies using
categoryas taxonomy and fails to find any (because your CPT uses a custom taxonomy, not categories), hence the reason why you’re seeing a blank space where your CPT taxonomy should be.Here’s what your shortcode should look like now to display your custom taxonomy in the listing:
[wpp post_type='products' stats_views="1" order_by="views" taxonomy="product-categories" stats_taxonomy="1" post_html='<li> <a href="{url}">{text_title} - {taxonomy}</a></li>']I removed the
categoryrelated parameters/tags since your CPT doesn’t use it.If you have any other questions don’t hesitate to ask.
Hi Hector,
That worked, thank you so much for your help!
Cheers!
-
This reply was modified 5 years, 10 months ago by
The topic ‘CPT Category Display’ is closed to new replies.