我有一个自定义帖子类型和自定义分类的问题。我创建了一个名为'galleries‘的自定义帖子类型和一个名为'gallery_ type’的分类法。我已经创建了一个名为Gallery的页面,我在其中加载了所有来自Gallery的帖子。然后我创建了“花园”和“房子”分类术语,并为每个“花园”和“房子”分配了3-3个帖子。我把“花园”和“房子”这两个分类术语作为子菜单链接到我当前的父级图库。我想用它们各自的术语来显示这些帖子。但是它没有加载。我能做什么??我的代码是:‘
$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???发布于 2013-08-02 19:41:47
尝试在分类法术语的名称上使用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');
}https://stackoverflow.com/questions/18015098
复制相似问题