Php Wordpress-仅显示当前类别下的标记

Php Wordpress-仅显示当前类别下的标记,php,wordpress,tags,Php,Wordpress,Tags,查看在Wordpress中的Archieve.php文件上显示标记,以仅显示当前类别下的标记 目前,我所能做的就是显示所有标签,而不是仅选择当前类别下的标签,代码如下: <ul id="blog-tags"> <?php $tags = get_tags(); if ( $tags ) { foreach ( $tags as $tag ) { echo '<li>'; if ( (int) $tag->term_id === get_que

查看在Wordpress中的Archieve.php文件上显示标记,以仅显示当前类别下的标记

目前,我所能做的就是显示所有标签,而不是仅选择当前类别下的标签,代码如下:

<ul id="blog-tags">
<?php
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
    echo '<li>';

    if ( (int) $tag->term_id === get_queried_object_id() )
        echo "<b>$tag->name</b>";
    else
        printf(
            '<a href="%1$s">%2$s</a>',
            get_tag_link( $tag->term_id ),
            $tag->name
        );

    echo '</li>';
}
}
?>
</ul>   

是否可以操纵上面的代码来做我想做的事情?或者我必须采取完全不同的方法

不完全确定这是您想要的,但基本上只需要按类别循环浏览所有帖子,然后获取这些帖子的标签

您可以尝试这样的方法来获取当前类别的所有标记。您需要对其进行一些操作,以便按照您希望的方式使用特定的
HTML
将其格式化

<?php
$custom_query = new WP_Query( 
array( 
    'cat' => get_query_var('cat') 
  ) 
);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags[] = $tag->term_id;
        }
    }
endwhile;
endif;

$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$args = array(
'smallest'  => 12,
'largest'   => 12,
'unit'      => 'px',
'number'    => 0,
'format'    => 'list',
'include'   => $tags_str
);
wp_tag_cloud($args);
// or use  <?php $my_tags = wp_tag_cloud( 'format=array' ); ?> to have them in an array that you can format after
?>
将它们放在一个数组中,之后可以对其进行格式化
?>

不完全确定这是您想要的,但基本上只需要按类别循环浏览所有帖子,然后获取这些帖子的标签

您可以尝试这样的方法来获取当前类别的所有标记。您需要对其进行一些操作,以便按照您希望的方式使用特定的
HTML
将其格式化

<?php
$custom_query = new WP_Query( 
array( 
    'cat' => get_query_var('cat') 
  ) 
);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags[] = $tag->term_id;
        }
    }
endwhile;
endif;

$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$args = array(
'smallest'  => 12,
'largest'   => 12,
'unit'      => 'px',
'number'    => 0,
'format'    => 'list',
'include'   => $tags_str
);
wp_tag_cloud($args);
// or use  <?php $my_tags = wp_tag_cloud( 'format=array' ); ?> to have them in an array that you can format after
?>
将它们放在一个数组中,之后可以对其进行格式化
?>

您在这里有两个帐户吗?你不是在这里发了这个问题吗:?你在这里有两个账户吗?您不是在这里发布了这个问题吗:?我建议在
if
之前将
$all_tags
数组声明为
NULL
,并选中
!在
endif
之后为空($all_tags)
,以防止PHP错误。我建议在
if
之前将
$all_tags
数组声明为
null
,并选中
!在
endif
之后为空($all_标签)
,以防止PHP错误。