[Resolved] Why doesn’t any of my taxonomies show up?

Home Forums Support [Resolved] Why doesn’t any of my taxonomies show up?

Home Forums Support Why doesn’t any of my taxonomies show up?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2341315
    Sebastian

    Hey guys,

    I have a custom post type with 5 custom taxonomies. The taxonomies are reached through 4 menu items. When a menu item is clicked, **only** the content belonging to that taxonomy must show, except for the last one which I call “Others”. This taxonomy contains several subcategories (more will be added as time goes) and it must show contents from **all** subcategories.

    However, each one of these subcats must also be available through the menu. See table below

    Others

    – Subtax 1
    – Subtax 2
    – and so on…

    This is my problem:

    I want the taxonomy and its description to <u>always</u> show, no matter if tax is empty or not. Now only taxonomies that actually have content shows up, but it doesn’t show any actual posts. The ones that doesn’t contain any content yet only show the default generate_do_template_part('none'), and no taxonomy description.

    For this I’m using a customized GP archive.php template file (taxonomy-name.php). Also inside this file i’m calling get_template_part('template-parts/content', 'cpt_name-3grid') which is a custom template with cards inside a grid.

    My code:

    <?php
    /**
    
     * The template for displaying Archive pages.
       *
     * @package GeneratePress
       */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly.
    }
    
    get_header(); 
    
    $terms = get_terms(array(
        'taxonomy'      =>  'radgivartyper',
        'hide_empty'    =>  false
      ));
    
      ?>
    
        <div <?php generate_do_attr( 'content' ); ?>>
            <main <?php generate_do_attr( 'main' ); ?>>
                <?php
                /**
                 * generate_before_main_content hook.
                 *
                 * @since 0.1
                 */
                do_action( 'generate_before_main_content' );
    
    ​            if ( generate_has_default_loop() ) {
    ​                if ( have_posts() ) :
    
    ​                 /*
    		* generate_archive_title hook.
    		* @since 0.1
    		* @hooked generate_archive_title - 10
    		*/
    
                   		do_action( 'generate_archive_title' );
    
    ​                 /**
    		* generate_before_loop hook.
    		* @since 3.1.0
    		*/
    
              ​       do_action( 'generate_before_loop', 'archive' ); ?>
    
                        <div class="itc-row gutter">
      
      			<?php
      									
      			foreach($terms as $term) {
    
                          $args = array(
                            'tax_query'     =>  array(
                              array(
                                'taxonomy'  =>  'radgivartyper',
                                'field'     =>  'slug',
                                'terms'     =>  $term->slug,
    ​                                    )
    ​                                )
    ​                            );
                          
                          $agent_cat_posts = new WP_Query($args);
                          
                          while ( $agent_cat_posts->have_posts() ) :
                          $agent_cat_posts->the_post();
                          
                          get_template_part( 'template-parts/content', 'radgivare-3grid' );
                          
                          endwhile;
                          wp_reset_postdata(); }
    ​                        
    ​                    ?>
    ​                    </div>
    ​                    <?php
      
      			/**
    			* generate_after_loop hook.
    			* @since 2.3
    			*/
      
      			do_action( 'generate_after_loop', 'archive' );
      
      			else :
      
      			generate_do_template_part( 'none' );
      						
      			endif;
    
                            /* End of foreach */
    			}
    
    			/**
    			* generate_after_main_content hook.
    			* @since 0.1
    			*/
    
    			do_action( 'generate_after_main_content' );
    			?>
          </main>
    </div>
    
    <?php
    
    /**
    * generate_after_primary_content_area hook.
    * @since 2.0
    */
    
    do_action( 'generate_after_primary_content_area' );
    generate_construct_sidebars();
    get_footer();

    As you can see I’m (trying to) harnessing the power of get_terms() but obviously I’m doing something wrong since it’s producing no results, except for showing the description when there are posts in that taxonomy.

    Phuh! That’s a lot, sorry about that. I really need assistance her though. Thank you in advance.

    #2341464
    David
    Staff
    Customer Support

    Hi there,

    where can i see one of the problem archives ?

    #2341507
    Sebastian

    See private info area

    #2341517
    Sebastian

    Or rather this

    #2341535
    David
    Staff
    Customer Support

    I am not sure i have an answer for this as it falls into custom development.
    But my approach would be to use the default template hierarchy to set the query for you:

    https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/#taxonomy-template-hierarchy

    For example if you were to

    1. just take a copy of the GP archive.php
    2. rename it to: taxonomy-{taxonomy}.php
    2.1 changing the {taxonomy} for the name of your taxonomy.

    eg. taxonomy-radgivartyper.php

    Then you should see the various taxonomy archives for each of the taxonomy terms you have set.

    If that works then the archive you want to show several sub categories in you can:

    1. Create another copy of the archive.php
    2. rename this to: taxonomy-{taxonomy}-{parent-term}.php
    2.1 changing the {taxonomy} for the name of your taxonomy.
    2.2 changing the {parent-term} for the parent term of your taxonomy.

    eg. taxonomy-radgivartyper-parentterm.php

    That should show all posts that are in the parent term and its children.

    Could you try that? If that works – Then we can look at the content template changes and keeping the description always visisble

    #2342656
    Sebastian

    Hi David,

    Thank you, your example code works like a charm! I’m not surprised though 😉

    So now maybe we could find a solution to the empty categories with the dreaded “Nothing found” text then?

    Cheers!

    #2342704
    David
    Staff
    Customer Support

    In your copies of the archive.php look for this line:

    https://github.com/tomusborne/generatepress/blob/ae37efd2d134e7f7c5cef1db06a9a3841a6ed95c/archive.php#L60

    delete that line and replace it with:

    do_action( 'generate_archive_title' );

    So in this instance instead of it loading the no-results.php – it will hook in the archive title and description.

    #2342711
    Sebastian

    Well, I’m speechless. Things are so easy when you know what to do 😂

    Like always… big thank you David! You’re my hero 🍺

    #2342717
    David
    Staff
    Customer Support

    I am just super glad to hear it works 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.