Wordpress 常用函数汇总(持续更新)

82 阅读1分钟

1. 根据文章id获取当前文章内容 (单个文章)

get_post($id)

//返回结果如下
WP_Post Object
(
    [ID] => 1378
    [post_author] => 36
    [post_date] => 2023-09-13 07:25:53
    [post_date_gmt] => 2023-09-13 07:25:53
    [post_content] => 
    [post_title] => TV Interactive Whiteboard TV stands
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => tv-interactive-whiteboard-tv-stands
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2023-09-14 02:07:43
    [post_modified_gmt] => 2023-09-14 02:07:43
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => https://j691.goodao.net/?p=1378
    [menu_order] => 0
    [post_type] => post
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [format_content] => 
)

2. 根据文章id获取当前文章所有的分类

 get_the_category($id);

//返回结果如下
Array
(
    [0] => stdClass Object
        (
            [term_id] => 811
            [name] => 1 BAY
            [slug] => 1-bay
            [term_group] => 0
            [term_order] => 0
            [term_taxonomy_id] => 823
            [taxonomy] => category
            [description] => 
            [parent] => 806
            [count] => 1
            [cat_ID] => 811
            [category_count] => 1
            [category_description] => 
            [cat_name] => 1 BAY
            [category_nicename] => 1-bay
            [category_parent] => 806
        )

    [1] => stdClass Object
        (
            [term_id] => 820
            [name] => Deep Space Grey
            [slug] => %e6%b7%b1%e7%a9%ba%e7%81%b0
            [term_group] => 0
            [term_order] => 0
            [term_taxonomy_id] => 832
            [taxonomy] => category
            [description] => 
            [parent] => 808
            [count] => 1
            [cat_ID] => 820
            [category_count] => 1
            [category_description] => 
            [cat_name] => Deep Space Grey
            [category_nicename] => %e6%b7%b1%e7%a9%ba%e7%81%b0
            [category_parent] => 808
        )
)

3.获取当前页面的id

get_the_ID()