我目前正在为一家我们的客户律师事务所建立一个网站。我们有两种自定义的帖子类型,律师和案例,我们需要在整个网站的各个区域将它们链接在一起。例如,当访问一个案件页面时,侧栏应该反映处理该案件的律师,每个律师页面应该显示他们处理过的案件。
我目前已经对其进行了设置,以便每次创建新案例时,都会在自定义分类法“案例日志”中创建一个条目。通过这种方式,只需进入配置文件后端并单击相关配置文件,即可为案件指派律师。我的目标的后半部分已经实现了这一点(在他们的个人资料中列出了工作过的案例),但还没有能够实现侧边栏列表。这是我正在尝试运行的查询。。。
<?php
// $valuec = get_query_var($wp_query->query_vars[\'case-log\']);
// echo get_term_by(\'slug\',$value,$wp_query->query_vars[\'case-log\']);
$terms = get_the_terms( $post->ID, \'case-log\' );
if ( !empty( $terms ) ){
// get the first term
$term = array_shift( $terms );
$casesearch = implode(",", $terms);
echo $casesearch;
echo $term->slug;
}
$args = array(
\'post_type\' => \'attorney\',
\'pagename\' => $casesearch
);
$case_attorney = new WP_Query( $args );
if ( $case_attorney->have_posts() ) :
?>
<?php while ( $case_attorney->have_posts() ) : $case_attorney->the_post() ?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
</div>
<?php endwhile ?>
<?php else : ?>
<h2>Ooops, no posts here!</h2>
<?php endif ?>
有了以上内容,我可以使用echo$term->slug;,打印出slug;,循环运行,但它只会吐出每个律师的个人资料名称,似乎跳过了第二个参数。
非常感谢您的帮助,谢谢!
更新:侧栏模板的完整代码。
<header class="caseh">Attorneys</header>
<div class="caseattorneys">
<?php
if( is_singular( "case" ) ) {
//We are in single "case" view
//Get the ID of current case post
$case_id = get_queried_object_id();
// Set "post_type" agument for the query
$args = array(
\'post_type\' => \'attorney\'
);
//Get the terms from case-log taxonomy
//get_the_terms returns an array of terms, false or a WP_Error object
$terms = get_the_terms( $case_id, \'case-log\' );
if( ! is_wp_error( $terms ) && $terms ) {
// Set "tax_query" agument for the query
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'case-log\',
\'terms\' => $terms
),
);
}
$case_attorney = new WP_Query( $args );
}
if ( $case_attorney->have_posts() ) :
?>
<?php while ( $case_attorney->have_posts() ) : $case_attorney->the_post() ?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
</div>
<?php endwhile ?>
<?php else : ?>
<h2>Ooops, no posts here!</h2>
<?php endif ?>
</div>
<div class="morefeaturedcases">
<h3>More Featured Cases</h3>
</div>
<a href="/contact-us/" class="ccu">Contact Us</a>
</aside>
最合适的回答,由SO网友:cybmeta 整理而成
pagename
用于请求页面(核心page
立柱类型)通过slug。如果我理解正确的话,你想要的是得到属于同一个组织的“律师”职位case-log
当前“案例”帖子中的术语。我会这样做(不确定您将在哪里执行代码,我已尝试尽可能普遍地编写代码):
if( is_singular( "case" ) ) {
//We are in single "case" view
//Get the ID of current case post
$case_id = get_queried_object_id();
// Set "post_type" agument for the query
$args = array(
\'post_type\' => \'attorney\'
);
//Get the terms from case-log taxonomy
//get_the_terms returns an array of term objects, false or a WP_Error object
$terms = get_the_terms( $case_id, \'case-log\' );
if( ! is_wp_error( $terms ) && $terms ) {
$terms_ids = array();
foreach( $terms as $term ) {
$terms_ids[] = $term->term_id;
}
// Set "tax_query" agument for the query
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'case-log\',
\'terms\' => $terms_ids
),
);
}
$case_attorney = new WP_Query( $args );
if ( $case_attorney->have_posts() ) {
while( $case_attorney->have_posts() ) {
$case_attorney->the_post();
?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
</div>
<?php
}
wp_reset_postdata();
} else {
echo "No matching attorney";
}
}