WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add category to a custom post type? (25 posts)

  1. PhillShaw
    Member
    Posted 1 year ago #

    Hi Guys,
    I have been reading other posts for hours but cannot find an answer.

    Basically I have a 'review' theme that is a custom post type, I cannot specify any categories for this post type that I create in posts>categories, I want to be able to use this function.

    The reason for this is that I have a slider on the homepage that will only display posts from standard categories that I define in posts>categories, I want the most recent reviews to be able to be shown in the slider by having a category called 'Featured' (for example.

    Is this possible? at first I thought custom fields may work by using something like 'category - "Horror"' but I dont even have access to custom fields from within the custom post type editor (see screenshot).

    Screenshot - http://www.phillshaw.co.uk/babylongeek/wp-content/uploads/2012/07/Screen-Shot-2012-07-20-at-19.21.24.jpg

    I have thought about plugins but i'm not really sure what I am supposed to be doing. I have also contacted the developer but haven't had a reply, yet.

    The theme is 'Valor' - http://www.web2feel.com/valor/
    The review post type DOES have a custom category selector called consoles but the slider cannot use these.

    Any thoughts would be amazing!

  2. stvwlf
    Member
    Posted 1 year ago #

    You can add the regular post category taxonomy to a custom post type in its register_post_type statement, sample below:

    register_post_type('discography',
        array(
        'labels' => array(
                 //your label stuff
                 ),
    
      'taxonomies' => array('category'),       <========= add this....
    
      'public' => true,
      'show_ui' => true,
      'exclude_from_search' => true,
      'hierarchical' => true,
      'supports' => array( 'title', 'editor', 'thumbnail' ),
      'query_var' => true
            )
      );
  3. PhillShaw
    Member
    Posted 1 year ago #

    where would I add that?
    in the single-review.php (which is the cutom post type) my code is:

    // Review Post type
    
    add_action('init', 'review_register');
    
    function review_register() {
    
    	$labels = array(
    		'name' => _x('Reviews', 'post type general name'),
    		'singular_name' => _x('Review', 'post type singular name'),
    		'add_new' => _x('Add New', 'review'),
    		'add_new_item' => __('Add New Review'),
    		'edit_item' => __('Edit Review'),
    		'new_item' => __('New Review'),
    		'view_item' => __('View Review'),
    		'search_items' => __('Search Review'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => null,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail')
    	  ); 
    
    	register_post_type( 'review' , $args );
    }
    
    // Custom Taxonomy
    
    function add_console_taxonomies() {
    
    	register_taxonomy('console', 'review', array(
    		// Hierarchical taxonomy (like categories)
    		'hierarchical' => true,
    		// This array of options controls the labels displayed in the WordPress Admin UI
    		'labels' => array(
    			'name' => _x( 'Review Category', 'taxonomy general name' ),
    			'singular_name' => _x( 'Review-Category', 'taxonomy singular name' ),
    			'search_items' =>  __( 'Search Review-Categories' ),
    			'all_items' => __( 'All Review-Categories' ),
    			'parent_item' => __( 'Parent Review-Category' ),
    			'parent_item_colon' => __( 'Parent Review-Category:' ),
    			'edit_item' => __( 'Edit Review-Category' ),
    			'update_item' => __( 'Update Review-Category' ),
    			'add_new_item' => __( 'Add New Review-Category' ),
    			'new_item_name' => __( 'New Review-Category Name' ),
    			'menu_name' => __( 'Review Categories' ),
    		),
    
    		// Control the slugs used for this taxonomy
    		'rewrite' => array(
    			'slug' => 'console', // This controls the base slug that will display before each term
    			'with_front' => false, // Don't display the category base before "/locations/"
    			'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    		),
    	));
    }
    add_action( 'init', 'add_console_taxonomies', 0 );
    
    ?>
  4. stvwlf
    Member
    Posted 1 year ago #

    see code below

    $args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => null,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail'),
                    'taxonomies' => array('category')
    	  );
  5. PhillShaw
    Member
    Posted 1 year ago #

    ok I made the changes and:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /websites/123reg/LinuxPackage21/ph/il/ls/phillshaw.co.uk/public_html/babylongeek/wp-content/themes/Valor/cpt.php on line 35

  6. stvwlf
    Member
    Posted 1 year ago #

    My eyes are not noticing any typo in what I posted.

    Your original post said the code you posted was from file single-review.php

    The error message says the error is in theme file cpt.php

    Not sure why changing something in single-review.php would cause a syntax error in cpt.php.

    Does the error go away when you revert the $args code back to what it was before my suggestion?

  7. PhillShaw
    Member
    Posted 1 year ago #

    Sorry my mistake in the first post, its not single-review.php its cpt.php where the code you suggested lives.

    Yes when I take out
    'taxonomies' => array('category')
    the site returns to normal.

  8. stvwlf
    Member
    Posted 1 year ago #

    try this instead.
    in cpt.php after
    register_post_type( 'review' , $args );

    add
    register_taxonomy_for_object_type( 'category', 'review' );

  9. Sanjiv
    Member
    Posted 1 year ago #

    Hi. PhillShaw.

    This code might help you. Have a look.

    function themes_taxonomy() {
    	register_taxonomy(
    		'themes_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
    		'themes',   		 //post type name
    		array(
    			'hierarchical' 		=> true,
    			'label' 			=> 'Themes store',  //Display name
    			'query_var' 		=> true,
    			'rewrite'			=> array(
    					'slug' 			=> 'themes', // This controls the base slug that will display before each term
    					'with_front' 	=> false // Don't display the category base before
    					)
    			)
    		);
    }
    add_action( 'init', 'themes_taxonomy');'
    
    /**
     * Maintain the permalink structure for custom taxonomy
     * Display custom taxonomy term name before post related to that term
     * @uses post_type_filter hook
     */
    'function filter_post_type_link( $link, $post) {
        if ( $post->post_type != 'themes' )
            return $link;
    
        if ( $cats = get_the_terms( $post->ID, 'themes_categories' ) )
            $link = str_replace( '%themes_categories%', array_pop($cats)->slug, $link );
        return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);'
    
    //Registering Custom Post Type Themes
    'add_action( 'init', 'register_themepost', 20 );
    function register_themepost() {
        $labels = array(
    		'name' => _x( 'Themes', 'catchthemes_custom_post','catchthemes' ),
    		'singular_name' => _x( 'Theme', 'catchthemes_custom_post', 'catchthemes' ),
    		'add_new' => _x( 'Add New', 'catchthemes_custom_post', 'catchthemes' ),
    		'add_new_item' => _x( 'Add New ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'edit_item' => _x( 'Edit ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'new_item' => _x( 'New ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'view_item' => _x( 'View ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'search_items' => _x( 'Search ThemePosts', 'catchthemes_custom_post', 'catchthemes' ),
    		'not_found' => _x( 'No ThemePosts found', 'catchthemes_custom_post', 'catchthemes' ),
    		'not_found_in_trash' => _x( 'No ThemePosts found in Trash', 'catchthemes_custom_post', 'catchthemes' ),
    		'parent_item_colon' => _x( 'Parent ThemePost:', 'catchthemes_custom_post', 'catchthemes' ),
    		'menu_name' => _x( 'Themes Posts', 'catchthemes_custom_post', 'catchthemes' ),
        );
    
        $args = array(
    		'labels' => $labels,
    		'hierarchical' => false,
    		'description' => 'Custom Theme Posts',
    		'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
    		'taxonomies' => array( 'post_tag','themes_categories'),
    		'show_ui' => true,
    		'show_in_menu' => true,
    		'menu_position' => 5,
    		'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
    		'show_in_nav_menus' => true,
    		'publicly_queryable' => true,
    		'exclude_from_search' => false,
    		'query_var' => true,
    		'can_export' => true,
    		'rewrite' => array('slug' => 'themes/%themes_categories%','with_front' => FALSE),
    		'public' => true,
    		'has_archive' => 'themes',
    		'capability_type' => 'post'
        );
    	register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces
    }

    [moderated: please ensure that your code is enclosed in backticks (`) or use the code button.]

  10. PhillShaw
    Member
    Posted 1 year ago #

    at stvwlf, That solved it! Much appreciated for your efforts!!

    Thanks guys.

  11. PhillShaw
    Member
    Posted 1 year ago #

    I spoke too soon!
    I now have access to the categories from within custom post type but on the frontend of the site it doesnt look like they're being filtered through, I have added 'Movie News' as a category within my review (custom post type) but when I click movie news on my site it isnt there??

  12. stvwlf
    Member
    Posted 1 year ago #

    I suspect a normal WordPress category link is only going to display posts of post type 'posts'. You probably need to filter the category list code for it to include posts from other post types.

    A quick search brought up this
    http://stackoverflow.com/questions/5008327/custom-post-types-on-category-pages
    See if it helps or gets you started

  13. PhillShaw
    Member
    Posted 1 year ago #

    Ok thanks for that, it makes sense in that I need to call those categories but the code makes no sense to me!

    Any ideas on how to make this function for my situation? (sorry to keep leeching your time!)

  14. stvwlf
    Member
    Posted 1 year ago #

    but when I click movie news on my site it isnt there??

    Please post the relevant PHP code that is displaying the category links that are not including the CPT's. If its more than ten lines, post it in a pastebin http://pastebin.com/ and link the pastebin URL here.

    I have to see how the category links are being created (the underlying PHP code) to give suggestions on how to include custom post types in the cat pages.

  15. PhillShaw
    Member
    Posted 1 year ago #

    ok thanks,

    here is the full cpt.php code:
    http://pastebin.com/Fmr3x9F8

  16. stvwlf
    Member
    Posted 1 year ago #

    Hi

    That is the Custom post type code. (We already have the CPT and Taxonomy working correctly)

    I need to see the code that displays the Movie News link - what is the URL of the page that has the Movie News Link on it? The one that when clicked doesn't display the CPT Reviews posts. It is the code that displays that link that I need to see.
    thanks

  17. PhillShaw
    Member
    Posted 1 year ago #

    Ok, my mistake,

    This code is from single.php which is where we right normal blog posts in the backend, these are showing categories properly.

    (the website is http://www.phillshaw.co.uk/babylongeek/)

    http://pastebin.com/nGTYrSJT

  18. stvwlf
    Member
    Posted 1 year ago #

    Hi

    The file that displays the category pages in your theme is archive.php

    You don't need to change archive.php. I found this code on the support forum. Add it to your theme's functions.php file.
    I set it up for the 'review' CPT. If you want more CPT's than review added, add them to this line $post_type = array('post','review'); in the code block below:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','review');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }
  19. PhillShaw
    Member
    Posted 1 year ago #

    Thanks for the help, I now get this error at the top of my site:

    I have implemented it, but I am still not seeing the results being passed to the archive pages. I have a review that tagged that should show in the "News" archive.

    Can you explain what

    $post_type = array('post','review'); relates to in cpt.php so I understand how each links?

    thanks.

  20. PhillShaw
    Member
    Posted 1 year ago #

    aside from the above issue I am also having issues with showing that an author/editor/contributor (etc) has created any custom post type when you click the author name.

    At present when you click the author name it only shows posts from regular post types. I imagine thats because my 'review' section doesn't have the author box in available in the screen options. Can I make this happen?

    see screenshot

  21. stvwlf
    Member
    Posted 1 year ago #

    The error message you reported is not an error message. It is the code itself that you pasted into the functions.php file. That means the code is not being executed, it is instead displaying on the screen. Most likely this means it is not included within a <?php ?> block

    Can you explain what
    $post_type = array('post','review'); relates to in cpt.php so I understand how each links?

    That line should be in the code I just had you add to functions.php. That is not supposed to be added to cpt.php

    What it does is tell WordPress that when displaying a category page, to include not only "post" post types as WordPress normally does but also to include "review" post types. When you get it working, as you want other posts types added to the category page you insert their post type names in the array on that same line.

    The issue with author pages only displaying regular posts is the same issue as on the category pages. By default WordPress author, archive, tag, category pages only display 'post' post types. To make them display other posts types requires the code I posted above. It will need to be modified to work with post types other than review and on more pages than just the_category() and the_tag(), such as the_author()

    I'm sorry, but I've reached the limit of the amount of assistance I can offer you on this topic on the forum. We resolved the original issue of your request, adding the category taxonomy to your custom post type. The rest of what we are discussing are additional issues that have come up since resolving your original request.

    If you wish to add another forum post, the question to ask is "How can I add custom post types to the posts displayed on a Category page?". The code I posted is the main part of the solution. You need assistance with implementing it.

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','review');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    It goes in functions.php. When you see the code displayed on the screen instead of executed it means you pasted it in the wrong place.

  22. PhillShaw
    Member
    Posted 1 year ago #

    great thanks for your time in resolving many issues I had, i'll continue to investigate.

    Phil

  23. ngoodfellow
    Member
    Posted 1 year ago #

    I am trying to create a site that allows users to "like" a post and then review the posts that were "liked" is this the same functionality you are using? can you link me to your example site?

  24. athos99
    Member
    Posted 11 months ago #

    There is a plugin for solve your problem
    http://wordpress.org/extend/plugins/cat-post-type/

  25. benarobinson
    Member
    Posted 9 months ago #

    +1 to athos99 -- the plugin works and solves this issue painlessly.

Topic Closed

This topic has been closed to new replies.

About this Topic