WP_Query 导致 Wordpress 仪表板消失
WP_Query causing Wordpress dashboard to disappear
出于某种原因,在我创建新的 WP 查询对象后在 if 语句中使用 while 循环后,我的仪表板消失了。
<?php
$args = array(
'post_type' => 'info'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<section class="info">
<p class="quote"><?php the_field('quote'); ?></p>
<div class="text">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
</section>
<?php endwhile; endif; wp_reset(); ?>
删除 if 语句和 while 循环会导致仪表板再次出现。这似乎是正确的格式,我找不到其他人遇到同样问题的案例。
如果将最后一位 wp_reset();
更正为 wp_reset_postdata();
怎么样?
尝试 get_posts()
函数而不是 new WP_Query( );
。
类似的东西:
<?php
$args = array(
'post_type' => 'info'
);
$myposts = get_posts($args);
foreach ($myposts as $post) :
setup_postdata($post);
?>
<section class="info">
<p class="quote"><?php the_field('quote'); ?></p>
<div class="text">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
</section>
<?php endforeach;
wp_reset_postdata();
?>
希望对你有帮助。
出于某种原因,在我创建新的 WP 查询对象后在 if 语句中使用 while 循环后,我的仪表板消失了。
<?php
$args = array(
'post_type' => 'info'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<section class="info">
<p class="quote"><?php the_field('quote'); ?></p>
<div class="text">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
</section>
<?php endwhile; endif; wp_reset(); ?>
删除 if 语句和 while 循环会导致仪表板再次出现。这似乎是正确的格式,我找不到其他人遇到同样问题的案例。
如果将最后一位 wp_reset();
更正为 wp_reset_postdata();
怎么样?
尝试 get_posts()
函数而不是 new WP_Query( );
。
类似的东西:
<?php
$args = array(
'post_type' => 'info'
);
$myposts = get_posts($args);
foreach ($myposts as $post) :
setup_postdata($post);
?>
<section class="info">
<p class="quote"><?php the_field('quote'); ?></p>
<div class="text">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
</section>
<?php endforeach;
wp_reset_postdata();
?>
希望对你有帮助。