I'm at a loss. On my single{custom-post-type}.php I want to create a loop that first shows the title of the page and the content. But then it also generates a list with all the content from a tag that had the same name as the title of the page.
Can somebody help me on the way?
You need an instance of WP_Query to query the posts inside the taxonomy term with the same name as the title. This is done by adding the tax_query
field to the arguements.
<?php
// WordPress header
get_header();
the_post();
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1, // -1 retrieves all the posts the query finds.
'tax_query' => array(
array(
'taxonomy' => 'category', // This is the default 'post' taxonomy, if you are using a custom taxonomy then you need to change this.
'field' => 'name', // Use name as you want to match the title
'terms' => get_the_title(), // Get the title of the current post as a string.
)
),
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()): $query->the_post(); // Loop through the posts from the term with same name as current post title. ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile;
wp_reset_postdata(); // Reset usage of 'the_post()'
endif;
<?php get_footer();