php 如何从 WP_Post 对象获取标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18077278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get title from WP_Post Object
提问by sasi kanth
i was trying to see the list of child pages name with some description to display ..i use below code
我试图查看带有一些描述的子页面名称列表以显示..我使用下面的代码
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
// Get the page as an Object
$portfolio = get_page_by_title('service');
// Filter through all pages and find Portfolio's children
$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );
// echo what we get back from WP to the browser
echo "<pre>";print_r(
);
foreach($portfolio_children as $pagedet):
echo $pagedet['post_title'];
endforeach;
i am getting array before using foreach
我在使用 foreach 之前得到数组
when i print $portfolio_children iam getting out put like this
Array
(
[0] => WP_Post Object
(
[ID] => 201
[post_title] => Website Hosting
)
[1]=> WP_Post Object
(
[ID] => 202
[post_title] => Website
)
after foreach if i print $pagedet iam getting
在 foreach 之后,如果我打印 $pagedet 我得到
WP_Post Object
(
[ID] => 201
[post_title] => Website Hosting
)
i tried to call $pagedet['post_title'] but id does't display any thing ...thanks in advance
我试图调用 $pagedet['post_title'] 但 id 没有显示任何东西......提前致谢
回答by K?v?lc?m
Just to be sure, you should use every page data as column name.
可以肯定的是,您应该使用每个页面数据作为列名。
For instance,
例如,
$page_data->post_content //is true,
$page_data->the_title // is false.
回答by Ben Racicot
This is from my notes that I got working with your exact situation. Hope it helps.
这是从我的笔记中得出的,我正在处理您的确切情况。希望能帮助到你。
<?php
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));
$childpg = get_page_children(8, $all_wp_pages);
foreach($childpg as $children){
$page = $children->ID;
$page_data = get_page($page);
$content = $page_data->post_content;
$content = $page_data->the_title;
$content = apply_filters('the_content',$content);
$content = str_replace(']]>', ']]>', $content);
echo '<div class="row-fluid"><span class="span4">';
echo get_the_post_thumbnail( $page );
echo '</span><span class="span8">'.$content.'</span></div>';
}
?>
回答by Amir Iqbal
Try this one. Giving just idea.
试试这个。给只是想法。
<?php
$post = get_post($_GET['id']);
$post->post_title;
?>
回答by Jacob Robertson
Replace above foreach:
替换上面的foreach:
Take the array of objects ([0],[1],[2]...) and set (each) as singular instance of $pagedet.
取对象数组 ([0],[1],[2]...) 并将(每个)设置为 $pagedet 的单个实例。
foreach($portfolio_children as $pagedet) {
Now create variable $post_title to equal 'post_title' value of each object in array.
现在创建变量 $post_title 以等于数组中每个对象的 'post_title' 值。
$post_title = $pagedet->post_title;
This would technically work, but you want to loop through each instance programmatically without identifying each object in the array.
这在技术上是可行的,但您希望以编程方式遍历每个实例而不识别数组中的每个对象。
echo $pagedet[0]->post_title;
echo $pagedet[1]->post_title;
echo $pagedet[2]->post_title;
Now you can write out each post_title value:
现在您可以写出每个 post_title 值:
echo $post_title;
};
In summary
总之
foreach($portfolio_children as $pagedet) {
$post_title = $pagedet->post_title;
echo $post_title;
};