Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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中获取类别_Php_Css_Wordpress - Fatal编程技术网

Php 如何在wordpress中获取类别

Php 如何在wordpress中获取类别,php,css,wordpress,Php,Css,Wordpress,我想在wordpress网站上获得所有类别,但要分别获得父类别和子类别(这样我很容易进行样式设置)。在下面的代码中,我得到了父类别,但所有子类别都对每个父类别重复。 谢谢 有几种方法可以创建类别的HTML列表 get_the_categories(); function get_the_categories( $parent = 0 ) { $categories = get_categories( "hide_empty=0&parent=$parent" );

我想在wordpress网站上获得所有类别,但要分别获得父类别和子类别(这样我很容易进行样式设置)。在下面的代码中,我得到了父类别,但所有子类别都对每个父类别重复。 谢谢


有几种方法可以创建类别的HTML列表

get_the_categories();

function get_the_categories( $parent = 0 ) 
{
    $categories = get_categories( "hide_empty=0&parent=$parent" );

    if ( $categories ) {
        echo '<ul class="span-5 colborder list_main">';
        foreach ( $categories as $cat ) {
            if ( $cat->category_parent == $parent ) {
                echo '<a href="' . get_category_link( $cat->term_id ) . '" >' . $cat->name . '</a><br/>';
                get_the_categories( $cat->term_id );
            }
        }
        echo '</ul>';
    }
}

最简单的方法是使用默认CSS选择器显示类别列表并为输出设置样式:

li.categories li.cat-item li.cat-item-7 li.current-cat li.current-cat-parent ul.children 递归函数也是遍历类别的好方法。这样,您必须在每个节点上手动执行。由于该函数返回所有子节点中的所有子类别,因此必须传递当前类别的id才能仅显示当前级别类别

get_the_categories();

function get_the_categories( $parent = 0 ) 
{
    $categories = get_categories( "hide_empty=0&parent=$parent" );

    if ( $categories ) {
        echo '<ul class="span-5 colborder list_main">';
        foreach ( $categories as $cat ) {
            if ( $cat->category_parent == $parent ) {
                echo '<a href="' . get_category_link( $cat->term_id ) . '" >' . $cat->name . '</a><br/>';
                get_the_categories( $cat->term_id );
            }
        }
        echo '</ul>';
    }
}
get_the_categories();
函数获取类别($parent=0)
{
$categories=get_categories(“hide_empty=0&parent=$parent”);
如果($类别){
echo'
    ; foreach($类别为$cat){ 如果($cat->category_parent==$parent){ 回声“
    ”; 获取\u类别($cat->term\u id); } } 回声“
”; } }
有几种方法可以创建类别的HTML列表

get_the_categories();

function get_the_categories( $parent = 0 ) 
{
    $categories = get_categories( "hide_empty=0&parent=$parent" );

    if ( $categories ) {
        echo '<ul class="span-5 colborder list_main">';
        foreach ( $categories as $cat ) {
            if ( $cat->category_parent == $parent ) {
                echo '<a href="' . get_category_link( $cat->term_id ) . '" >' . $cat->name . '</a><br/>';
                get_the_categories( $cat->term_id );
            }
        }
        echo '</ul>';
    }
}

最简单的方法是使用默认CSS选择器显示类别列表并为输出设置样式:

li.categories li.cat-item li.cat-item-7 li.current-cat li.current-cat-parent ul.children 递归函数也是遍历类别的好方法。这样,您必须在每个节点上手动执行。由于该函数返回所有子节点中的所有子类别,因此必须传递当前类别的id才能仅显示当前级别类别

get_the_categories();

function get_the_categories( $parent = 0 ) 
{
    $categories = get_categories( "hide_empty=0&parent=$parent" );

    if ( $categories ) {
        echo '<ul class="span-5 colborder list_main">';
        foreach ( $categories as $cat ) {
            if ( $cat->category_parent == $parent ) {
                echo '<a href="' . get_category_link( $cat->term_id ) . '" >' . $cat->name . '</a><br/>';
                get_the_categories( $cat->term_id );
            }
        }
        echo '</ul>';
    }
}
get_the_categories();
函数获取类别($parent=0)
{
$categories=get_categories(“hide_empty=0&parent=$parent”);
如果($类别){
echo'
    ; foreach($类别为$cat){ 如果($cat->category_parent==$parent){ 回声“
    ”; 获取\u类别($cat->term\u id); } } 回声“
”; } }
我不久前就实现了这段代码,但您可以尝试一下

     // get_categories() function will return all the categories
            $upaae_categories = get_categories( array(
             'orderby' => 'name',
             'order' => 'ASC'
            ) );

            foreach( $upaae_categories as $single_cat ) {
             if($single_cat->parent < 1) // Display if parent category and exclude child categories
             {
        echo 'Parent: '.$single_cat->name;
        // now get all the child categories
        $child_categories=get_categories(
            array( 'parent' => $single_cat->term_id )
        );
        if(sizeof($child_categories)>0){ /* this is just for ensuring that this parent category do have child categories otherwise a category cannot be a parent if does not have any child categories*/
        echo '###childs###</br>'
        foreach ($child_categories as $child) {

            echo $child->name.'</br>';
        }// end of loop displaying child categories
        } //end of if parent have child categories

             }
            } 
//get_categories()函数将返回所有类别
$upaae_categories=获取_类别(数组(
'orderby'=>'name',
“订单”=>“ASC”
) );
foreach($upaae_类别为$single_类别){
if($single_cat->parent<1)//显示if父类别和exclude子类别
{
回显“父项:”。$single_cat->name;
//现在获取所有子类别
$child\u categories=get\u categories(
数组('parent'=>$single\u cat->term\u id)
);
如果(sizeof($child_categories)>0){/*这只是为了确保此父类别确实有子类别,否则如果没有任何子类别,则类别不能是父类别*/
回音“####childs####
” foreach($child\u类别为$child){ echo$child->name.“
”; }//循环结束显示子类别 }//如果父类别有子类别,则结束 } }
我不久前就实现了这段代码,但您可以尝试一下

     // get_categories() function will return all the categories
            $upaae_categories = get_categories( array(
             'orderby' => 'name',
             'order' => 'ASC'
            ) );

            foreach( $upaae_categories as $single_cat ) {
             if($single_cat->parent < 1) // Display if parent category and exclude child categories
             {
        echo 'Parent: '.$single_cat->name;
        // now get all the child categories
        $child_categories=get_categories(
            array( 'parent' => $single_cat->term_id )
        );
        if(sizeof($child_categories)>0){ /* this is just for ensuring that this parent category do have child categories otherwise a category cannot be a parent if does not have any child categories*/
        echo '###childs###</br>'
        foreach ($child_categories as $child) {

            echo $child->name.'</br>';
        }// end of loop displaying child categories
        } //end of if parent have child categories

             }
            } 
//get_categories()函数将返回所有类别
$upaae_categories=获取_类别(数组(
'orderby'=>'name',
“订单”=>“ASC”
) );
foreach($upaae_类别为$single_类别){
if($single_cat->parent<1)//显示if父类别和exclude子类别
{
回显“父项:”。$single_cat->name;
//现在获取所有子类别
$child\u categories=get\u categories(
数组('parent'=>$single\u cat->term\u id)
);
如果(sizeof($child_categories)>0){/*这只是为了确保此父类别确实有子类别,否则如果没有任何子类别,则类别不能是父类别*/
回音“####childs####
” foreach($child\u类别为$child){ echo$child->name.“
”; }//循环结束显示子类别 }//如果父类别有子类别,则结束 } }