Wordpress自定义帖子类型和自定义分类标准

问题描述:

我遇到自定义帖子类型和自定义分类标准的问题。我创建了一个名为“画廊”的自定义帖子类型和名为“gallery_type”的分类法。我创建了一个名为Gallery的页面,其中我加载了来自画廊的所有帖子。然后,我创建了'花园'&'house'分类术语,并为每个'garden'&'house'分配了3-3个帖子。我把分类术语“花园”和“房子”作为子菜单链接到我当前的父母。我想按他们各自的条款显示帖子。但它没有加载。我能做什么 ?? 我的代码是:“Wordpress自定义帖子类型和自定义分类标准

$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??? 

尝试使用,而不是塞在分类项的名称。

$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'); 
    }