【问题标题】:How to display my all category names using loop (WordPress)如何使用循环显示我的所有类别名称(WordPress)
【发布时间】:2013-09-05 08:42:52
【问题描述】:

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

<?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>';
    }
    ?>

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

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

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

        <?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>';
        }
    ?>
    

    【讨论】:

    • 如果这篇文章对你有帮助,请投票。非常感谢
    • 感谢您提供好的解决方案。我想知道现在如何仅在标题中显示内容。我也想显示内容和高级自定义字段值。你能帮忙吗
    • @pagol 你可以使用$current_post-&gt;post_content; 来显示帖子的内容。检查此 AdvancedCustomFields advancedcustomfields.com/resources/query-posts-custom-fields
    【解决方案2】:
    • 为获取所有父类别而写
    • 现在为他们执行一个 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

    【讨论】:

      【解决方案3】:

      Wordpress 有&lt;?php wp_dropdown_categories( $args ); ?&gt;

      示例用法(使用 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多