我正在尝试对嵌套插件使用嵌套循环。循环都可以,但是问题在第二个嵌套循环($ issue)之后出现。我想再次访问$ publication循环,但数据仍然是$ issue数据。

wp_reset_query()将在我不想的single.php中重置回到主循环。

我可以使用get_posts()而不是新的WP_Query,但我希望能够使用get_template_part()

如何将数据重置回发布循环,以便第二个“

这是我在single.php中的代码:

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

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

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}


#1 楼

我将自己回答这个问题,但是正是人民代码的非常聪明的@simonwheatley替我解决了这个问题。

除了使用wp_reset_postdata()wp_reset_query()之外,您还可以使用以下代码:

$publication->reset_postdata();


其中$ publication是您的查询对象。

现在的工作代码如下:

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

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

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile; $publication->reset_postdata();
    }

    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}


评论


确实,这是执行此操作的更聪明的方法。

–大卫
2013年12月13日上午10:16

这真的对您有用吗?

– GDY
18 Mar 6 '18 at 13:36



#2 楼

首先,我认为可以将get_posts()setup_postdata()结合使用。有了这些,您就可以像在普通的WordPress循环中一样使用模板标记。

但是您也可以在嵌套循环中使用此功能:

# make sure $post is the global in your scope (which should be the case in single.php)
global $post;
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    # preserve the current post in the higher loop
    $preserve_post = get_post();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

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

            // need to be able to use template parts in here
           echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    # set the global back to your first loop post
    $post = $preserve_post;
    setup_postdata( $post );
    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
wp_reset_query();