【问题标题】:Show loop content according to value, inside another loop根据值显示循环内容,在另一个循环内
【发布时间】:2014-11-05 17:13:34
【问题描述】:

场景:

我有一个帖子类型 portfolio 和一个自定义帖子类型 song,还有一个名为 songs_categories 的自定义分类。

作品集在loop 1内,歌曲在loop 2,在loop 1内。

我需要做的是显示组合中的歌曲,但前提是歌曲category 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 -->

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    只需修改您的 wp_query 以包含您想要的过滤器

    $args= array(
        'post_type'=>'song',
        'category_name'=>$post->post_name
    );
    
    $my_query = new WP_Query($args);
    

    Wp_query 非常详细,您应该阅读它以充分利用它。

    http://codex.wordpress.org/Class_Reference/WP_Query

    【讨论】:

    • 感谢您的意见。又翻了一遍手抄本,从头开始,终于搞明白了。
    【解决方案2】:

    通过修改我的查询得到了想要的结果:

    <?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();
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 2012-12-13
      • 1970-01-01
      • 2021-04-11
      • 2015-12-07
      相关资源
      最近更新 更多