【问题标题】:tag list made from specific category - wordpress由特定类别制作的标签列表 - wordpress
【发布时间】:2011-03-16 05:20:13
【问题描述】:

这个功能是否内置在 wordpress 中?我没有在法典中看到任何内容。

codex.wordpress.org/Function_Reference/wp_tag_cloud

我有几个特定类别的页面,我想显示与这些帖子相关的所有标签。

我确实找到了这个,但我不确定它是否正确或是否存在更好的方法(source)(旧方法!!!):

<?php
    query_posts('category_name=html');
    if (have_posts()) : while (have_posts()) : the_post();
        $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
                $all_tags_arr[] = $tag -> name;
            }
        }
    endwhile; endif; 

    $tags_arr = array_unique($all_tags_arr);
?>
    <ul>
<?php
    foreach($tags_arr as $tag){
        echo '<li>'.$tag.'</li>';
    }
?>
</ul>
<?php wp_reset_query(); ?>

更新(简化):::

要制作来自特定类别的标签列表,此代码要好得多(只需更改类别名称):

::最近因为循环错误再次更新::

    <ul>
                <?php
                    query_posts('category_name=html');
                    if (have_posts()) : while (have_posts()) : the_post();

                        if( get_the_tag_list() ){
                            echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
                        }

                    endwhile; endif; 

                    wp_reset_query(); 
                ?>
</ul>

即使遇到困难,我也可能有解决方案,如果有新解决方案出现,请更新。

【问题讨论】:

  • 你如何整合这个例如在侧边栏中?您能否更具体地说明步骤、粘贴代码的位置等?谢谢!
  • 代码底部唯一依赖的是您输入的 category_name。您可以将其粘贴到任何您想要的位置。如果您希望它显示由 ONE 类别组成的标签列表,请将类别名称放在显示“html”的位置并将其粘贴到您想要的任何位置。
  • 这不会忽略重复项。每次出现在帖子中时,它都会显示标签。有谁知道忽略重复并仅显示唯一标签的方法?

标签: wordpress categories tag-cloud


【解决方案1】:

我认为您找到的方法是您实现所需目标的唯一方法。 也许你可以修改一些行,但概念是对的。

目前我认为没有一种方法可以像使用核心 wordpress 功能那样过滤标签。

【讨论】:

  • 这很有趣,因为在我寻找答案的过程中,人们早在 3 年前就一直在问这个问题(在 wordpress 论坛上)。不知道为什么他们还没有包括它。哦,好吧,这样就可以了
  • 也许只是因为有更紧迫的事情需要处理。如果用 8 行代码就可以实现,那就可以了。
  • 我设法用我找到的 wordpress 函数缩短了代码。它看起来好一点。我更新了问题
【解决方案2】:

我没有得到上面的代码来安装 WordPress。但是,我确实设法对其进行了调整,直到它起作用为止。这是我的调整:

$catid = get_cat_ID(single_cat_title("",false));
$catobj = get_category($catid);
$catslug = $catobj->slug;
$all_tags_arr = array();
query_posts('category_name='.$catslug);
if (have_posts()) : while (have_posts()) : the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags_arr[] = $tag -> term_id;
        }
    }
endwhile; endif; 

$tags_arr = array_unique($all_tags_arr);

$tagcloud_args = array(
    'include'   =>  implode(',',$tags_arr),
);

wp_tag_cloud( $tagcloud_args ); 
wp_reset_query();

【讨论】:

    【解决方案3】:

    这是一个更简单的示例.... 只需更改类别名称,然后您就完成了。关联的标签将以列表格式打印出来。

    <?php query_posts('category_name=html'); if (have_posts()) : while (have_posts()) : the_post();
    
        $posttags = get_the_tags();
    
        if ($posttags) {
            foreach($posttags as $tag) {
                $all_tags[] = $tag -> name;
            }
        }
        endwhile; endif; 
    
        //This snippet removes any duplicates.
        $tags_unique = array_unique($all_tags); 
    
        echo '<ul>';
            foreach($tags_unique as $unique) {
              echo  '<li>'.$unique.'</li>';
            }
        echo '</ul>';
    
        wp_reset_query();
    
    ?>
    

    【讨论】:

      【解决方案4】:

      首先,安装 ACF 插件并创建分类字段。在您要显示标签的位置添加以下代码后。

      $queriedObj = get_queried_object(); 
      $taxonomy = $queriedObj->taxonomy;
      $term_id = $queriedObj->term_id;  
      
      $current_tags = get_field('category_tags', $taxonomy . '_' . $term_id); //category_tags = ACF fieldname
      
      if ( $current_tags ) {
        echo '<ul>';
        foreach ( $current_tags as $term ) {
            echo '<li>';
            echo '<a href="/product-tag/' . $term->slug . '">';
            echo $term->name;
            echo '</a>';
            echo '</li>';
        }
        echo '</ul>';
      }
      else{
          echo '<ul>';
          echo '<li>No Tag.</li>';
          echo '</ul>';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多