繁体   English   中英

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

[英]Wordpress Custom Post Type and Custom Taxonomy

我在自定义帖子类型和自定义分类法上遇到问题。 我创建了一个名为“ galleries”的自定义帖子类型和一个名为“ gallery_type”的分类法。 我创建了一个名为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???

尝试在分类术语的名称上使用段塞。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM