样式子类别与其父类别不同

时间:2014-06-17 作者:Zymeth

我是wordpress和这个网站的新手。我一直在寻找解决我问题的办法。我发现这段代码解决了我的问题,但不幸的是,我无法将子类别与父类别的样式区分开来。我不是php程序员,有人能告诉我怎么做吗?

我想要这样的东西

<ul>
 <h2>Category 1</h2>
 <p>Description of the Category1</p>
 <li>Sub Category link 1</li>
 <li>Sub Category link 2</li>
 <li>Sub Category link 3</li>
</ul>

<ul>
 <h2>Category 2</h2>
 <p>Description of the Category2</p>
 <li>Sub Category link 1</li>
 <li>Sub Category link 2</li>
 <li>Sub Category link 3</li>
</ul>

 <ul>
 <h2>Category 3</h2>
 <p>Description of the Category3</p>
 <li>Sub Category link 1</li>
 <li>Sub Category link 2</li>
 <li>Sub Category link 3</li>
</ul>
这是我使用的代码,来自here

<?php 
if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
    echo \'<div id="catlist"><ul>\';
    $childcategories = get_categories(array(
        \'orderyby\' => \'name\',
        \'hide_empty\' => false,
        \'child_of\' => $this_category->cat_ID
        ));
    foreach($childcategories as $category) {
        echo \'<a href="\' . get_category_link( $category->term_id ) . \'" title="\' .     sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category-    >name.\'</a>\';
        echo \'<p>\'.$category->description.\'</p>\';
    }
    echo \'</ul></div>\';
}
}
?>

EDIT

这是我的完整代码

<?php get_header(); ?>
<div id="content">
<?php if ( function_exists(\'yoast_breadcrumb\') ) { yoast_breadcrumb(\'<p   id="breadcrumbs">\',\'</p>\');} ?>
    <div id="cont_left">
    <h1><?php single_cat_title( \'\', true ); ?></h1>
    <?php 
        if (is_category()) {
            $this_category = get_category($cat);
            if (get_category_children($this_category->cat_ID) != "") {
             echo \'<div id="cat-display"><ul>\';
                $childcategories = get_categories(array(
                  \'orderyby\' => \'name\',
                   \'hide_empty\' => false,
                   \'child_of\' => $this_category->cat_ID
                   ));

             foreach($childcategories as $category) {
                  echo \'<img src="http://marinelcdpro.com/wp-  content/uploads/2014/05/\' . $category->cat_ID . \'.jpg" alt="\' . $category->cat_name . \'"  />\';
                  echo \'<a href="\' . get_category_link( $category->term_id  ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' .  $category->      name.\'</a>\';

                    echo \'<p>\'.$category->description.\'</p>\';
             }
             echo \'</ul></div>\';
         }
        }
        ?>
    </div>
    <div ><?php get_sidebar(); ?></div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
这是output

实际上,我需要在父类别中放置一个图像,并在父类别下放置一个子类别链接列表。

1 个回复
SO网友:Subharanjan

它将按照您的要求生成结构。

if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo \'<div id="catlist"><ul>\';
        $childcategories = get_categories(array(
            \'orderyby\' => \'name\',
            \'hide_empty\' => false,
            \'child_of\' => $this_category->cat_ID
            ));
        foreach($childcategories as $category) {
            echo \'<li class="sub-cat">\';
            echo \'<a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a>\';
            echo \'<p>\'.$category->description.\'</p>\';
            echo \'</li>\';
        }
        echo \'</ul></div>\';
    }
}

结束

相关推荐

使用Get_Categories显示类别的图像,或显示任何子帖子中的图像

我正在使用get\\u categories列出父类别的子类别。我想使用get\\u categories输出将图像添加到子类别。我可以从我正在使用get\\u categories的类别的子类别(即父类别的子类别)的任何帖子中获取特色图像。我不想显示任何其他孙儿信息,只想从每组类别的孩子中获得一张特色图片我当前使用的代码是$args = array(\'child_of\' => 1 ); $categories = get_categories($args); forea