【问题标题】:WordPress have_posts returns false but should return trueWordPress has_posts 返回 false 但应该返回 true
【发布时间】:2014-10-21 09:41:58
【问题描述】:

在我拥有的 WordPress 主题中,当完成联系表单时,会在页面上写入一条消息,但不是在我想要的位置。但是当在 PHP 代码中更改消息的位置时,与联系表单(写在文章中)一起出现的段落不再出现。其实我认为have_posts方法返回false,所以php_content方法没有被调用。

这是原始代码:

<div id="main" class="site-main container_16">
        <div class="inner">
            <div id="primary" class="grid_11 suffix_1">

            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <?php the_content(); ?>
                <?php endwhile; // end of the loop. ?>

                <?php if(isset($emailSent) && $emailSent == true) { ?>
                    <div class="alert green">
                        <?php _e( 'Thanks, your email was sent successfully.', 'wplook' ); ?>
                    </div>
                <?php } else { ?>
                    <?php if(isset($hasError) ) { ?>
                        <p class="error"><?php _e( 'Sorry, an error occured.', 'wplook' ); ?><p>
                    <?php } ?>

                    <form action="<?php the_permalink(); ?>" id="contact-form" method="post"> ...

这就是我想要的样子:

<div id="main" class="site-main container_16">
        <div class="inner">
            <div id="primary" class="grid_11 suffix_1">

                <?php if(isset($emailSent) && $emailSent == true) { ?>
                    <div class="alert green">
                        <?php _e( 'Thanks, your email was sent successfully.', 'wplook' ); ?>
                    </div>
                <?php } else { ?>
                    <?php if(isset($hasError) ) { ?>
                        <p class="error"><?php _e( 'Sorry, an error occured.', 'wplook' ); ?><p>
                    <?php } ?>

            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <?php the_content(); ?>
                <?php endwhile; // end of the loop. ?>

                    <form action="<?php the_permalink(); ?>" id="contact-form" method="post">...

如您所见,我只更改了确认消息的位置。但是在第二种方式中,have_posts 的条件似乎不成立,因为我在文章中写的文字没有显示出来。

有人可以帮我解决这个问题吗?

非常感谢!

编辑:

var_dump($wp_query) 显示:

object(WP_Query)#195 (49) { ["query"]=> array(2) { ["page"]=> string(0) "" ["pagename"]=> string(22) " accueil/nous-rejoindre" } ["query_vars"]=> array(62) { ["page"]=> int(0) ["pagename"]=> string(14) "nous-rejoindre" ["error" ]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost" ]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name" ]=> string(14) "nous-rejoindre" ["static"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" [ "minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year "]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"] => string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed "]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["cmets_popup"]=> string(0) "" ["meta_key "]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string( 0) "" ["category__in"]=> 数组(0) { } ["category__not_in"]=> 数组(0) { } ["category__and"]=> 数组(0) { } ["post__in"]=>数组(0) { } ["post__not_in"]=> 数组(0) { } ["tag__in"]=> 数组(0) { } ["tag__not_in"]=> 数组(0) { } ["tag__and"] => array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in "]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters "]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string( 0) "" ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["cmets_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false ) ["order"]=> string(4) "DESC" } ["tax_query"]=> NULL ["meta_query"]=> object(WP_Meta_Query)#75 (2) { ["qu eries"]=> array(0) { } ["relation"]=> NULL } ["date_query"]=> bool(false) ["queried_object"]=> object(WP_Post)#74 (24) { [" ID"]=> int(28) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-07-24 12:51:54" ["post_date_gmt" ]=> string(19) "2014-07-24 11:51:54" ["post_content"]=> string(1257) "Rejoindre Win France c'est...[我的页面内容]" ["post_title "]=> string(14) "Nous rejoindre" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "close" ["post_password"]=> string(0) "" ["post_name"]=> string(14) "nous-rejoindre" ["to_ping" ]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-07-26 18:06:47" ["post_modified_gmt"] => string(19) "2014-07-26 17:06:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(8) ["guid"]=> string (41) "http://www.win-france.org/win/?page_id=28" ["menu_order"]=> int(0) ["post_type"]=> string(4) "page" ["post_mime_type"]=> string(0) "" ["comment_count" ]=> 秒字符串(1)“0”[“过滤器”]=>字符串(3)“原始”}[“查询对象ID”]=>整数(28)[“请求”]=>字符串(172)“选择winfrance_posts。* FROM winfrance_posts WHERE 1=1 AND (winfrance_posts.ID = '28') AND winfrance_posts.post_type = 'page' ORDER BY winfrance_posts.post_date DESC " ["posts"]=> &array(1) { [0]=> object(WP_Post )#73 (24) { ["ID"]=> int(28) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-07-24 12 :51:54" ["post_date_gmt"]=> string(19) "2014-07-24 11:51:54" ["post_content"]=> string(1257) "Rejoindre WiN France c'est...[我的页面内容]" ["post_title"]=> string(14) "Nous rejoindre" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["评论状态"]=> 字符串(4) "打开" ["ping_status"]=> 字符串(6) "关闭" ["post_password"]=> 字符串(0) "" ["post_name"]=> 字符串(14) "nous-rejoindre" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-07-26 18: 06:47" ["post_modified_gmt"]=> 字符串(19) "2014-07-26 17:06:47" ["post_ content_filtered"]=> string(0) "" ["post_parent"]=> int(8) ["guid"]=> string(41) "[我的页面链接]" ["menu_order"]=> int( 0) ["post_type"]=> string(4) "page" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } } ["post_count"]=> int(1) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["post"]=> object(WP_Post)#73 (24) { ["ID"]=> int(28) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-07 -24 12:51:54" ["post_date_gmt"]=> string(19) "2014-07-24 11:51:54" ["post_content"]=> string(1257) "Rejoindre Win France c'est. ..[我的页面内容]" ["post_title"]=> string(14) "Nous rejoindre" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish " ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string (14)“nous-rejoindre”[“to_ping”]=>字符串(0)“”[“pinged”]=>字符串(0)“”[“post_modified”]=>字符串(19)“2014-07- 26 18:06:47" ["post_modified _gmt"]=> string(19) "2014-07-26 17:06:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(8) ["guid"] => string(41) "http://www.win-france.org/win/?page_id=28" ["menu_order"]=> int(0) ["post_type"]=> string(4) "page" ["post_mime_type"]=> string(0) "" [ "comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } ["comment_count"]=> int(0) ["current_comment"]=> int(-1 ) ["found_posts"]=> int(1) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"] => bool(false) ["is_page"]=> bool(true) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]= > bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) [ "is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_cmets_popup"]=>布尔(假)[“是_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(true) ["is_robots"]=> bool (false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> string(32) "763460f55e15fb6827b2ee25a60c558c" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL }

【问题讨论】:

  • 这可能无关,但我注意到您的表单标签有些奇怪:the_permalink 应该在循环内部调用,但您的却发生在外部。在呈现表单的页面上,表单的action属性的值是多少?
  • 表单的action属性的值是包含表单的页面的URL。这可能是个问题吗?
  • 有人有其他想法吗?
  • 尝试在 ifs 之前添加这个 var_dump($wp_query); 并将结果添加到问题中(或某个代码托管站点的链接)
  • 完成。未显示的文本内容在 ["post_content"]...

标签: php wordpress


【解决方案1】:

hiii,我尝试了你的两个代码,它正在提供帖子内容

我尝试了以下代码,

<div id="main" class="site-main container_16">
        <div class="inner">
            <div id="primary" class="grid_11 suffix_1">

                <?php if(isset($emailSent) && $emailSent == true) { ?>
                    <div class="alert green">
                        <?php _e( 'Thanks, your email was sent successfully.', 'wplook' ); ?>
                    </div>
                <?php } else { ?>
                    <?php if(isset($hasError) ) { ?>
                        <p class="error"><?php _e( 'Sorry, an error occured.', 'wplook' ); ?><p>
                    <?php } ?>
              <?php } ?>

            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <?php the_content(); ?>
                <?php endwhile; // end of the loop. ?>

                    </div>
        </div>
        </div>

【讨论】:

  • 谢谢! else 条件确实没有关闭......愚蠢的事情!
猜你喜欢
  • 2020-02-12
  • 2016-04-16
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多