2012-12-27 12 views
8

मैं वर्तमान में इस का उपयोग कर रहा सभी श्रेणियों सूची:सूची सभी श्रेणियों लेकिन केवल पोते लंगर डाले है (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 

मेरे समस्या यह है कि मैं केवल महान पोते एंकर करना चाहते हैं। मैं नहीं चाहता कि माता-पिता, बच्चे या दादाजी एंकर हों।

किसी भी सुझाव की सराहना की जाएगी।

+0

आप नहीं माता पिता, बच्चे और पोता बिल्कुल ही दिखाई करना चाहते हैं:

ऊपर वर्ग wp_list_categories की तरह फोन करके इस्तेमाल किया जा सकता? या सिर्फ उन्हें नहीं चाहते हैं? आपको वर्तमान आइटम के पूर्वजों पर चेक करने वाली सूची प्रस्तुत करने के लिए शायद एक कस्टम वॉकर का उपयोग करने की आवश्यकता होगी (यदि इसमें 3 है - इसे दिखाएं, अन्यथा नहीं)। वैकल्पिक रूप से यदि आप सभी को एक साथ लिंक छुपा रहे हैं तो आप सीएसएस का उपयोग कर सकते हैं। इससे मदद मिलेगी http://scribu.net/wordpress/extending-the-category-walker.html – Lleo

उत्तर

0

इस कोड

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

इस कोड को केवल दो स्तर वर्ग के लिए परीक्षण किया जाता है का उपयोग करें।

अपनी श्रेणी आईडी के साथ "ग्रैंडचिल्ड" केटोग्री आईडी के साथ 9 बदलें। इस कोड का परीक्षण नहीं किया है, लेकिन यह आप

1

के लिए यह उपयोगी काम

आशा है आप कर सकते थे सब पर किसी भी php कोड को बदले बिना बस अक्षम लिंक शुद्ध सीएसएस का उपयोग कर। निम्न कोड की जाँच करें, यह माउस कर्सर बदल जाएगा, जोड़ने समारोह, अक्षम और रेखांकन शैली छिपाने:

.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 */ 
} 

परिणाम बिल्कुल के रूप में आप की आवश्यकता हो जाएगा, केवल पोते श्रेणी टिका होना प्रतीत होता है।

आशा है कि आपके सवाल का

1

टिप्पणी में लिएओ होम्स द्वारा उल्लेख किया है जवाब, सबसे अच्छा तरीका इस कार्यक्षमता को लागू करने के लिए एक कस्टम Walker Class तैयार करना है। मैंने इसे एक चाकू ले लिया, और निम्नलिखित के साथ आया था: ताकि हम parent::start_el() फोन करता है, तो यह उचित गहराई पर है कड़ी निर्माण करने के लिए कर सकते हैं

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 वर्ग फैली हुई है। कन्स्ट्रक्टर गहराई की एक सरणी स्वीकार करता है जिसमें वे स्तर शामिल होते हैं जिन्हें आप लिंक प्रदर्शित करना चाहते हैं। उस सरणी के बाहर आने वाली कोई भी गहराई सादे-पाठ के रूप में प्रस्तुत की जाएगी। ध्यान दें कि else कोड Walker_Category::start_el से लिया गया था, इसलिए बेस क्लास कभी भी संशोधित होने पर यह भविष्य की रिलीज में टूट सकता है।

<?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> 
संबंधित मुद्दे