- This topic has 8 replies, 2 voices, and was last updated 6 months, 2 weeks ago by
David.
-
AuthorPosts
-
September 13, 2022 at 1:16 am #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.
September 13, 2022 at 4:20 am #2341464David
StaffCustomer SupportHi there,
where can i see one of the problem archives ?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 13, 2022 at 5:17 am #2341507Sebastian
See private info area
September 13, 2022 at 5:26 am #2341517Sebastian
Or rather this
September 13, 2022 at 5:39 am #2341535David
StaffCustomer SupportI 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: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 thename
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
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 14, 2022 at 6:26 am #2342656Sebastian
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!
September 14, 2022 at 7:24 am #2342704David
StaffCustomer SupportIn your copies of the
archive.php
look for this line: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.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 14, 2022 at 7:28 am #2342711Sebastian
Well, I’m speechless. Things are so easy when you know what to do 😂
Like always… big thank you David! You’re my hero 🍺
September 14, 2022 at 7:38 am #2342717David
StaffCustomer SupportI am just super glad to hear it works 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.