Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Custom taxonomy template

  • I have created a custom taxonomy with Advanced Custom Fields, called glossary, which has terms and a description for each (the definition of the term), so the display URL will be domain.com/glossary/term.

    I am not sure either this is the right way to do what I want or should I change the terms from taxonomies to posts, in order to achieve the following.

    1. How can a I create a template for the taxonomy archive; that is: domain.com/glossary, that shows all terms created, even those that doesn’t have any post related?
    * Can I do it with Block Elements or should I create a page to display manually all terms?
    * Is there a way to automate the above so in case of adding a new term, this will be added to the list/archive mentioned?

    2. How can I create a Loop template for posts related to all terms, so when I go to one term (domain.com/glossary/term) I can see related posts to that term?
    * I have created an element Loop Template block, with display rule Post Archive Term -> All Terms.
    * First container has a headline with dynamic data for Title + a dynamic content for Term Description.
    * Second container has a query loop with Inherit query from template ON.
    * I also tried setting the query loop for posts with Term taxonomy -> current terms of the post.

    No matter first or second scenario for the query loop, posts are not being shown.

  • Hi Ro,

    You can just create a static page.

    1. To query all terms of a taxonomy, you can use a shortcode. For instance, a shortcode like this – [get-all-categories-shortcode] – would return all categories with this PHP code:

    function get_all_categories() {
        $categories = get_categories();
    	if ($categories) {
    		echo '<table class="category-list"><tr><td><h3>Titulo</h3></td></tr>';
    		foreach($categories as $category) {
    			echo '<tr><td><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></td></tr>';
    		}
    		echo '</table>';  
    	}
    }
    add_shortcode( 'get-all-categories-shortcode', 'get_all_categories' );

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    What terms are you trying to retrieve?

    2. You can use a GB Query Loop Block on your static page. Reference: https://docs.generateblocks.com/article/query-loop-overview/

  • Hey there,

    I am following up with this issue.

    1. Thanks for the help with the shortcode, it works, and now I only need to improve the design of the table and rest of elements.

    2. I believe I get misunderstood on this. Each term of the glossary has its own definition. This is a custom taxonomy, which the users can select when creating/editing a post.

    It should work as any taxonomy archive. Whenever you go to domain.com/glossary/term it should show the title of the term, its definition and posts associated with that term. But the latter are not showing up.

    In terms of templates, I have
    * A page hero block element for the title + definition of a term, on a Display_rule = Post Archive Term = All terms
    * A loop template block element with inherit query from template setup for the related posts of that term; that is: for the custom taxonomy archive, on a Display_rule = Post Archive Term = All terms

    Moreover:
    3. I’m experiencing a similar issue with other taxonomies archive, like custom taxonomy Type and default Tags (labeled as Topics). I have created a loop template block element as a global posts archive, with Display_rule either on “All archive”, “Post Archive Tag = All tags” and “Post Archive Type = All types”, and it just not displaying the posts on the archive pages.

    What am I doing wrong here?

  • To clarify, are you wanting a page showing all taxonomy terms or a page showing all posts of a custom taxonomy?

  • I need both of them:

    1. A (single) page with a list of all custom taxonomy terms (domain.com/glossary). I don’t mind on creating it manually, as terms won’t probably change a lot in the future.

    2. An archive showing all post of a custom taxonomy term (domain.com/glossary/term).

    The second is priority. The first one I can do it manually, in the end.

  • I see. Could you open a new topic for 2?

    Let’s address #1 here. Could you share the link to where you added the shortcode? What kind of design changes do you need?

  • For #1, I’m looking forward to doing something like this test page.

    Regarding #2, I have created a new topic here.

  • Hi again,

    I believe I managed to what I needed. I used the following PHP code:

    function get_all_sexionario_terms() {
        $taxonomy = 'sexionario';
        $terms = get_terms(array(
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        ));
    
        if ($terms && !is_wp_error($terms)) {
            echo '<div class="term-container">';
            echo '<h1 class="title-sexionario">Sexionario</h1>';
    
    		echo '<div class="term-grid">';
            $column_count = 4;
            $current_column = 1;
    
            foreach ($terms as $term) {
                if ($current_column == 1) {
                    echo '<div class="term-column">';
                }
    
                echo '<div class="term"><a class="term-link" href="' . get_term_link($term->term_id, $taxonomy) . '">' . $term->name . '</a></div>';
    
                if ($current_column == $column_count) {
                    echo '</div>';
                    $current_column = 1;
                } else {
                    $current_column++;
                }
            }
    
            // Close any open column div
            if ($current_column != 1) {
                echo '</div>';
    			echo '</div>';
            }
    
            echo '</div>';
        }
    }
    add_shortcode('get_all_sexionario_terms_shortcode', 'get_all_sexionario_terms');

    And the CSS from below:

    .term-container {
    	padding: 40px;
    	border-radius: 25px;
    	background-color: var(--morado);
    }
    
    .title-sexionario {
    	color: var(--amarillo);
    }
    
    .term-grid {
    	display: flex;
    	flex-wrap: wrap;
    }
    
    .term-column {
    	width: 33%;
    }
    
    .term-link {
    	color: var(--blanco);
    }
    
    .term-link:hover {
    	color: var(--amarillo);
    }
    
    @media only screen and (max-width: 1023px) {
        .term-grid {
            display: flex;
            flex-wrap: wrap;
        }
    
        .term-column {
            width: 50%; /* 2 columns on tablets */
        }
    }
    
    @media only screen and (max-width: 767px) {
        .term-column {
            width: 100%; /* 1 column on mobile devices */
        }
    }

    I’m not sure if this way is the right path. For example, terms are not ordered alphabetically, nor distributed in the same amount on all columns. How could I make the code from above cleaner?

    The only issue I am encountering is that when I edit the page, it throws this error: ‘The update has failed. The response is not a valid JSON response’.

    Do you what this error means?

  • The PHP code can be simplified as this:

    function get_all_sexionario_terms() {
        $taxonomy = 'sexionario';
        $terms = get_terms(array(
            'taxonomy' =>  $taxonomy,
            'hide_empty' => false,
        ));
    
        if ($terms && !is_wp_error($terms)) {
            echo '<div class="term-container">';
            echo '<h1 class="title-sexionario">Sexionario</h1>';
    
    		echo '<div class="term-grid">';
            
            foreach ($terms as $term) {
                               echo '<div class="term term-'.$term->slug.'"><a class="term-link" href="' . get_term_link($term->term_id, $taxonomy) . '">' . $term->name . '</a></div>';
          }
          echo '</div></div></div>';
        }
    }
    add_shortcode('get_all_sexionario_terms_shortcode', 'get_all_sexionario_terms');

    As the HTML is simpler now, you can adjust the CSS according to it.

  • Thank you so much. Now with the CSS adjusted, seems to be working well, except for 1 thing:

    When I click on each term, the title displays only for those that have related posts. For the ones that don’t have any post related, the title is not showing up.

    Also, I wonder What does the rule `term-‘.$term->slug.’ do?

  • Also, I wonder What does the rule `term-‘.$term->slug.’ do?

    Just to add a specific class for each term, in case you want to style them differently in the future.

    But it’s not in the plan, feel free to remove it.

    When I click on each term, the title displays only for those that have related posts. For the ones that don’t have any post related, the title is not showing up.

    What title? I don’t think the code has anything to do with the actual term page.

  • Great, thanks for the clarification. I’ll leave it in case need some tweaks in the future.

    > What title? I don’t think the code has anything to do with the actual term page.

    You’re right. I will check it apart.

    Thanks for your help.

  • You are welcome   🙂

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