首页 文章

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

提问于
浏览
0

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

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

1 回答

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

相关问题