admin管理员组文章数量:1429844
<?php
if(have_posts()){
while(have_posts()){
the_post();
echo get_the_title();
}
}
?>
this is my loop, really simple, im calling this within a custom page template i made called services, i expect the posts titles to pop up, but instead im just getting services.
<?php
if(have_posts()){
while(have_posts()){
the_post();
echo get_the_title();
}
}
?>
this is my loop, really simple, im calling this within a custom page template i made called services, i expect the posts titles to pop up, but instead im just getting services.
Share Improve this question asked Jun 2, 2019 at 20:03 EliEli 112 bronze badges 4 |1 Answer
Reset to default 2You are using the default page loop and it will output the current page attributes like title or other. You should create your own loop with custon query instead. See WP_Query or get_posts.
Example
$query = new WP_Query(array(
'post_type' => 'post',
'posta_status' => 'publish',
));
if($query->have_posts()){
while($query->have_posts()){
$query->the_post();
echo get_the_title();
}
wp_reset_postdata();
}
本文标签: phpthetitle gives me the page39s title not the post title in the following code
版权声明:本文标题:php - the_title gives me the page's title not the post title in the following code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745442849a2658524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_the_post()
it's going to give the page title. – rudtek Commented Jun 2, 2019 at 20:44get_post()
etc work on the global post object when no post is specified in the first parameter. When you're callinghave_posts()
,the_post()
, etc, you're actually asking WordPress to use this global post. In the context of WordPress, Pages, Posts, and Custom Post Types are all "posts", just of different types. So,have_posts()
istrue
because the page you're on is a post, just a post with thepost_type
ofpage
. – phatskat Commented Jun 3, 2019 at 16:21