希望有人可以帮助我解决这个问题 - 多年来我一直通过本网站的见解调试了很多代码。
我正在尝试在一个页面上运行两个wordpress查询,但由于某种原因,即使在使用wp_reset_postdata之后我也无法清除第一个查询中的数据。我究竟做错了什么?我应该尝试以其他方式进行查询吗?
我也尝试过wp_reset_query,但没有成功。
这是一个自定义主题 - 我需要在functions.php中使用某些东西才能使其工作吗? (我不得不在很久以前使用过缩略图)
这是代码:
$args1 = array( 'category' => 'locations' );
$myposts1 = get_posts( $args1 );
foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of Locations -->
<!-- second column-->
<div class="col-md-5">
<h2>Latest </h2>
<?php
$args2 = array( 'category' => 'secondcategory' );
$myposts2 = get_posts( $args2 );
foreach ( $myposts2 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of secondcatagory -->