Php 根据值在另一个循环内显示循环内容
场景: 我有一个post-typePhp 根据值在另一个循环内显示循环内容,php,wordpress,loops,Php,Wordpress,Loops,场景: 我有一个post-typeportfolio和一个自定义post-typesong,还有一个名为songs\u categories的自定义分类法 公文包在循环1内,歌曲在循环2内,即在循环1内 我需要做的是显示组合中的歌曲,但前提是歌曲类别slug(唱片集名称)具有组合post_name(唱片集名称)的实际值 我可以得到所需的值,但是我在编写过滤所需的实际函数时遇到了困难。感谢您的帮助 <!-- Loop 1 --> <div class="songs">
portfolio
和一个自定义post-typesong
,还有一个名为songs\u categories
的自定义分类法
公文包在循环1内,歌曲在循环2内,即在循环1内
我需要做的是显示组合中的歌曲,但前提是歌曲类别slug
(唱片集名称)具有组合post_name
(唱片集名称)的实际值
我可以得到所需的值,但是我在编写过滤所需的实际函数时遇到了困难。感谢您的帮助
<!-- Loop 1 -->
<div class="songs">
<?php
$var1 = $post->post_name;
$my_query = new WP_Query('post_type=song' );
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<?php $terms = get_the_terms( $post->ID , 'songs_categories' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'songs_categories' );
if( is_wp_error( $term_link ) )
continue;
echo "$var1";
echo "$term->slug";
} ?>
<div class="col one">
<a href="<?php echo get_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
h2><?php the_title(); ?></h2>
<?php
$autor_name = get_post_meta($post->ID, "_cmb_autor_text", false);
if ($autor_name[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<?php foreach($autor_name as $autor_name) {
echo '<p>'.$autor_name.'</p>';
} ?>
<?php } ?>
</div>
<?php endwhile; ?> <!-- End 2nd loop -->
</div> <!-- End songs -->
<?php endif; endwhile; ?>
<?php endif; // password check ?> <!-- End 1st loop -->
h2>
只需修改wp\u查询以包含所需的过滤器即可
$args= array(
'post_type'=>'song',
'category_name'=>$post->post_name
);
$my_query = new WP_Query($args);
Wp_查询非常详细,您应该仔细阅读,以便从中获得最大的收益
通过修改我的查询获得了所需的结果:
<?php
$var1 = $post->post_name;
$args = array(
'post_type' => 'song',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'songs_categories',
'field' => 'slug',
'terms' => $var1,
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
?>
感谢您的输入。我又看了一遍法典,从头开始,最后我终于明白了。