WordPress.org

Ready to get started?Download WordPress

Forums

Twenty Eleven custom post types won't display on home (6 posts)

  1. adminRimma
    Member
    Posted 2 years ago #

    Hi, I have a custom post type "review" and cannot get the posts to display on the main page. I want the other post types (aside, gallery, etc.) to display as well.

    There is no "loop.php" file - where do I add the code to get the custom post type to display?
    Thanks

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Do not edit the Twenty Eleven theme. It is the default WordPress 3.2 theme and having access to an unedited version of the theme is vital when dealing with a range of site issues.

    Create a child theme for your customisations. Once your child theme is active, we should be able to help with the customisation.

  3. adminRimma
    Member
    Posted 2 years ago #

    Thanks esmi for your reply! I do have a child theme set up. I have added the following line & it works. Is this OK? Just to make sure.

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    
    	if ( is_home() && false == $query->query_vars['suppress_filters'] )
    		$query->set( 'post_type', array( 'post', 'review' ) );
    
    	return $query;
    }
  4. esmi
    Forum Moderator
    Posted 2 years ago #

    It seems OK but not sure why you're checking for suppress_filters.

  5. adminRimma
    Member
    Posted 2 years ago #

    After much head-scratching, I have used the following code to display my custom post-type 'review' on my index/home page AND on my category archive pages. The problem is the 'menu_nav_item' addition causes additional "posts' to show on my index/home page that were created when I created my menu! I am very confused. Please help! I have incuded my code in my child functions file:

    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','review','nav_menu_item');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }
  6. adminRimma
    Member
    Posted 2 years ago #

    <article id="post-249" class="post-249 nav_menu_item type-nav_menu_item status-publish hentry">

    The above is the the post that is showing up since it is of type nav_menu_item (there are additional ones as well, since I have multiple nav menu items. If I remove the 'nav_menu_item' from the array, my nav menu does not display on category or tag archive pages.

    Should I just exlude this post-type from the loop? If so, how do I do that? Thanks in advance!!!

Topic Closed

This topic has been closed to new replies.

About this Topic