doujiu3095 2014-11-05 17:13
浏览 36
已采纳

在另一个循环内根据值显示循环内容

Scenario:

I have a post type portfolio and a custom post type song, and also a custom taxonomy called songs_categories.

Portfolio is inside loop 1 and the songs are in loop 2, which is inside loop 1.

What I need to do is to show the songs in the portfolio but only if the songs category slug (album name) has the actual value of the portfolios post_name (album name).

I can get the values needed but I'm having trouble writing the actual function necessary to do the filtering. Any help is appreciated.

<!-- 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 -->
  • 写回答

2条回答 默认 最新

  • dongyong5255 2014-11-06 17:50
    关注

    Got the desired result by modifying my query:

    <?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();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器