programing

모든 범주 나열(손자만 고정됨)(Wordpress)

golfzon 2023. 4. 4. 22:40
반응형

모든 범주 나열(손자만 고정됨)(Wordpress)

현재 모든 카테고리를 나열하는 데 사용하고 있습니다.

<?php 
    $args = array (
    'menu' => 'secondary-navigation',
    'sort_column' => 'menu_order',
    'container' => 'false', 
    'title_li' => 0,
    'hide_empty' => 0
    );
    wp_list_categories($args);
?>

계층 내 모든 카테고리와 각 항목이 고정되는 목록만 표시됩니다.

제 카테고리는 실제로 다음과 같이 설정되어 있습니다.

Parent
    -> Child
        -> Grandchild
            -> Great Grandchild

문제는 증손자들한테만 앵커가 있었으면 좋겠다는 거야부모, 자식, 손자가 앵커를 두는 것을 원하지 않는다.

어떤 제안이라도 해주시면 감사하겠습니다.

php 코드를 전혀 변경하지 않고 순수 CSS를 사용하여 링크를 비활성화할 수 있습니다.다음 코드를 체크하면 마우스 커서가 변경되고 링크 기능이 비활성화되며 밑줄 스타일이 숨겨집니다.

.cat-item a, .cat-item .cat-item .cat-item .cat-item a {
      cursor: default;
      pointer-events: none;
      text-decoration: none;
}
.cat-item .cat-item .cat-item a {
      cursor: pointer;
      pointer-events: auto;
      text-decoration: underline;
      /* also add here any special style for grandchildren categories */
}

결과는 정확히 필요한 대로 됩니다.손자 카테고리만 앵커되어 있습니다.

그것이 당신의 질문에 답하기를 바랍니다.

Lleo Holmes가 코멘트에서 언급했듯이, 이 기능을 구현하기 위해 커스텀 워커 클래스를 만드는 것이 가장 좋은 방법입니다.나는 그것을 시도해 보고, 다음과 같이 생각해 냈다.

class Depth_Category_Walker extends Walker_Category {
    private $allowed_depths;

    function __construct( $depths_to_link = array() ) {
        $this->allowed_depths = !is_array($depths_to_link) ? array($depths_to_link) : $depths_to_link;
    }

    function start_el( &$output, $category, $current_depth = 0, $args = array(), $id = 0 ) {
        extract($args);
        if( in_array( $current_depth, $this->allowed_depths ) ) {
            parent::start_el( $output, $category, $current_depth, $args, $id );
        } else {
            $cat_name = esc_attr( $category->name );
            $cat_name = apply_filters( 'list_cats', $cat_name, $category );
            if ( !empty($show_count) )
                $cat_name .= ' (' . intval($category->count) . ')';

            if ( 'list' == $args['style'] ) {
                $output .= "\t<li";
                $class = 'cat-item cat-item-' . $category->term_id;
                if ( !empty($current_category) ) {
                    $_current_category = get_term( $current_category, $category->taxonomy );
                    if ( $category->term_id == $current_category )
                        $class .=  ' current-cat';
                    elseif ( $category->term_id == $_current_category->parent )
                        $class .=  ' current-cat-parent';
                }
                $output .=  ' class="' . $class . '"';
                $output .= ">$cat_name\n";
            } else {
                $output .= "\t$cat_name<br />\n";
            }
        }
    }
}

이것에 의해,Walker_Category전화할 수 있도록parent::start_el()적절한 깊이에 있으면 링크를 생성할 수 있습니다.생성자는 링크를 표시할 수준을 포함하는 깊이 배열을 허용합니다.배열을 벗어나는 모든 깊이는 일반 텍스트로 렌더링됩니다.주의:else코드가 에서 추출되었습니다.Walker_Category::start_el이 때문에, 베이스 클래스가 변경되었을 경우, 향후의 릴리스에서는 이 문제가 발생할 가능성이 있습니다.

위의 클래스는 호출로 사용할 수 있습니다.wp_list_categories예를 들어 다음과 같습니다.

<?php
   $args = array(
       'orderby' => 'menu_order',
       'title_li' => 0,
       'hide_empty' => 0,
       'walker' => new Depth_Category_Walker(2)
   );
?>
<ul>
   <?php wp_list_categories( $args ); ?>
</ul>

이 코드를 사용하세요.

$categories = get_categories(array(
        'child_of' => 9,
        'hide_empty' => false
    ));
foreach($categories as $cat){
    echo $cat->name;
    echo '<br />';
}

이 코드는 2레벨 카테고리에 대해서만 테스트됩니다.

9를 "Handchild" cateogry ID와 같이 카테고리 ID로 바꿉니다.이 코드는 테스트되지 않았지만 작업입니다.

이것이 당신에게 도움이 되길 바랍니다.

언급URL : https://stackoverflow.com/questions/14048753/list-all-categories-but-only-have-grandchildren-anchored-wordpress

반응형