4

我在这里有以下代码。显示我也想要类别名称和描述。现在我需要显示他们在其类别中的帖子。我怎么做?

<?php 
    $args = array(
        'orderby' => 'id',
        'hide_empty'=> 0,
        'child_of' => 10, //Child From Boxes Category 
    );
    $categories = get_categories($args);
    foreach ($categories as $cat) {
        echo '<div class="one_fourth">';
        echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
        $post = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 10 );
        $posts_array = get_posts( $post );

        echo '</div>';
    }
    ?>

如果有任何其他方法可以获取子类别帖子并循环显示子类别名称和帖子。请在这里告诉我。

4

3 回答 3

21

在我解决了这个问题之后。我想这会对你有所帮助。

    <?php 
    $args = array(
      'orderby' => 'id',
      'hide_empty'=> 0,
      'child_of' => 5, //Child From Boxes Category 
  );
  $categories = get_categories($args);
  foreach ($categories as $cat) {
        echo '<div style="width:20%;float:left;">';
        echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
        //echo '<br />';
        $args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
        $posts_in_category = get_posts($args2);
        foreach($posts_in_category as $current_post) {
            echo '<span>';
            ?>
            <li type='none' style='list-style-type: none !important;'><a href="<?=$current_post->guid;?>"><?='+ '.$current_post->post_title;?></a></li>
            <?php
            echo '</span>';
        }
        echo '</div>';
    }
?>
于 2013-09-15T07:09:05.903 回答
1
  • 为获取所有父类别而写
  • 现在为他们做一个 foreach 循环并获取他们的子类别。
  • 现在在上面的 foreach 下使用这个获取子类别的帖子写另一个 foreach

    $parent_cats = get_categories($args);

    foreach ( $parent_cats as $parent_cat) {
        $child_cats = some wp functions to get child cats of current parent category
        foreach ( $child_cats as $child_cat ) {
            $child_cat_post = get the post of child category
        }
    }
    

有用的链接: http: //codex.wordpress.org/Function_Reference/get_categories http://codex.wordpress.org/Template_Tags/get_posts

于 2013-09-05T09:13:16.977 回答
1

WordPress 有<?php wp_dropdown_categories( $args ); ?>

示例用法(使用 JavaScript 的不带提交按钮的下拉菜单)

<li id="categories">
  <h2><?php _e( 'Posts by Category' ); ?></h2>
  <?php wp_dropdown_categories( 'show_option_none=Select category' ); ?>
  <script type="text/javascript">
  <!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
      if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
        location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
      }
    }
    dropdown.onchange = onCatChange;
  -->
  </script>
</li>

其他示例可在 Codex 网站上找到 - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories

于 2018-05-29T19:56:59.787 回答