我遇到自定义帖子类型和自定义分类的问题。我创建了一个名为'画廊的自定义帖子类型'和分类名称' gallery_type'。我创建了一个名为Gallery的页面,其中我加载了来自画廊的所有帖子。然后我创造了' garden' &安培; '的房子'分类术语和分配3-3个帖子到每个花园' &安培; '房子&#39 ;.我将分类学术语与“花园”相关联。和' house'作为画廊的子菜单,这是我当前的父母。我想以各自的条款显示帖子。但它没有加载。我能做什么 ?? 我的代码是:'
$tax = 'gallery_type';
$terms = get_the_terms($post->ID, $tax);
if($terms){
foreach($terms as $term){
$single = $term->name;
?>
<?php
$args = array('post_type'=>'galleries',
'posts_per_page'=> -1,
'tax_query'=>array(
array('taxonomy'=>'gallery_type',
'terms'=> $single)
),
'order_by'=>'post_date');
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()):
$query->the_post();
$post_id = $post->ID;
?>
<li class="thumb <?php echo $post_id; ?>" onclick="changeContent(<?php echo $post_id; ?>);">
<?php if(has_post_thumbnail())
the_post_thumbnail('gallery-thumb');
?>
<?php
endwhile;
wp_reset_query();
}
else{
_e('No image found in gallery');
}
'
Any fix???
答案 0 :(得分:0)
尝试使用slug代替分类术语的名称。
$tax = 'gallery_type';
$terms = get_the_terms($post->ID, $tax);
if($terms){
foreach($terms as $term){
$single = $term->slug;
// Also instead of this query for the taxonomy, as you are on the taxonomy page, you should use $single = $wp_query->query_vars['taxonomy_name']; <- This is the slug of the current viewed taxonomy
?>
<?php
$args = array('post_type'=>'galleries',
'posts_per_page'=> -1,
'tax_query'=>array(
array('taxonomy'=>'gallery_type','field' => 'slug',
'terms'=> $single)
),
'order_by'=>'post_date');
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()):
$query->the_post();
$post_id = $post->ID;
?>
<li class="thumb <?php echo $post_id; ?>" onclick="changeContent(<?php echo $post_id; ?>);">
<?php if(has_post_thumbnail())
the_post_thumbnail('gallery-thumb');
?>
<?php
endwhile;
wp_reset_query();
}
else{
_e('No image found in gallery');
}