wordpress主题开发get_post()笔记

在主题开发中经常需要在非文章页面或者分类页面调用某一篇文章的标题,简介等信息,所以就需要单独得获取这些指定的文章,而文章的唯一的标记的就是该篇文章的ID,那如何获取文章的ID呢,在我的其他博文里面有“wordpress获取当前页面的ID:

 

根据ID获取文章内容需要用到get_post($id)方法:

 

$id 可以是对应的ID值,或者是变量,但最好是变量

 

$result = get_post($id)                                 

 

其返回值:$result 是一个对象,打印出来:

 

WP_Post Object
(

    [ID] => 344

    [post_author] => 1

    [post_date] => 2017-08-04 15:19:42

    [post_date_gmt] => 2017-08-04 07:19:42

    [post_content] => 页面内容

    [post_title] => 服务简介

    [post_excerpt] =>

    [post_status] => publish

    [comment_status] => closed

    [ping_status] => closed

    [post_password] =>

    [post_name] => 服务简介

    [to_ping] =>

    [pinged] =>

    [post_modified] => 2017-08-04 16:59:26

    [post_modified_gmt] => 2017-08-04 08:59:26

    [post_content_filtered] =>

    [post_parent] => 0

    [guid] => http://localhost/wordpress/?page_id=344

    [menu_order] => 0

    [post_type] => page

    [post_mime_type] =>

    [comment_count] => 0

    [filter] => raw

)

 

获取其信息 就可以用类似于 $result -> post_title 的方法,实现信息调用。


你可能感兴趣的:(wordpress,php)