删除了原始代码示例,并根据新答案进行了更新:
老实说,这没有太大变化,只是让它更简洁了一点,我将解释我所做的主要工作。
您可以大量重用此代码示例:
<li class="cat-item"><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a><span class="post_count"><?php echo " ($cat_posts->found_posts+$cat_probs->found_posts)"; ?></span>
内联,这变得相对难以阅读,如果您需要更改或添加一个类,它会变得有点乏味。由于它重复了三次,您可能应该将其转换为函数,因此在开始时,我创建了一个名为
jjycjn_display_link()
这将获取category对象以及posts查询和probs查询返回的对象,并自动回显输出,因此现在只需使用:
<?php display_link( $cat, $cat_posts, $cat_probs ); ?>
在读取这样的文件时,这更容易理解。
您可以做的另一件事是在if
陈述您不需要分配一个变量,然后检查它是否存在,您可以在一条语句中完成。所以你的
$categories = get_categories( array(\'parent\' => 0, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) );
if( $categories ):
在功能上等同于
if( $categories = get_categories( array(\'parent\' => 0, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ) ):
将分配
$categories
返回值为
get_categories()
, 如果它返回
Truthy Value 然后它将继续到语句中。
您甚至可以跳过分配变量,如$cat_posts
只需将WP\\u查询放入jjycjyn_display_link()
函数,因为您不使用指定的变量,但出于可读性和调试目的,我保留了它(如果您已将其指定给变量,则更易于调试)
祝你好运!
<?php
function jjycjn_display_link( $cat, $posts, $probs ){
echo \'<a href="\'. get_category_link($cat->term_id) .\'">\'. $cat->name .\'</a><span class="post_count"> (\'. $posts->found_posts + $probs->found_posts .\')</span>\';
}
?>
<aside class="widget widget_categories">
<h3 class="widget-title"><span class="widget-title-tab">Categories</span></h3>
<?php if( $categories = get_categories( array(\'parent\' => 0, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ) ):
echo \'<ul>\';
foreach($categories as $cat):
$cat_posts = new WP_Query( array( \'cat\' => $cat->term_id, \'post_type\' => \'post\' ) );
$cat_probs = new WP_Query( array( \'cat\' => $cat->term_id, \'post_type\' => \'probsoln\' ) ); ?>
<li class="cat-item"><?php jjycjn_display_link( $cat, $cat_posts, $cat_probs ); ?>
<?php if( $sub_categories = get_categories( array(\'parent\' => $cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ) ):
echo \'<ul class="children">\';
foreach($sub_categories as $sub_cat):
$sub_cat_posts = new WP_Query( array( \'cat\' => $sub_cat->term_id, \'post_type\' => \'post\' ) );
$sub_cat_probs = new WP_Query( array( \'cat\' => $sub_cat->term_id, \'post_type\' => \'probsoln\' ) ); ?>
<li class="cat-item"><?php jjycjn_display_link( $sub_cat, $sub_cat_posts, $sub_cat_probs ); ?>
<?php if( $sub_sub_categories = get_categories( array(\'parent\' => $sub_cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ) ):
echo \'<ul class="children">\';
foreach ($sub_sub_categories as $sub_sub_cat):
$sub_sub_cat_posts = new WP_Query( array ( \'cat\' => $sub_sub_cat->term_id, \'post_type\' => \'post\' ) );
$sub_sub_cat_probs = new WP_Query( array ( \'cat\' => $sub_sub_cat->term_id, \'post_type\' => \'probsoln\' ) ); ?>
<li class="cat-item"><?php jjycjn_display_link( $sub_sub_cat, $sub_sub_cat_posts, $sub_sub_cat_probs ); ?>
<?php endforeach;
echo \'</ul>\';
endif;
echo \'</li>\';
endforeach;
echo \'</ul>\';
endif;
echo \'</li>\';
endforeach;
echo \'</ul>\';
endif; ?>
</aside>