始终将其中一个分类显示为默认分类

时间:2011-12-12 作者:Kalle H. Väravas

我有一个自定义的帖子类型staff 具有自定义分类法的departments. 我希望员工的首页(archive staff.php)不显示所有员工,而是显示第一个部门(假设有3个部门)

我的存档页的开头如下:

global $wp_query;
$args = array_merge( $wp_query->query, array(
    \'post_type\' => \'staff\',
) );
query_posts( $args );

while (have_posts()): the_post(); // ...
我不知道从这里该怎么走。我还需要显示当前部门标题。

2 个回复
最合适的回答,由SO网友:Kalle H. Väravas 整理而成

这就是我的工作原理:

// Custom taxonomy
$custom_taxonomy = \'department\';

// Get the current department
if (!$current_department = $wp_query->query_vars[$custom_taxonomy]) {
    $get_all_departments = get_terms($custom_taxonomy, array(
        \'number\' => 1
    ));
    $current_department = $get_all_departments[0]->slug;
}

// Get the departments information
$department = get_term_by(\'slug\', $current_department, $custom_taxonomy);
所以$current_department 是当前分类法的鼻涕虫$department 是当前分类法的数组。

SO网友:kaiser
$tax_slug = \'departments\';
$tax_term = \'first-department\';
// get taxonomy by slug and term
$tax_args = array( $tax_slug => $tax_term );
// merge your args, with the tax args
$args = array_merge( $args, $tax_args );

// Get term object
$term_title = get_term_by( $tax_term, \'\', $tax_slug );

// Display Title/Headline
echo "<h2>{$term_title->name}</h2>";
结束

相关推荐