Php 如何在WordPress中通过所选标记显示帖子?

Php 如何在WordPress中通过所选标记显示帖子?,php,wordpress,Php,Wordpress,是否可以通过选定的标签显示帖子?我试着在谷歌上做一些研究,但我没能做到。我已经面对了这个问题,我被困住了 我已经创建了一个新的WordPress帖子 前 我和千层面将其标记为甜食、奶油食品 面条和标签=辛辣食物、甜食 在页面上,我想显示甜食标签,它将显示面条和拉扎尼亚。这可能吗?是的,这是可能的 function get_tags_post($tag_name) { $original_query = $wp_query; $wp_query = null; $brand

是否可以通过选定的标签显示帖子?我试着在谷歌上做一些研究,但我没能做到。我已经面对了这个问题,我被困住了

我已经创建了一个新的WordPress帖子

我和千层面将其标记为甜食、奶油食品
面条和标签=辛辣食物、甜食

在页面上,我想显示甜食标签,它将显示面条和拉扎尼亚。这可能吗?

是的,这是可能的

function get_tags_post($tag_name)
{
    $original_query = $wp_query;
    $wp_query = null;
    $brand_name= $tag_name;
    $args=array('posts_per_page'=>5, 'tag' => $brand_name);
    $wp_query = new WP_Query( $args );
    if ( have_posts() ) :
        while (have_posts()) : the_post();
            echo '<li>';
            the_title();
            echo '</li>';
        endwhile;
    endif;
    $wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();
}
函数get\u tags\u post($tag\u name)
{
$original\U query=$wp\U query;
$wp_query=null;
$brand\u name=$tag\u name;
$args=array('posts\u per\u page'=>5,'tag'=>$brand\u name);
$wp\u query=新的wp\u查询($args);
如果(have_posts()):
while(have_posts()):the_post();
回音“
  • ”; _title(); 回音“
  • ”; 结束时; endif; $wp_query=null; $wp\u query=$original\u query; wp_reset_postdata(); }
    在变量$brand_name中传递标记名,您可以获得与标记相关的帖子

    如果你想得到标签列表,你可以通过下面的代码来实现

    <?php
    $posttags = get_the_tags(); // gives you the list of all tags 
    if ($posttags) {
      foreach($posttags as $tag) {
        echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg" 
    alt="' . $tag->name . '" />'; 
      get_tags_post($tag->name); // pass the dynamic names to the function and it will return the post of the particular tag 
      }
    }
    ?>
    

    顺便问一下放在哪里$原始查询=$wp\u查询$wp_query=null$brand_name=“您的标签”$args=array('posts\u per\u page'=>5,'tag'=>$brand\u name)$wp_query=新的wp_查询($args);if(have_posts()):while(have_posts()):the_post();回音“
  • ”;_title();回音“
  • ”;结束时;endif$wp_query=null$wp_query=$original_query;wp_reset_postdata();而且。。。它显示所有的标签。。我想。。特定标签only@pentashape对于特定标记,需要在$brand_name变量中传递标记名。$original_查询用于保存$wp_查询的实例,然后重新分配。谢谢:D。。我现在明白了如果我想用这个品牌的名字?为什么不能