如何在页面模板中显示页面内容?
-
-
问题是什么?这是一个页面模板,因此您可以访问页面内容.例如,通过另一个单独的查询,您可以访问特定的帖子,因此可以输出其内容.所以?What is the problem? This is a page template, so you have access to the page content. By means of another separate query you gain access to a specific post, for instance, and thus can output its content. So?
- 2
- 2013-03-11
- tfrommen
-
请在投票之前耐心等待.我为此奋斗,然后找到了解决方案.我试图在这里进行问答,与其他人分享逻辑-我认为这将以我寻找的方式澄清事实.希望对您的问与答清楚.Please be patient before voting down. I's struggling for it and then I found the solution. I tried to Q&A here to share the logic with others - I think it will clarify the fact in a way I's looking for it. Hope the Q & A is clear to you.
- 0
- 2013-03-11
- Mayeenul Islam
-
首先,我没有**否决你的问题.其次,感谢您与我们分享您的知识.您这样做绝对是对的.我想,问题是这个_question_对于经验丰富的WP用户/开发人员来说并没有那么难解决,以及您单独发布问题的事实.如果您想从一开始就提出问题和答案,只需将您的答案/解决方案直接包含在您写问题的同一页上.在"发布您的问题"按钮下方,有一个复选框"回答您自己的问题".再次感谢.Firstly, I did **not** downvote your question. Secondly, thanks for sharing your knowledge with us. You're absolutely right to do so. I guess, the problem is/was that this _question_ was not that hard to solve for experienced WP users/developers, as well as the fact that you posted the question alone. If you want to question & answer right from the start, just include your answer/solution directly on the same page that you write your question on. Below the _Post Your Question_ button there is a check box **Answer your own question**. Thanks again.
- 0
- 2013-03-11
- tfrommen
-
wp_reset_postdata()进行救援.应该在每个自定义查询之后执行.`wp_reset_postdata()` for the rescue. Should be done _after each custom query_.
- 0
- 2013-03-11
- kaiser
-
2 个回答
- 投票数
-
- 2013-03-11
我正在使用两个循环.第一个循环是显示页面内容,第二个循环是显示查询的帖子内容.我在必要时对代码进行了评论.我强调了循环,如 Deckster0 说的在WordPress支持中,
the_content()
仅在WordPress循环内有效.我将这些代码放入自己的模板中:<?php /* * Template Name: My Template */ get_header(); ?> <div id="container"> <div id="content" class="pageContent"> <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title --> <?php // TO SHOW THE PAGE CONTENTS while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop --> <div class="entry-content-page"> <?php the_content(); ?> <!-- Page Content --> </div><!-- .entry-content-page --> <?php endwhile; //resetting the page loop wp_reset_query(); //resetting the page query ?> <?php // TO SHOW THE POST CONTENTS ?> <?php $my_query = new WP_Query( 'cat=1' ); // I used a category id 1 as an example ?> <?php if ( $my_query->have_posts() ) : ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Queried Post Title --> <div class="entry-content"> <?php the_excerpt(); ?> <!-- Queried Post Excerpts --> </div><!-- .entry-content --> <?php endwhile; //resetting the post loop ?> </div><!-- #post-<?php the_ID(); ?> --> <?php wp_reset_postdata(); //resetting the post query endif; ?> </div><!-- #content --> </div><!-- #container -->
I'm using two loops. First loop is to show the page content, and the second loop is to show the queried post contents. I commented into the codes where necessary. I emphasized into the loops, as Deckster0 said in WordPress support that,
the_content()
works only inside a WordPress Loop. I'm placing these code into a my own template:<?php /* * Template Name: My Template */ get_header(); ?> <div id="container"> <div id="content" class="pageContent"> <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title --> <?php // TO SHOW THE PAGE CONTENTS while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop --> <div class="entry-content-page"> <?php the_content(); ?> <!-- Page Content --> </div><!-- .entry-content-page --> <?php endwhile; //resetting the page loop wp_reset_query(); //resetting the page query ?> <?php // TO SHOW THE POST CONTENTS ?> <?php $my_query = new WP_Query( 'cat=1' ); // I used a category id 1 as an example ?> <?php if ( $my_query->have_posts() ) : ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Queried Post Title --> <div class="entry-content"> <?php the_excerpt(); ?> <!-- Queried Post Excerpts --> </div><!-- .entry-content --> <?php endwhile; //resetting the post loop ?> </div><!-- #post-<?php the_ID(); ?> --> <?php wp_reset_postdata(); //resetting the post query endif; ?> </div><!-- #content --> </div><!-- #container -->
-
第二个查询不应在if(have_posts())内部,因为该语句将始终为true.如果要检查查询是否有结果,应在$my_query=new WP_Query('cat=1');和args行之后调用if($my_query-> have_posts()).That second query shouldn't be inside `if( have_posts() )` because that statement will always be true. You should call `if( $my_query->have_posts() )` after the `$my_query = new WP_Query( 'cat=1' );` and args lines if you want to check that query has results.
- 0
- 2013-04-12
- t31os
-
@t31os你是对的.我的错.现在将代码更正为此类.感谢您的识别.:)@t31os you are right. It's my fault. Now corrected the code to such. Thanks for the identification. :)
- 0
- 2014-05-28
- Mayeenul Islam
-
- 2013-03-11
通常有两个循环,但是用药过量.
每个帖子或页面都为您提供超变
$post
.是否曾经想过为什么您的get_post_meta()
与简单的$post->ID
;)一起工作?因此,在启动获取列出的帖子的WP_Query()之前,可以使用
$post->ID
,$post->post_content
,$post->guid
等.在循环中,该变量被循环的帖子填充.要保存以供以后使用,您可以新建一个变量
$temp_post = $post // new WP_Query() + loop here
或致电
wp_reset_query()
.无论如何,都应调用最后一个函数,以确保侧边栏中的数据适合当前页面/帖子.
Two loops is common to do this, but a bit overdosed.
Every post or page gives you the super-variable
$post
. Ever wondered why yourget_post_meta()
works with a simple$post->ID
;) ?So, before you start the WP_Query() that gets your listed posts, you can access the current page-/post-data with
$post->ID
,$post->post_content
,$post->guid
and so on.In the loop, this variable gets filled by the looped post. To save it for later, you can either make a new variable
$temp_post = $post // new WP_Query() + loop here
or call
wp_reset_query()
after the listing. The last function should be called anyway to ensure that the data in your sidebar is the right for the current page/post.
在我的WordPress网站上,我制作了一个自定义页面模板,其中包含一个自定义查询(使用
WP_Query()
]).通过该查询,我可以完美地获取某个类别的帖子.但是我想显示页面内容以及查询的帖子.事情会像:
---------------------------
页面标题
页面内容
查询的帖子标题
查询帖子内容
---------------------------