WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Zane Matthew
    Member
    Posted 3 years ago #

    I'd do a print_r or var_dump on $arrImages and start there

  2. nihilistar
    Member
    Posted 3 years ago #

    $arrImages is empty when the script is activated but I don't know why, because it's working fine when the script is turned off. I think it's something wrong with that link used to get all the post details and something has to be changed to work with the custom posts.

  3. nihilistar
    Member
    Posted 3 years ago #

    Solved. Show have added 'attachment' to

    $post_type = array('post','cpt');

  4. Zane Matthew
    Member
    Posted 3 years ago #

    cool, you might want to do a var_dump on:

    $post_types = get_post_types();

    As the "fix" is overriding the natural behavior or WordPress, therefore any CPT that is added will not work, nor will any other post types unless you add them back in.

    You can also do:

    $all_post_types = get_post_types();
    $post_type = array( $all_post_types );

    This way your taking all registered post types (default one's and custom one's) and adding it back into your filter, then again, this might not be what you want to do.

  5. Marcus
    NetWebLogic Support
    Posted 3 years ago #

    76blueberries answer worked for me:

    register_taxonomy_for_object_type('category', 'your_post_type');

    The rest still broke my menu if there were sub-menus or labels as submenu headers.

  6. karlclement
    Member
    Posted 3 years ago #

    Thanks a lot Designophobia and Ryansigg! But I suggest making a minor change to the syntax.

    Replace this line:
    if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    With this line:
    if ( is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {

    Here is the full function to be posted in your functions.php:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
        $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;
        }
    }

    In the array:
    $post_type = array('post','articles','nav_menu_item');

    Replace "articles" with your custom post type name. You can add as many post types as you want in the array.

    This snippet of code, allows you to have all post types show in categories (archive page), home page and tag page.

    Enjoy,

  7. M@rc
    Member
    Posted 3 years ago #

    I have a problem with adding 'nav_menu_item' to the $post_type array, because when I do, every menu item I add to my custom menu shows up as a new post in my "Recent posts" widget on the category page (as a blank item linking to a broken page). When I leave out 'nav_menu_item', that problem is gone but then my custom menu won't show up. What to do?

  8. Rico Michel
    Member
    Posted 3 years ago #

    This code seems work good. $query->set seems like the way to go.

    Now, How do you display a special archive page for your custom posts? Mine aren’t categorized, and the archive shows the blog archive, as if there wasn’t a request.

    I’d need a way to catch the custom post type in an archive page to show a special header. Something like "if is_custom('name') echo $specialheader".

    Geez, now i’m a real wordpress geek… welcome, eh ;-)

  9. Triple P
    Member
    Posted 3 years ago #

    You could create a page template, modify the query, and use this code to display your special header image on the archive page and single posts:

    <?php if (is_singular('my-post-type') || is_page('name-of-the-page')) { ?>
    //your header image
    <?php } ?>
  10. Rico Michel
    Member
    Posted 3 years ago #

    Tanks for your answer, but I think I found a better one. WordPress will look for an archive page for custom post types; but you have to tell it it's there. First, when you initialize the custom type, there's an has_archive option that needs to be 'true'. Then, you create an archive page named "archive-"[customtypename].

    Then you put whatever you want in this page, like a special header followed by a call to loop-archive, for example.

    See ya people!
    Rico

Topic Closed

This topic has been closed to new replies.

About this Topic