I have two main taxonomies: 1. Libros 2. Asuntos
and multiple taxonomy terms: 1. Al Bujari 2. Muslim 3. La Paciencia 4. La Fe 5. La Caridad and many other taxonomy terms using the custom post type: hadices.
The case is that when I click on the slug related to any of the taxonomy terms above, it displays all the posts not just the posts related to the taxonomy term I clicked. I am working on the taxonomy.php file. Here is the code I have on the taxonomy.php:
<?php $taxes = array( 'libro_de_hadiz' , 'asunto');
$terms = get_terms( $taxes );
echo $terms;
$paged = ( get_query_var( 'paged' ) ) ? absint ( get_query_var( 'paged' ) ) : 1;
$args = array( 'post_type' => 'hadices',
'posts_per_page' => 6,
'paged' => $paged,
));
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php endif; ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( $loop->have_posts() ) : ?>
<?php
/* Start the Loop */
while ( $loop->have_posts() ) : $loop->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
$loop->the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' .$loop-> __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . $loop->__( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . $loop->__( 'Page', 'twentyseventeen' ) . ' </span>',
) );
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '/paged/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif; wp_reset_postdata(); ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
This is the code for my custom post and taxonomies:
function cptui_register_my_cpts() {
/**
* Post Type: Hadices.
*/
$labels = array(
"name" => __( "Hadices", "twentyseventeen" ),
"singular_name" => __( "Hadiz", "twentyseventeen" ),
);
$args = array(
"label" => __( "Hadices", "twentyseventeen" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "hadiz", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor" ),
"taxonomies" => array( "libro_de_hadiz", "asunto" ),
);
register_post_type( "hadices", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_taxes() {
/**
* Taxonomy: Libros de Hadiz.
*/
$labels = array(
"name" => __( "Libros de Hadiz", "twentyseventeen" ),
"singular_name" => __( "Libro", "twentyseventeen" ),
);
$args = array(
"label" => __( "Libros de Hadiz", "twentyseventeen" ),
"labels" => $labels,
"public" => true,
"hierarchical" => false,
"label" => "Libros de Hadiz",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'libro_de_hadiz', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => false,
);
register_taxonomy( "libro_de_hadiz", array( "hadices" ), $args );
register_taxonomy( "asunto", array( "hadices" ), array(
"name" => __( "Asuntos", "twentyseventeen" ),
"singular_name" => __( "Asunto", "twentyseventeen" ),
"label" => __( "Asuntos", "twentyseventeen" ),
"public" => true,
"hierarchical" => false,
"label" => "Asuntos",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'asunto', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => false,
) );
}
add_action( 'init', 'cptui_register_my_taxes' );
I have seen the answers to the post in this link: Archive template for taxonomy terms
However the answers were just saying to add a taxonomy-term.php for each term, which will be very tedious in my case as I would need tens of files with similar code just changing the term slug in the tax_query. Is there a way to add a dynamic variable for the terms in tax_query to show only the posts related to the term or any other solution if any?
Thanks in advance