更改您的$args
稍微排列一点:
$args = array(
\'post_type\' => array( \'my_projects\' ), // it need to get the posts from this post_type
\'order\' => \'DESC\',
\'posts_per_page\' => 5,
\'project_tags\' => $term // search taxonomy
);
可以找到搜索分类术语的其他方法
here 在WordPress Codex页面中。
回答您的问题update:
您可以添加返回的支票false
当函数“不能”返回值时:
// if the given category doesn\'t exist
if ( ! is_category( $term ) )
return false;
// if the given category doen\'t have posts
if ( ! $loop->have_posts() )
return false;
设置代码以检查是否存在相关项目的方式将无法像现在设置函数的方式那样工作,请尝试以下操作:
function get_related_projects( $term ) {
// if the given category doesn\'t exist
if ( ! is_category( $term ) )
return false;
$return = \'\';
$return .= \'<ul class="featured-items">\';
$args = array(
\'post_type\' => array( \'my_projects\' ), // it need to get the posts from this post_type
\'order\' => \'DESC\',
\'posts_per_page\' => 5,
\'project_tags\' => $term // search taxonomy
);
$loop = new WP_Query( $args );
// if the given category doen\'t have posts
if ( ! $loop->have_posts() )
return false;
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
$figure = \'\';
if ( has_post_thumbnail()) :
$figure = sprintf( \'<a href="%1$s" title="%2$s">%3$s</a>\',
get_permalink( get_the_ID() ),
the_title_attribute( array( \'echo\' => false ) ),
get_the_post_thumbnail( get_the_ID() )
);
endif;
$return .= sprintf( \'
<li>
<figure class="project-image">%1$s</figure>
<h1 class="title"><a href="%2$s"><span class="goto">a</span> %3$s</a></h1>
</li>\',
$figure,
get_permalink( get_the_ID() ),
get_the_title()
);
endwhile;
wp_reset_postdata();
$return .= \'</ul>\'
return $return;
}
然后使用以下代码检查功能:
<?php if ( $projects = get_related_projects( "Car" ) ): ?>
<em>Related Projects</em>
<?php echo $projects; ?>
<?php endif; ?>