【发布时间】:2017-03-20 08:54:15
【问题描述】:
由于我是 PHP 新手,有人可以帮我解决这个问题吗?
我有一个包含分类和术语的自定义类别: IE 主画廊(自定义类别) - 画廊一(分类1) - 专辑一(第一学期) - 专辑二(第二期) - 画廊二(分类 1)
我想显示专辑 1(第 1 学期)中的所有帖子。
到目前为止,我有这个代码:slug 名称在前端打印,但不用于返回帖子,是这个位吗'terms' => array_shift( $terms ) ??
如果我在数组中指定术语名称,我可以让它工作,但我需要它从页面 slug 中读取。
如前所述,我对 PHP 很陌生,可能在某处有结构错误,做了一些事情来打乱循环或类似的事情。非常感谢任何帮助。
代码:
<?php
$terms = get_the_terms( $post->ID, 'pubgal' ); // get the term $term = array_shift( $terms );
echo $term->slug;
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'pubgal',
'field' => 'slug',
'terms' => array_shift( $terms )
),
),
'post_type' => 'gallery'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$term = $query->queried_object;
while ( $query->have_posts() ) :
$query->the_post();
the_title();
the_content();
the_post_thumbnail();
endwhile;
}
//RESET YOUR QUERY VARS
wp_reset_query();
?>
【问题讨论】:
-
代码:ID, 'pubgal'); // 获取术语 $term = array_shift( $terms );回声$术语->蛞蝓; $args = array('tax_query' => array(array('taxonomy' => 'pubgal', 'field' => 'slug', 'terms' => array_shift($terms)),), 'post_type' = >“画廊”); $query = new WP_Query($args); if ( $query->have_posts() ) { $term = $query->queried_object;而 ( $query->have_posts() ) : $query->the_post();标题();内容(); the_post_thumbnail();结束; } //重置你的查询变量 wp_reset_query(); ?>
标签: php wordpress custom-wordpress-pages