显示与自定义 post wordpress 标题相同的标签

Show tags that are the same as the title of the custom post wordpress

我很茫然。在我的单曲{custom-post-type}.php 上,我想创建一个首先显示页面标题和内容的循环。但随后它还会生成一个列表,其中包含与页面标题同名的标签中的所有内容。

路上有人可以帮我吗?

您需要一个 WP_Query 的实例来查询分类术语中与标题同名的帖子。这是通过将 tax_query 字段添加到参数中来完成的。

<?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();