将发布数据重置为嵌套循环中的上一个循环


21

我正在尝试对嵌套插件使用嵌套循环。循环都可以,但是问题是在第二个嵌套循环($ 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;
}

Answers:


20

我将自己回答这个问题,但是正是人民的非常聪明的@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;
}

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

这真的对您有用吗?
GDY

5

首先,我认为可以与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();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.