WordPress.org

Ready to get started?Download WordPress

Forums

Adding custom post type to the loop - wp nav menu dissapears (7 posts)

  1. ivan.chaquea
    Member
    Posted 4 years ago #

    Hello, after some search i found this usefull code to add my custom post type post into my category display, it works as expected, since im using the default categories with the custom post types. but i have a problem with the wp_nav_menu on every page i target the result. on this example im using is_category() and when i enter any category page the menu dissapears.im using a fresh install with the twentyten template.

    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
            $post_types= get_post_types( array( '_builtin' => false ), 'objects' );
            $array = array('post');
            foreach($post_types as $type)
                    array_push($array, $type->name);
    
    	if ( is_category() )
    		$query->set( 'post_type', $array );
    
    	return $query;
    }

    i would apreciate any help with this issue, thanks

  2. ivan.chaquea
    Member
    Posted 4 years ago #

    edit: im using the default workdpress categories i created one category with a sub category, added some entries to the main category and then added my custom post type to the sub category. then i needed to display those subcategory items on my page. this is the updated code:

    // create post type: testimonios
    add_action('init', 'post_type_testimonios');
    add_filter( 'pre_get_posts', 'mostrar_testimonios'  );
    
    function post_type_testimonios() {
    
        register_post_type(
            'testimonios',
            array(
                'label' => __('Testimonios'),
                'description' => __('Ingresa un testimonio de  pacientes.'),
                'public' => true,
                 'show_ui' => true,
                   'capability_type' => 'post',
                   'hierarchical' => false,
                   'rewrite' => true,
                   'query_var' => true,
                'menu_position' => 15,
                'supports' => array (
                    'title',
                    'editor',
                    'thumbnail',
                    'custom-fields',
                    'categories'),
                    'taxonomies' => array('post_tag','category')
            )
        );
     register_taxonomy_for_object_type('category', 'external');
     register_taxonomy_for_object_type('post_tag', 'external');
    
    }
    function mostrar_testimonios( $query ) {
            if ( is_category()  )
                $query->set( 'post_type', array( 'post', 'testimonios' )  );
            return $query;
        }

    and it works as expected, the custom post type ( testimonios) is displaying well on its sub category. but, when i navigate on any category page, the wp_nav_menu() fails to load. its printing the cointainer div and the uls but none of the link items.

    if i change or add any other page type to the line if ( is_category() ), like is_home(), or is_single() the menu will stop working on those too.

    im also getting this error while navigating the categories panel on the backend

    Warning: Illegal offset type in isset or empty in /home/public_html/wp/wp-includes/post.php on line 736

    wish dissapears when i comment these lines :

    add_filter( 'pre_get_posts', 'mostrar_testimonios'  );
    
    function mostrar_testimonios( $query ) {
             if ( is_category()  )
                 $query->set( 'post_type', array( 'post', 'testimonios' )  );
             return $query;
         }

    im not sure what other function could i use to add these posts to the loop without needing to set more queries into it, or would that be the only choice for it?

  3. ivan.chaquea
    Member
    Posted 4 years ago #

    update: ading these 2 conditions the backend and post page problems are fixed

    add_filter ('pre_get_posts', 'mostrar_testimonios' );
    function mostrar_testimonios( $query ) {
    	    if ( is_category() ||  !is_admin()  && $query->is_category_page )
    	        $query->set( 'post_type', array( 'post', 'testimonios' ) );
    	    return $query;
    	}

    but the wp_nav_menu keeps dissapearing on the target page ( categories in my case)

  4. Richard-B
    Member
    Posted 4 years ago #

    Im having the same problem but on the homepage

  5. nickmorss
    Member
    Posted 4 years ago #

    anyone solve this issue.

    Ivans fix works, as in it allows the categories to work, but it still causes his error!

    really need to figure this problem out!?

  6. nickmorss
    Member
    Posted 4 years ago #

    found a fix for the lost nav.

    add this as a post type in the filter, as the Nav derives from this

    'nav_menu_item'

    now for the Warning: Illegal offset type in isset or empty in /home/public_html/wp/wp-includes/post.php on line 736

    apprently its an issue with 3.0 and has been fixed...

    http://wordpress.org/support/topic/warning-illegal-offset-type-in-isset-or-empty

  7. Ivaylo Draganov
    Member
    Posted 4 years ago #

    I was stuck with the same problem but just found an article with what so far turns out to be a non-conflicting way to filter the query:
    http://bajada.net/2010/06/08/how-to-include-custom-post-types-in-the-loop-in-your-wordpress-weblog

Topic Closed

This topic has been closed to new replies.

About this Topic