GET_THE_TITLE()返回上一个循环的结果

时间:2015-04-09 作者:benc

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

问题是,函数ID);?>在最后一个循环中,将多次返回艺术家的标题以及当前自定义帖子的标题。我只需要当前循环帖子的标题。

    <!-- 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; ?>  


**EDIT** var dump of $stories

object(WP_Query)#307 (46) { ["query"]=> array(4) { ["post_type"]=> string(5) "story" ["posts_per_page"]=> int(-1) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> string(12) "category-one" } } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } } } ["query_vars"]=> array(63) { ["post_type"]=> string(5) "story" ["posts_per_page"]=> int(-1) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> string(12) "category-one" } } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(12) "category-one" ["tag"]=> string(0) "" ["cat"]=> int(6) ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["nopaging"]=> bool(true) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" } ["tax_query"]=> object(WP_Tax_Query)#304 (6) { ["queries"]=> array(1) { [0]=> array(5) { ["taxonomy"]=> string(8) "category" ["terms"]=> array(1) { [0]=> string(12) "category-one" } ["field"]=> string(4) "slug" ["operator"]=> string(2) "IN" ["include_children"]=> bool(true) } } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(1) { [0]=> string(21) "wp_term_relationships" } ["queried_terms"]=> array(1) { ["category"]=> array(2) { ["terms"]=> array(1) { [0]=> string(12) "category-one" } ["field"]=> string(4) "slug" } } ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> object(WP_Meta_Query)#305 (7) { ["queries"]=> array(2) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } ["relation"]=> string(2) "OR" } ["relation"]=> string(3) "AND" ["meta_table"]=> string(11) "wp_postmeta" ["meta_id_column"]=> string(7) "post_id" ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" ["table_aliases":protected]=> array(1) { [0]=> string(11) "wp_postmeta" } } ["date_query"]=> bool(false) ["request"]=> string(525) "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = \'story\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND ( ( wp_postmeta.meta_key = \'select_artist\' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%\\"104\\"%\' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC " ["posts"]=> array(0) { } ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(true) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(true) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "6db7f7961e185d6e6411219e7317dd72" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL }

1 个回复
最合适的回答,由SO网友:DACrosby 整理而成

我认为,问题是if ($stories) 总是返回true,即使没有帖子。然后,它将遍历WP查询元素,而不是实际的帖子。请尝试以下操作:

if ( $stories->have_posts() ) {
  while ( $stories->have_posts() ) : $stories->the_post();

     echo get_the_title( get_the_ID() );

  endwhile; $query->reset_postdata();
}

结束

相关推荐

Iterate through ID's in loop

我已经基于category创建了一个自定义循环,现在我想运行一个函数,将当前帖子的特定ID作为参数进行迭代。我有。。$secondary_loop = new WP_Query(array( \'category_name\' => get_the_title(), \'posts_per_page\' => 5 )); while ( $secondary_loop->have_posts() ) : $secondary_loop->the_post();&#x