为什么 wp_query 让我的网站无限循环?
Why does wp_query make my website loop endlessly?
我有一个名为“actualites”的自定义 post 类型。
我想要 wp_query post 数据(现在是标题和内容)。
这是我使用的代码:
<div class="row">
<div class="col-sm-8 blog-main">
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
</div>
<!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul>
<?php endif; ?>
</div><!-- /.blog-sidebar -->
出于某种原因,这使我的网站无限循环。什么都没有出现,甚至 header 或页脚也没有。为什么会这样?这是 wp_query 部分的 100%。
我尝试了标准查询并得到了一个 "expected statement ",这可能是我遇到问题的原因。我现在正在尝试备用 wp 查询代码,但我不知道什么不起作用。有什么帮助吗?谢谢!
您的代码应如下所示。在您的代码中 $the_query
变量未定义。我认为这就是它不起作用的原因。开发的时候也要开启WP DEBUG MODE
。因此您将能够看到错误和警告。
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
我有一个名为“actualites”的自定义 post 类型。
我想要 wp_query post 数据(现在是标题和内容)。
这是我使用的代码:
<div class="row">
<div class="col-sm-8 blog-main">
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
</div>
<!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul>
<?php endif; ?>
</div><!-- /.blog-sidebar -->
出于某种原因,这使我的网站无限循环。什么都没有出现,甚至 header 或页脚也没有。为什么会这样?这是 wp_query 部分的 100%。
我尝试了标准查询并得到了一个 "expected statement ",这可能是我遇到问题的原因。我现在正在尝试备用 wp 查询代码,但我不知道什么不起作用。有什么帮助吗?谢谢!
您的代码应如下所示。在您的代码中 $the_query
变量未定义。我认为这就是它不起作用的原因。开发的时候也要开启WP DEBUG MODE
。因此您将能够看到错误和警告。
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>