2017-06-15 4 views
0

Grundsätzlich, wie der Titel sagt, möchte ich eine Liste von Kategorien und Unterkategorien bekommen und dann (mit Links zu ihnen) Beiträge für diese Kategorien/Unterkategorien veröffentlichen.Kategorien/Unterkategorien mit Posts anzeigen WP

Das ist die Struktur Ich versuche zu erreichen:

<ul> 
    <li>Category 1</li> 
    <ul> 
     <li>Subcategory 1 within category 1</li> 
     <ul> 
      <li>Post 1 within subcategory 1</li> 
      <li>Post 2 within subcategory 1</li> 
      <li>Post 3 within subcategory 1</li> 
     </ul> 
     <li>Subcategory 2 within category 1</li> 
     <ul> 
      <li>Post 1 within subcategory 2</li> 
      <li>Post 2 within subcategory 2</li> 
      <li>Post 3 within subcategory 2</li> 
     </ul> 
     <li>Subcategory 3 within category 1</li> 
     <ul> 
      <li>Post 1 within subcategory 3</li> 
      <li>Post 2 within subcategory 3</li> 
      <li>Post 3 within subcategory 3</li> 
     </ul> 
     <li>Posts that have no subcategory</li> 
     <ul> 
      <li>Post 1 with no subcategory</li> 
      <li>Post 2 with no subcategory</li> 
     </ul> 
    </ul> 
    <li>Category 2</li> 
    <ul> 
     <li>Subcategory 1 within category 2</li> 
     <ul> 
      <li>Post 1 within subcategory 1</li> 
      <li>Post 2 within subcategory 1</li> 
      <li>Post 3 within subcategory 1</li> 
     </ul> 
     <li>Subcategory 2 within category 2</li> 
     <ul> 
      <li>Post 1 within subcategory 2</li> 
      <li>Post 2 within subcategory 2</li> 
      <li>Post 3 within subcategory 2</li> 
     </ul> 
     <li>Subcategory 3 within category 2</li> 
     <ul> 
      <li>Post 1 within subcategory 2</li> 
      <li>Post 2 within subcategory 2</li> 
      <li>Post 3 within subcategory 2</li> 
     </ul> 
     <li>Posts that have no subcategory</li> 
     <ul> 
      <li>Post 1 with no subcategory</li> 
      <li>Post 2 with no subcategory</li> 
     </ul> 
    </ul> 
</ul> 

jetzt weit nach so nach allem, was ich lesen auf dem Thema, das ich den folgenden Code haben gefunden konnte:

<ul> 
    <?php 
     $get_parent_cats = array(
      'parent' => '0' //get top level categories only 
     ); 

     $all_categories = get_categories($get_parent_cats);//get parent categories 

     foreach($all_categories as $single_category){ 
      //for each category, get the ID 
      $catID = $single_category->cat_ID; 

      echo '<li><a href=" ' . get_category_link($catID) . ' ">' . $single_category->name . '</a>'; //category name & link 
      $get_children_cats = array(
       'child_of' => $catID //get children of this parent using the catID variable from earlier 
      ); 

      $child_cats = get_categories($get_children_cats);//get children of parent category 
      echo '<ul class="children">'; 
       foreach($child_cats as $child_cat){ 
        //for each child category, get the ID 
        $childID = $child_cat->cat_ID; 

        //for each child category, give us the link and name 
        echo '<a href=" ' . get_category_link($childID) . ' ">' . $child_cat->name . '</a>'; 

       } 
      echo '</ul></li>'; 
     } //end of categories logic ?> 
</ul> 

Nun ist diese Der Code zeigt Kategorien und Unterkategorien an, aber ich muss meine Posts irgendwie durchgehen und sie mit Kategorien/Unterkategorien anzeigen. Ich habe auch versucht Brache-Code zu verwenden:

<?php 
     // get all the categories from the database 
     $cats = get_categories(); 

      // loop through the categries 
      foreach ($cats as $cat) { 
       // setup the cateogory ID 
       $cat_id= $cat->term_id; 
       // Make a header for the cateogry 
       echo "<h2>".$cat->name."</h2>"; 
       // create a custom wordpress query 
       query_posts("cat=$cat_id&posts_per_page=100"); 
       // start the wordpress loop! 
       if (have_posts()) : while (have_posts()) : the_post(); ?> 

        <?php // create our link now that the post is setup ?> 
        <a href="<?php the_permalink();?>"><?php the_title(); ?></a> 
        <?php echo '<hr/>'; ?> 

       <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?> 
      <?php } // done the foreach statement ?> 

     </div><!-- #content --> 
    </div><!-- #container --> 

Dieser Code zeigt alle Kategorien und Beiträge in bestimmten Kategorie, aber die Struktur ist nicht das, was ich will. Ich habe versucht, diese zwei Codeschnipsel für zwei Tage zu kombinieren, aber nichts, das ich versuche, gibt mir das Ergebnis, das ich will. Ich bin mit Wordpress unerfahren und ich könnte wirklich Hilfe mit diesem verwenden.

Antwort

0

Dies ist die Lösung für alle Interessierten:

<ul> 
     <?php 
      $get_parent_cats = array(
       'parent' => '0' //get top level categories only 
      ); 

      $all_categories = get_categories($get_parent_cats);//get parent categories 

      foreach($all_categories as $single_category){ 
       //for each category, get the ID 
       $catID = $single_category->cat_ID; 

       echo '<li><a href=" ' . get_category_link($catID) . ' ">' . $single_category->name . '</a>'; //category name & link 
       echo '<ul class="post-title">'; 

       $query = new WP_Query(array('cat'=> $catID, 'posts_per_page'=>10)); 
       while($query->have_posts()):$query->the_post(); 
       echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>'; 
       endwhile; 
       wp_reset_postdata(); 

       echo '</ul>'; 
       $get_children_cats = array(
        'child_of' => $catID //get children of this parent using the catID variable from earlier 
       ); 

       $child_cats = get_categories($get_children_cats);//get children of parent category 
       echo '<ul class="children">'; 
        foreach($child_cats as $child_cat){ 
         //for each child category, get the ID 
         $childID = $child_cat->cat_ID; 

         //for each child category, give us the link and name 
         echo '<a href=" ' . get_category_link($childID) . ' ">' . $child_cat->name . '</a>'; 

         echo '<ul class="post-title">'; 

         $query = new WP_Query(array('cat'=> $childID, 'posts_per_page'=>10)); 
         while($query->have_posts()):$query->the_post(); 
         echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>'; 
         endwhile; 
         wp_reset_postdata(); 

         echo '</ul>'; 

        } 
       echo '</ul></li>'; 
      } //end of categories logic ?> 
    </ul> 
Verwandte Themen