在循环的不同部分显示特定类别的级别
Display specific categories' levels in different parts of a loop
我需要在 Wordpress 循环中组合两件事。
首先,我想显示所有类别中每个 second-level 类别(示例中的 C1.1 和 C2.1)的类别名称。
那我想在每个second-level的分类标题下放一个table。它们的列数应与每个 second-level 类别的 third-levels(如示例中的 C1.1.1)一样多。在每一列中,我想显示该 third-level 类别的每个 post。
类别层次结构:
C1
— C1.1
— — C1.1.1
— — C1.1.2
— — C1.1.3
C2
— C2.1
— — C2.1.1
— — C2.1.2
...
这是一个例子:
C1.1
+——————————+——————————+——————————+
| C1.1.1 | C1.1.2 | C1.1.3 |
+——————————+——————————+——————————+
| Post | Post | Post |
| Post | Post | |
| | Post | |
+——————————+——————————+——————————+
C2.1
+——————————+——————————+
| C2.1.1 | C2.1.2 |
+——————————+——————————+
| Post | Post |
| Post | Post |
| Post | Post |
| | Post |
| | Post |
+——————————+——————————+
...
是否可以在自定义模板页面中执行此操作?
试试这个,
<?php
//Get All Categories like Parent, parent of child, child of subchild (Level 0 , 1, 2)
$taxonomy = 'category'; //Choose the taxonomy
$terms = get_terms( $taxonomy ); //Get all the terms
foreach ($terms as $term) { //Cycle through terms, one at a time
// Check and see if the term is a top-level parent. If so, display it.
$parent = $term->parent;
if ( $parent=='0' ) {
$term_id = $term->term_id; //Define the term ID
$term_link = get_term_link( $term, $taxonomy ); //Get the link to the archive page for that term
$term_name = $term->name;
//Main Category
echo '<a class="ccats" href="' . $term_link . '"><span class="label">' . $term_name . '</span></a><br/>';
$childargs = array('parent' => $term_id);
$childcategories = get_categories( $childargs );
foreach($childcategories as $childcategory) {
$childterm_id = $childcategory->term_id;
//Parent of Child Category
echo '<p>Category: <a href="' . get_category_link( $childcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $childcategory->name ) . '" ' . '>' . $childcategory->name.'</a> </p> ';
$subchildargs = array('parent' => $childterm_id);
$subchildcategories = get_categories( $subchildargs );
foreach($subchildcategories as $subchildcategory) {
//Parent of Child of Subchild Category
echo '<p>SubCategory: <a href="' . get_category_link( $subchildcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $subchildcategory->name ) . '" ' . '>' . $subchildcategory->name.'</a> </p> ';
}
}
}
}
?>
我需要在 Wordpress 循环中组合两件事。
首先,我想显示所有类别中每个 second-level 类别(示例中的 C1.1 和 C2.1)的类别名称。
那我想在每个second-level的分类标题下放一个table。它们的列数应与每个 second-level 类别的 third-levels(如示例中的 C1.1.1)一样多。在每一列中,我想显示该 third-level 类别的每个 post。
类别层次结构:
C1
— C1.1
— — C1.1.1
— — C1.1.2
— — C1.1.3
C2
— C2.1
— — C2.1.1
— — C2.1.2
...
这是一个例子:
C1.1
+——————————+——————————+——————————+
| C1.1.1 | C1.1.2 | C1.1.3 |
+——————————+——————————+——————————+
| Post | Post | Post |
| Post | Post | |
| | Post | |
+——————————+——————————+——————————+
C2.1
+——————————+——————————+
| C2.1.1 | C2.1.2 |
+——————————+——————————+
| Post | Post |
| Post | Post |
| Post | Post |
| | Post |
| | Post |
+——————————+——————————+
...
是否可以在自定义模板页面中执行此操作?
试试这个,
<?php
//Get All Categories like Parent, parent of child, child of subchild (Level 0 , 1, 2)
$taxonomy = 'category'; //Choose the taxonomy
$terms = get_terms( $taxonomy ); //Get all the terms
foreach ($terms as $term) { //Cycle through terms, one at a time
// Check and see if the term is a top-level parent. If so, display it.
$parent = $term->parent;
if ( $parent=='0' ) {
$term_id = $term->term_id; //Define the term ID
$term_link = get_term_link( $term, $taxonomy ); //Get the link to the archive page for that term
$term_name = $term->name;
//Main Category
echo '<a class="ccats" href="' . $term_link . '"><span class="label">' . $term_name . '</span></a><br/>';
$childargs = array('parent' => $term_id);
$childcategories = get_categories( $childargs );
foreach($childcategories as $childcategory) {
$childterm_id = $childcategory->term_id;
//Parent of Child Category
echo '<p>Category: <a href="' . get_category_link( $childcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $childcategory->name ) . '" ' . '>' . $childcategory->name.'</a> </p> ';
$subchildargs = array('parent' => $childterm_id);
$subchildcategories = get_categories( $subchildargs );
foreach($subchildcategories as $subchildcategory) {
//Parent of Child of Subchild Category
echo '<p>SubCategory: <a href="' . get_category_link( $subchildcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $subchildcategory->name ) . '" ' . '>' . $subchildcategory->name.'</a> </p> ';
}
}
}
}
?>