get_the_title()返回上一循环的结果

时间:2015-04-09 17:20:22

标签: wordpress

我正在尝试循环思考自定义帖子类型(艺术家),然后是每个类别,然后是另一个与艺术家有关系的自定义帖子类型(故事)。

问题是,最后一个循环中的函数<?php echo get_the_title( $story->ID ); ?>多次返回艺术家的标题,以及当前自定义帖子的标题。我只需要currentntent loop post的标题。

<!-- get all the artists -->

 <?php

  $args = array(

    'post_type' => 'artists'

  );

    $query = new WP_QUERY( $args );

    ?>

    <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>

        <h1><?php the_title(); ?></h1>

        <p><?php the_field('artist_website'); ?></p> 

    <!--  looping through all the categories -->
            <?php

            $cats = get_categories(); 

                // loop through the categries
                foreach ($cats as $cat) {
                    // setup the cateogory ID
                    $cat_id= $cat->term_id;
                    // Make a header for the cateogry
                    echo "<h2>".$cat->name."</h2>";
                    ?>

                 <!--  looping through the stories that have a relationship with the artist (select_artist) -->
                    <?php
                        $post_type = 'story';

                    $args = array(
                            'post_type' => $post_type,
                            'posts_per_page' => -1,  //show all posts
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'category',
                                    'field' => 'slug',
                                    'terms' => $cat->slug,
                                )
                            ),
                            'meta_query' => array(
                                array(
                                    'key' => 'select_artist', // name of custom field
                                    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
                                    'compare' => 'LIKE'
                                )
                            )
                        );

                    $stories = new WP_Query($args);

                    ?>

                    <?php if( $stories ): ?>

                        <ul>
                        <?php foreach( $stories as $story ): ?>
                           <!-- Problem echo below -->
                            <?php echo get_the_title( $story->ID ); ?> 

                        <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>


                <?php } // done the foreach statement ?>

        <?php endwhile; wp_reset_postdata(); endif; ?>  

</section>

1 个答案:

答案 0 :(得分:1)

如果你正在使用WP_Query,你应该使用while循环并以the_post()结束,以便设置允许get_the_title()正常工作的内部变量。

作为一个额外的衡量标准,您可以在内循环之前将$ post变量设置为$ temp变量,然后在之后重置。

<?php
$stories = new WP_Query($args);

if( $stories->have_posts() ): 

global $post;
$temp = $post; 
?>

<ul>

    <?php 
    while( $stories->have_posts() ): $stories->the_post();

        the_title(); 

    endwhile;
    $post = $temp;
    ?>

<ul>

<?php endif; ?>