2009-11-22 14 views
7

मैं वर्डप्रेस के लिए नया हूं और एक श्रेणी लूप बनाने की कोशिश कर अपने बालों को खींच रहा हूं। पाश माना जाता है: सभी श्रेणियों वर्डप्रेस श्रेणियों के माध्यम से लूपिंग

  • (
  • के लिए बाहर गूंज श्रेणी के नाम पर लिंक के साथ के माध्यम से

    1. पाश बाहर पोस्ट करने के लिए (उस श्रेणी में पिछले 5 पदों गूंज स्थायी लिंक के साथ)

    प्रत्येक के लिए एचटीएमएल होगा

    <div class="cat_wrap"> 
        <div class="cat_name"> 
         <a href="<?php get_category_link($category_id); ?>">Cat Name</a> 
        </div> 
        <ul class="cat_items"> 
         <li class="cat_item"> 
         <a href="permalink">cat item 1</a> 
         </li> 
         <li class="cat_item"> 
         <a href="permalink">cat item 2</a> 
         </li> 
         <li class="cat_item"> 
          <a href="permalink">cat item 3</a> 
         </li> 
         <li class="cat_item"> 
         <a href="permalink">cat item 4</a> 
         </li> 
         <li class="cat_item"> 
         <a href="permalink">cat item 5</a> 
         </li> 
        </ul> 
    </div> 
    

    कृपया मदद

  • +0

    क्या यह टेम्पलेट अनुभाग या अन्य फ़ाइलें है? – streetparade

    उत्तर

    6

    Hy रखते हुए चीज़ें यहाँ सरल आप इसे कैसे

    <?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?> 
    
    +1

    हां, wp_list_categories और सेटिंग्स-रीडिंग में, अपने "ब्लॉग पेज अधिकतर दिखाएं" को सेट करें। – Michael

    +0

    wp_list_categories() के साथ एकमात्र समस्या यह है कि आप किसी भी सभ्य सीमा पर इसका आउटपुट नियंत्रित नहीं कर सकते हैं। –

    8

    हल कर सकते हैं ओह, याद है कि आप 5 पदों

    <?php 
    //for each category, show 5 posts 
    $cat_args=array(
        'orderby' => 'name', 
        'order' => 'ASC' 
        ); 
    $categories=get_categories($cat_args); 
        foreach($categories as $category) { 
        $args=array(
         'showposts' => 5, 
         'category__in' => array($category->term_id), 
         'caller_get_posts'=>1 
        ); 
        $posts=get_posts($args); 
         if ($posts) { 
         echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> '; 
         foreach($posts as $post) { 
          setup_postdata($post); ?> 
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
          <?php 
         } // foreach($posts 
         } // if ($posts 
        } // foreach($categories 
    ?> 
    
    +0

    मैंने पाया कि इनलाइन 'वैश्विक $ पोस्ट;' शीर्ष पर इस पर जोड़ा गया है, [https://codex.wordpress.org/Function_Reference/setup_postdata ](https://codex.wordpress.org/Function_Reference/setup_postdata) – MrG

    +0

    @ एमआरजी जब तक आप लूप के भीतर इसका उपयोग नहीं कर रहे हैं, हाँ। –

    1

    मैं नेस्टेड लूप करने के लिए कोड के इस बिट बना दिया है चाहता था है श्रेणियाँ। साझा करना।

     //Start on the category of your choice  
         ShowCategories(0); 
    
         function ShowCategories($parent_category) { 
           $categories = get_categories(array('parent' => $parent_category, 'hide_empty' => 0)); 
           foreach ($categories as $category) { 
            ?><ul><li><?=$category->cat_name;?><? 
            ShowCategories($category->cat_ID); 
            ?></li></ul><? 
           } 
         } 
    
    0

    यह अन्य Stackoverflow धागा पर एक नज़र डालें:

    https://wordpress.stackexchange.com/questions/346/loop-through-custom-taxonomies-and-display-posts/233948#233948

    मैं एक जवाब है कि मैं उत्पादन में इस्तेमाल करते हैं और एक आकर्षण की तरह काम करता था।

    बस के बजाय केवल 5 पदों को प्रदर्शित करने के लिए तर्कों को समायोजित करना याद रखें।

    $args = array('showposts' => 5); 
    

    जोड़ें पाश में बहस के अपने वर्तमान सरणी के लिए 'showposts' => 5 कि प्रत्येक श्रेणी के पदों के माध्यम से दोहराता।

    संबंधित मुद्दे