sourcetip

특정 카테고리에서 만든 태그 목록 - word press

fileupload 2023. 10. 5. 23:28
반응형

특정 카테고리에서 만든 태그 목록 - word press

이 기능은 워드프레스에 내장되어 있습니까?코덱스 안에서 아무것도 보지 못했습니다.

codex.wordpress.org/Function_Reference/wp_tag_cloud

카테고리별 페이지가 몇 개 있는데 해당 게시물과 관련된 모든 태그를 보여주고 싶습니다.

제가 이걸 찾긴 했는데 이게 제대로 된 건지 아니면 더 좋은 방법이 존재하는 건지 (소스) (오래된 방법!!!!):

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

UPDATE(간소화):

특정 카테고리의 태그 목록을 만들려면 이 코드가 훨씬 좋습니다(카테고리 이름만 변경).

:: 루프 오류로 인해 최근에 다시 업데이트::

    <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>

어려운 경우에도 해결책이 있을 수 있으니, 새로운 해결책이 나오면 업데이트 부탁드립니다.

당신이 찾은 방법만이 당신이 원하는 것을 이룰 수 있는 유일한 방법이라고 생각합니다.대사를 수정할 수도 있겠지만, 컨셉은 맞습니다.

현재로서는 핵심 워드프레스 기능을 사용하는 것처럼 태그를 필터링하는 방법은 없다고 생각합니다.

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

여기에 훨씬 더 쉬운 예가 있습니다.카테고리 이름만 바꾸면 됩니다.연결된 태그는 목록 형식으로 인쇄됩니다.

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

?>

우선 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>';
}

언급URL : https://stackoverflow.com/questions/5321373/tag-list-made-from-specific-category-wordpress

반응형