Custom Category widget

时间:2018-02-27 作者:jjycjn

我在侧边栏中有一个自定义代码。php,显示我的所有类别和帖子数量+自定义帖子。代码运行良好,但由于我从stackexchange论坛复制/粘贴和修改了其他各种代码,我想知道代码的效率有多高。

我对php没有任何背景知识,但至少我可以看出下面的代码有点凌乱。特别是,该代码将几乎相同的行重复三次,以正确显示所有子类别,并分别统计正常帖子和自定义帖子。代码显示如下内容

祖父母(100+20)

孩子1.1(10+0)

  • 孩子1.2(10+5)
  • 父母2(80+15)
    • 孩子2.1(40+5)
    • 孩子2.2(40+5)
    • 孩子2.3(0+5)
        • 有办法简化下面的代码吗?代码在速度+效率方面有多好/有多差?

          <aside class="widget widget_categories">
          <h3 class="widget-title"><span class="widget-title-tab">Categories</span></h3>  
              <?php $categories = get_categories( array(\'parent\' => 0, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ); ?>
              <?php if($categories): ?>
                  <ul>
                      <?php $cat_posts = get_posts(\'post_type=post&category=\' . $cat->term_id . \'&numberposts=-1\'); ?>
                      <?php $cat_probs = get_posts(\'post_type=probsoln&category=\' . $cat->term_id . \'&numberposts=-1\'); ?>
                      <?php $count = count($cat_posts); ?>
                      <?php $count_prob = count($cat_probs); ?>
                      <li class="cat-item" style=""><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a><span class="post_count"><?php echo " ($count+$count_prob)"; ?></span>
                      <?php $sub_categories = get_categories( array(\'parent\' => $cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ); ?>
                      <?php if($sub_categories): ?>
                          <ul class="children">
                          <?php foreach ($sub_categories as $sub_cat): ?>
                              <?php $sub_cat_posts = get_posts(\'post_type=post&category=\' . $sub_cat->term_id . \'&numberposts=-1\'); ?>
                              <?php $sub_cat_probs = get_posts(\'post_type=probsoln&category=\' . $sub_cat->term_id . \'&numberposts=-1\'); ?>
                              <?php $sub_count = count($sub_cat_posts); ?>
                              <?php $sub_count_prob = count($sub_cat_probs); ?>
                                  <li class="cat-item"><a href="<?php echo get_category_link($sub_cat->term_id); ?>"><?php echo $sub_cat->name; ?></a><span class="post_count"><?php echo " ($sub_count+$sub_count_prob)"; ?></span>
                                  <?php $sub_sub_categories = get_categories( array(\'parent\' => $sub_cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) ); ?>
                                  <?php if($sub_sub_categories): ?>
                                      <ul class="children">
                                      <?php foreach ($sub_sub_categories as $sub_sub_cat): ?>
                                      <?php $sub_sub_cat_posts = get_posts(\'post_type=post&category=\' . $sub_sub_cat->term_id . \'&numberposts=-1\'); ?>
                                      <?php $sub_sub_cat_probs = get_posts(\'post_type=probsoln&category=\' . $sub_sub_cat->term_id . \'&numberposts=-1\'); ?>
                                      <?php $sub_sub_count = count($sub_sub_cat_posts); ?>
                                      <?php $sub_sub_count_prob = count($sub_sub_cat_probs); ?>   
                                          <li class="cat-item"><a href="<?php echo get_category_link($sub_sub_cat->term_id); ?>"><?php echo $sub_sub_cat->name; ?></a><span class="post_count"><?php echo " ($sub_sub_count+$sub_sub_count_prob)"; ?></span>
                                          </li>
                                      <?php endforeach; ?>
                                      </ul>       
                                  <?php endif; ?>
                              </li>       
                          <?php endforeach; ?>
                          </ul>   
                      <?php endif; ?>
                      </li>
                  <?php endforeach; ?>
              </ul>
              <?php endif; ?>
          </aside>
          

  • 2 个回复
    最合适的回答,由SO网友:Xhynk 整理而成

    删除了原始代码示例,并根据新答案进行了更新:

    老实说,这没有太大变化,只是让它更简洁了一点,我将解释我所做的主要工作。

    您可以大量重用此代码示例:

    <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>
    

    SO网友:jjycjn

    多亏了@Xhynk,我对以前的凌乱代码进行了修改。get_posts 现在更改为WP_Query 所有不必要的<?php?> 将删除对。我不知道这一个是否比前一个好,但它肯定更短!应该有办法打电话WP_Query 在每个for循环中只执行一次函数,但我找不到方法来执行它。我会在得到解决方案后更新答案。

    <aside class="widget widget_categories">
    <h3 class="widget-title"><span class="widget-title-tab">Categories</span></h3>  
        <?php $categories = get_categories( array(\'parent\' => 0, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) );
        if($categories):
        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" style=""><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>
            <?php $sub_categories = get_categories( array(\'parent\' => $cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) );
                if($sub_categories):
                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"><a href="<?php echo get_category_link($sub_cat->term_id); ?>"><?php echo $sub_cat->name; ?></a><span class="post_count"><?php echo " ($sub_cat_posts->found_posts+$sub_cat_probs->found_posts)"; ?></span>
                    <?php $sub_sub_categories = get_categories( array(\'parent\' => $sub_cat->term_id, \'hide_empty\' => 0, \'orderby\' => \'term_order\' ) );
                        if($sub_sub_categories):
                        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"><a href="<?php echo get_category_link($sub_sub_cat->term_id); ?>"><?php echo $sub_sub_cat->name; ?></a><span class="post_count"><?php echo " ($sub_sub_cat_posts->found_posts+$sub_sub_cat_probs->found_posts)"; ?></span>
                            </li>
                        <?php endforeach;
                        echo \'</ul>\';       
                        endif;
                    echo \'</li>\';       
                endforeach;
                echo \'</ul>\';   
                endif;
            echo \'</li>\';
        endforeach;
        echo \'</ul>\';
        endif; ?>
    </aside>
    

    结束

    相关推荐

    按帖子类型筛选wp_Dropdown_Categories

    我正在使用wp\\u dropdown\\u categories向媒体库添加类别过滤器。它工作正常,除了显示分配给帖子的所有类别,但我希望它只显示分配给附件的类别。如果只为附件更新计数也很好。下面是将类别添加到附件并允许您对其进行筛选的代码。/* Add categories to attachments/media library */ function wptp_add_categories_to_attachments() { register_taxonomy_for_objec