我希望所有这些都能在这个范畴内完成。php模板,函数帮助。php(如果需要)。
如果我的设置为:
Parent 1
--Child 1
--Child 2
Parent 2
--Child 3
--Child 4
Parent 3
--Child 5
--Child 6
。。。因此,奥尼希望能够做到这一点:
如果我正在查看父1(或2/3),它将显示子1和子2。但是,我不想使用wp\\u list\\u类别,因为对于每个孩子,我想显示缩略图(来自插件函数)、孩子的名字和类别描述。
如果我正在查看Child 1(或2/3/4/5/6),它会显示使用标准循环的最新2篇帖子。
我希望避免使用带有类别特定名称的硬编码连续语,以防将来添加新名称。
我们将不胜感激。非常感谢。
最合适的回答,由SO网友:Arkuen 整理而成
由于wp上的nofearinc,问题得以解决。组织irc聊天!:)
https://gist.github.com/mpeshev/cb1cd3c56d06017b3d87
<?php
$cat_id = get_query_var(\'cat\');
if( ! empty( $cat_id ) ) {
$category = get_category( $cat_id, ARRAY_A );
if( ! empty( $category ) ) {
// parent category
if( $category[\'parent\'] === \'0\' ) {
// get child IDs
$terms = get_term_children( $cat_id, \'category\' );
foreach( $terms as $term_id ) {
$child_category = get_term( $term_id, \'category\' );
// if you need the category
$category_name = $child_category->name;
/**
* Sample data in your child category object:
*
* object(stdClass)[365]
public \'term_id\' => string \'14\' (length=2)
public \'name\' => string \'Child 1\' (length=7)
public \'slug\' => string \'child-1\' (length=7)
public \'term_group\' => string \'0\' (length=1)
public \'term_taxonomy_id\' => string \'14\' (length=2)
public \'taxonomy\' => string \'category\' (length=8)
public \'description\' => string \'\' (length=0)
public \'parent\' => string \'12\' (length=2)
public \'count\' => string \'5\' (length=1)
public \'object_id\' => string \'33\' (length=2)
*
*
*/
// do whatever you like with the categories now...
}
} else { // in child category
// do the regular loop here, if ( have_posts() ) ...
}
}
}
SO网友:Edward Sztukowski
我想说,上面的答案对我很有用,但我需要对if语句做一些轻微的调整。我也调整了工作方式,比如。。。
如果帖子类别是父级,则显示子级
如果帖子类别是父类别但没有子类别,则显示帖子
<?php
$cat_id = get_query_var(\'cat\');
if( ! empty( $cat_id ) ) {
$category = get_category( $cat_id, ARRAY_A );
if( ! empty( $category ) ) {
//get child cats
$terms = get_term_children( $cat_id, \'category\' );
// parent category
if( $category[\'parent\'] == \'0\' and $terms[0] != \'\') {
foreach( $terms as $term_id ) {
$child_category = get_term( $term_id, \'category\' );
// if you need the category
$category_name = $child_category->name;
/**
* Sample data in your child category object:
*
* object(stdClass)[365]
public \'term_id\' => string \'14\' (length=2)
public \'name\' => string \'Child 1\' (length=7)
public \'slug\' => string \'child-1\' (length=7)
public \'term_group\' => string \'0\' (length=1)
public \'term_taxonomy_id\' => string \'14\' (length=2)
public \'taxonomy\' => string \'category\' (length=8)
public \'description\' => string \'\' (length=0)
public \'parent\' => string \'12\' (length=2)
public \'count\' => string \'5\' (length=1)
public \'object_id\' => string \'33\' (length=2)
*
*
*/
// do whatever you like with the categories now...
}
}
else { // in child category
// do the regular loop here, if ( have_posts() ) ...
}
}
}