如何使用循环 (WordPress) 显示我的所有类别名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18631655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display my all category names using loop (WordPress)
提问by yeshansachithak
I have below code here. It's display that I want category names and description as well. Now I need to display post that they have inside their categories. How I do it?
我这里有以下代码。它显示我也想要类别名称和描述。现在我需要显示他们在其类别中的帖子。我怎么做?
<?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>';
}
?>
If there have any other way to get child category post and display child category name and posts in loop. Please let me to know here.
如果有任何其他方法可以获取子类别帖子并循环显示子类别名称和帖子。请让我知道这里。
回答by yeshansachithak
After I solved this. I think this will help for you.
在我解决了这个问题之后。我认为这对你有帮助。
<?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>';
}
?>
回答by Manish
- Write for get all parent category
- Now do a foreach loop for them and get their child category.
Now under above foreach write another foreach using this get post for child category
$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 } }
- 写入获取所有父类别
- 现在为他们做一个 foreach 循环并获取他们的子类别。
现在在上面的 foreach 下写另一个 foreach 使用这个 get post for child category
$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 } }
Helpful links: http://codex.wordpress.org/Function_Reference/get_categorieshttp://codex.wordpress.org/Template_Tags/get_posts
有用的链接:http: //codex.wordpress.org/Function_Reference/get_categories http://codex.wordpress.org/Template_Tags/get_posts
回答by nihiser
Wordpress has <?php wp_dropdown_categories( $args ); ?>
WordPress 有 <?php wp_dropdown_categories( $args ); ?>
Example Usage (Dropdown without a Submit Button using JavaScript)
使用示例(使用 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>
Other examples can be found on the Codex site - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
其他示例可以在 Codex 网站上找到 - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories