WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type tags/categories archive page (40 posts)

  1. Leads
    Member
    Posted 3 years ago #

    Hi.

    I've been playing with custom post types, and whilst initially I'm impressed with them, I have a few issues.

    I have converted some posts to custom type, and have have given them a category and some tags. This display fine and have hyperlinks. However, when I click the hyperlink, I get the archive page of the tag, but only posts from that tag (or category), not from my custom post type.

    What do I need to do?

  2. parandroid
    Member
    Posted 3 years ago #

    Put code below to your functions.php

    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','cpt'); // replace cpt to your custom post type
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    Hope it will work for you.

  3. Khaled Hakim
    Member
    Posted 3 years ago #

    That is the same problem I was facing. I was using WordPress 3.0 built in widgets to add a category listing of categories I had created. The thing was that the categories I had created were for a Custom Post (content) Type that I had created myself using Custom Post Type UI plug-in that is so handy. The problem was that when I used the Category listing widget... although the listing of categories did show up... clicking on any single on of them took to me a 404 page on my own blog.

    I took the code provided by parandroid and pasted in my functions.php file:

    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','<strong>cpt</strong><strong>'); // replace cpt to your custom post type
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    In the code above there is a variable "cpt". I simply replaced this variable, as instructed by parandroid, with the name of the Custom Post Type I had created (which in my case was "Advertisements") and voila... things worked perfectly.

    Thanks for sharing. I am just posting this comment as I noticed no one had posted anything that would point to the succcessful implementation of the above.

    If you have any questions or want any help doing any of the above mentioned, I'd be more than glad to help.

    Khaled Hakim

  4. ecken
    Member
    Posted 3 years ago #

    This a great! Exactly what I needed. Now the issue I'm having is when I paste the filter in my functions.php, it conflicts with register_nav_menus function, basically it completely stripes out the code when viewing tag index pages. Very strange. Any ideas how to solve this? The search continues!

    if ( function_exists( 'register_nav_menus' ) ) {
      	register_nav_menus(
      		array(
      		  'foot_menu' => 'My Custom Footer Menu',
      		  'sidebar_menu' => 'Super Sidebar Menu'
      		)
      	);
    }

    The above is my register nav function, and I'm using the filter code from paradroids post. Thank you.

  5. zsiswick
    Member
    Posted 3 years ago #

    @parandroid - I tried your code and it worked like a charm. Thanks for sharing!

  6. marcycapron
    Member
    Posted 3 years ago #

    just want to add in for anyone who sees this, if you replace 'cpt' with a custom post type, then keep adding... like for the site I'm working on, 'news', 'listings') etc you can indefinitely draw that list out. just wanted to point it out for those of us working with (way too many?) multiple custom post types.

  7. bluekachina
    Member
    Posted 3 years ago #

    Same problem as "ecken" described.
    After pasting the code provided here, it resolved my problem (tag pages now display CPT records too), however, when viewing that resultant list of CPT records, the page that gets displayed is missing my WP3 menu.

    Has anybody else been able to resolve this problem?

    PS --> I pasted that code at the bottom of my functions.php file.

  8. Zane Matthew
    Member
    Posted 3 years ago #

    I wanted to say this worked for me also, this is exactly what i was looking for, i did modify it and add is_post to the conditional since in my case i am displaying custom post types and posts within the same page, at least for now.

  9. Designophobia
    Member
    Posted 3 years ago #

    I'm having the same problem that "ecken" described. When using this code, my custom menu in the primary navigation block disappears.

    Anyone have a workaround?

  10. Zane Matthew
    Member
    Posted 3 years ago #

    Do you have a development site set-up? Since your dealing with a php file it may be a good idea to turn on debugging via your wp-config.php file.

    That may help trouble shoot.

  11. Designophobia
    Member
    Posted 3 years ago #

    Thanks for the help.

    When WP E-Commerce plugin is activated I get alot of errors. When it is deactivated, not one error. It does not change the nav display.

    Any other suggestions?

  12. Zane Matthew
    Member
    Posted 3 years ago #

    hmm, yeah mainly look for errors/notices coming from functions.php.

    try stepping thru the function with either die or echo, let me know the line right before it fails, again try this on a development site.

  13. Designophobia
    Member
    Posted 3 years ago #

    I think I found the solution. I Googled "pre_get_posts register_nav_menus" and one of the results was this.

    So I added the nav_menu_item to the post_type array like this:

    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','articles','nav_menu_item');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    This appears to fix the problem. The custom nav is now showing up on the tag and category pages.

    Is this the proper way to fix it or is there a better way?

  14. ryansigg
    Member
    Posted 3 years ago #

    This most recent solution seems to have fixed the nav problem.

    Thanks!

  15. ryansigg
    Member
    Posted 3 years ago #

    Even better: here's a non-hacky fix for the nav-bar issue:
    Change this line:
    if(is_category() || is_tag()) {
    -to-
    if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    Now you can take the nav_menu_item out of the post-type array.

    Hope that helps!
    Fix provided by Justin Tadlock: http://justintadlock.com/

  16. user2934
    Member
    Posted 3 years ago #

    I have multiple custom post types (news,library,faqs). I show recent posts for all these cpt's on all pages of site including category and tag pages.

    Using this function does what I want by adding a custom post type to basic category and tag pages/feeds.

    However It also breaks all my recent custom post type displays on these pages and forces all the recent posts to be from the custom post typed defined within the function.

    Any ideas on how to fix this so that this function only affects the main loop and not any other mini loops i have set up to display other cpt's?

    thank you for any help you can provide.

  17. keithdevon
    Member
    Posted 3 years ago #

    Big thanks to the clever folk on this thread. I had this exact problem, including the nav issue. Now solved.

    Thanks.

  18. Zane Matthew
    Member
    Posted 3 years ago #

    Are you using clean urls? if so turn them off, use the nasty ones for now, test this out. see if it works. if it works then the issue is in the url and "routing" if not the issue is else where.

  19. Walton
    Member
    Posted 3 years ago #

    Used parandroid's fix. Worked great but got the same nav-bar issue as everyone else.

    Tried Ryansigg's solution and now my menu is just a vertical list. I.e. No css customizing of it at all.

  20. Walton
    Member
    Posted 3 years ago #

    Never mind. Seems to have been a one time glitch. Sorry about that. Everything looks beautiful.

  21. Zane Matthew
    Member
    Posted 3 years ago #

    If your like me your using multiple CPT's and may run into other "issues", this is what I added to sovle mine:

    $post_types = get_post_types();

    Doing a print_r on this returns the following:

    Array
    (
        [0] => post
        [1] => page
        [2] => attachment
        [3] => revision
        [4] => nav_menu_item
        [5] => article
    )

    As you can see it __should__ basically cover everything, it maybe slow if your using multiple CPT's or there may be unwanted effects.

    Here is the updated code:

    $post_types = get_post_types();
        if ( is_category() || is_tag()) {
    
            $post_type = get_query_var('article');
    
            if ( $post_type )
                $post_type = $post_type;
            else
                $post_type = $post_types;
    
            $query->set('post_type', $post_type);
    
        return $query;
        }
    }
  22. beR-
    Member
    Posted 3 years ago #

    ok, my pb is close to be the same :
    I would like to share same categories referentiel between my posts and those who belong to my custom post type (named 'Portolio')
    Actually, when i've registered the 'Portfolio' type, i've associated it to a "new" category referentiel (ie. different from the classic post category)
    My question is : i want the same categories referentiel than the classic post type.
    Have you got an idea ?
    Thanks for your reply!

  23. beR-
    Member
    Posted 3 years ago #

    ok i've found the solution : you have to use the function
    register_taxonomy_for_object_type('category', 'Portfolio');
    with category, a taxonomy ever registered for posts.

  24. Zane Matthew
    Member
    Posted 3 years ago #

    That's exactly how I was doing it and it worked for me.

  25. johnfrancisco
    Member
    Posted 3 years ago #

    Same Problem and this code works just great. (parandroid code)
    Better than "Custom Post Type Viewer" plugin wich do the stuff but also mix Posts and Custom Post Type Posts in the homepage.

    Just want to say thanks for that.

  26. kerssies
    Member
    Posted 3 years ago #

    I used the code as described above but now my menus are gone. Does anyone have a clue how to get them back?

    With menu: joinforkids.org
    Menu gone: joinforkids.org/category/voorbeeldprojecten

    Thanks!

  27. nihilistar
    Member
    Posted 2 years ago #

    I used the code paranoid provided and also ryansigg's fix, but now i have another problem when displaying the posts from a category. I use a function that displays the thumbnail of every post, but with this code the thumbnail disappears. Anyone any ideas ?
    Thanks

  28. Zane Matthew
    Member
    Posted 2 years ago #

    Can you paste this function?

  29. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    If it is over 10 lines long, please use the pastebin.

  30. nihilistar
    Member
    Posted 2 years ago #

    function imaginii1($postID) {
    
       $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $postID );
    
       if($arrImages) {
               // Get array keys representing attached image numbers
               $arrKeys = array_keys($arrImages);
               $sThumbUrl = wp_get_attachment_thumb_url($arrKeys[0]);
    
               // Build the <img> string
               $sImgString = '<a href="' . wp_get_attachment_url($arrKeys[0]) . '">' .
                       '<img src="' . $sThumbUrl . '" alt="" title="" />' .
                       '</a>';
    
               echo $sImgString;
        }
    }

    [Please post code snippets between backticks or use the code button.]

Topic Closed

This topic has been closed to new replies.

About this Topic