Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress-高级自定义功能wp\u列表\u类别_Php_Wordpress - Fatal编程技术网

Php WordPress-高级自定义功能wp\u列表\u类别

Php WordPress-高级自定义功能wp\u列表\u类别,php,wordpress,Php,Wordpress,使用WordPress功能“wp\u list\u categories”时,如: 它结果: <li class="cat-item"> <a href="x" title="x">Cat Name</a> (Cat Count) </li> <h2> <a href="x" title="x" ><img src="Category_Slug.png" alt="x"/> Cat Name</a&g

使用WordPress功能“wp\u list\u categories”时,如:


结果:

<li class="cat-item">
<a href="x" title="x">Cat Name</a> (Cat Count)
</li>
<h2>
<a href="x" title="x" ><img src="Category_Slug.png" alt="x"/> Cat Name</a> (Cat Count)
</h2><hr />
  • (猫数)
  • 我的目标:

    <li class="cat-item">
    <a href="x" title="x">Cat Name</a> (Cat Count)
    </li>
    
    <h2>
    <a href="x" title="x" ><img src="Category_Slug.png" alt="x"/> Cat Name</a> (Cat Count)
    </h2><hr />
    
    
    (猫数)
    

    如何修改此函数以获得目标结果?

    您可以使用类似的方法完全控制类别列表

    global $wpdb;
    $taxonomy = CUSTOM_CAT_TYPE; // in your case, it's categories
    $table_prefix = $wpdb->prefix;
    $wpcat_id = NULL;
    
    //Fetch category                          
    $wpcategories = (array) $wpdb->get_results("
        SELECT * FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
        WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
        AND {$table_prefix}term_taxonomy.taxonomy ='" . $taxonomy . "' and  {$table_prefix}term_taxonomy.parent=0  
        ORDER BY {$table_prefix}terms.name ASC" ); 
    
    foreach ($wpcategories as $wpcat) { 
        $name = $wpcat->name;
        echo '<a href="'.$wpcat->slug.'"><img src="'.$wpcat->slug.'.png"/></a>';
    }
    
    global$wpdb;
    $taxonomy=自定义类别;//在你的例子中,它是类别
    $table_prefix=$wpdb->prefix;
    $wpcat_id=NULL;
    //获取类别
    $wpcategories=(数组)$wpdb->获取结果(“
    从{$table\U prefix}术语、{$table\U prefix}术语分类中选择*
    其中{$table\u prefix}terms.term\u id={$table\u prefix}term\u taxonomy.term\u id
    和{$table_prefix}term_taxonomy.taxonomy='“$taxonomy.”和{$table_prefix}term_taxonomy.parent=0
    按{$table_prefix}terms.name ASC“)排序;
    foreach($wpcat){
    $name=$wpcat->name;
    回声';
    }
    
    如果有父类别和子类别,则可以执行类似的操作

    foreach ($wpcategories as $wpcat) { 
        $name = $wpcat->name;
        $termid = $wpcat->term_id; //parent category ID
    
        $args = array(
          'type' => POST_TYPE, //in normal case its post otherwise custom pist type
           'child_of' => '',
           'parent' => $termid, //parent category ID
           'orderby' => 'name',
           'order' => 'ASC',
           'hide_empty' => 0,
           'hierarchical' => 1,
           'exclude' => $termid, //stop duplicate parent category
           'include' => '',
           'number' => '',
           'taxonomy' => CUSTOM_CAT_TYPE, //categories in ur case
           'pad_counts' => false);
    
        $acb_cat = get_categories($args); //ALSO can SIMPLY USE this LINE and previous arguments
                        //Code for showing number of posts from subcategories you can do it for categories as you have get_categories output.
                            $postCount = 0;   
                                foreach($acb_cat as $subcat)
                                {                                    
                                    $countposts = get_term($subcat->term_id,$taxonomy);
    
                                    $postCount += $countposts->count;
                                }
        echo '<a href="'.$wpcat->slug.'"><img src="'.$wpcat->slug.'.png"/></a>'.$postCount;
    }
    
    foreach($wpcatas$wpcat){
    $name=$wpcat->name;
    $termid=$wpcat->term\u id;//父类别id
    $args=数组(
    'type'=>POST\u type,//在正常情况下,它的POST类型是自定义的pist类型
    'child_of'=>'',
    '父'=>$termid,//父类别ID
    'orderby'=>'name',
    “订单”=>“ASC”,
    “hide_empty”=>0,
    “层次结构”=>1,
    'exclude'=>$termid,//停止重复父类别
    '包括'=>'',
    '编号'=>'',
    'taxonomy'=>自定义类型,//您案例中的类别
    “pad_计数”=>false);
    $acb_cat=get_categories($args);//也可以简单地使用此行和前面的参数
    //用于显示子类别中的帖子数量的代码,您可以在获得_categories输出时对类别执行此操作。
    $postCount=0;
    foreach($acb_cat作为$subcat)
    {                                    
    $countposts=get\u term($subcat->term\u id,$taxonomy);
    $postCount+=$countposts->count;
    }
    回显“”。$postCount;
    }
    
    您还可以像这样向主题的function.php添加新函数

      add_filter ( 'wp_list_categories', 'img_before_link_list_categories' );
    
        function img_before_link_list_categories( $list ) {
          $cats = get_categories();
            foreach($cats as $cat) {
    
                $find = $cat->name.'</a>';
                $replace = '<img src="'.$cat->slug.'.png" alt="x"/>'.$cat->name.'</a>';
                $list = str_replace( $find, $replace, $list );
    
                $list = preg_replace('%<li class=".*">|</?ul>%U', '<h2>', $list);
                $list = str_replace('</li>', '</h2>', $list);
            }
         return $list;
        }
    
    add_filter('wp_list_categories','img_before_link_list_categories');
    函数img\u前链接\u列表\u类别($list){
    $cats=get_categories();
    foreach($cats作为$cat){
    $find=$cat->name.';
    $replace='slug..png“alt=“x”/>。$cat->name.”;
    $list=str_replace($find,$replace,$list);
    $list=preg_replace('%
  • |%U',''$list); $list=str_replace(“
  • ”,“$list”); } 返回$list; }
    解释原因总是很好。@Ram:Mate您的示例代码与我真正需要的非常接近。它根据我的需要添加图像。我只需要做一件事,就是将
  • 更改/替换为。我该怎么做?感谢创建另一个$find=“
  • ”;--$replace=“”;--$list=str u replace($find,$replace,$list)在最后一行$list=str之后再次…@Cyborg:我已经根据您的要求修改了我的答案,检查答案并将其标记为正确。这样它也会对其他人有所帮助。只需使用$acb_cat=get_categories($args);您还需要每个类别中的帖子数量吗?伙计,我有父类别和子类别。我想显示类别ID 78中的所有类别,类别ID 78中的所有类别都有20个类别,并且所有类别都有子类别。因此,我想显示这20个类别,包括子类别中的帖子数量。您的代码非常高级,我正在查找或者类似的东西。但是当我把你的代码添加到我的主题中时,它没有显示任何结果或错误。让你用“category”替换自定义类型,用“POST”替换POST类型。