You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取显示页面/文章标题及更新日期的WP短代码?

Custom Shortcodes for Page/Post Title and Modified Date

Hey there, this is totally doable! You don’t have to hunt for a plugin that checks all your boxes—you can build these two custom shortcodes yourself with just a bit of PHP. Here’s exactly how to set them up:

1. Shortcode for Current Page/Post Title

This shortcode pulls in the title of the page or post where you add it, and it’ll automatically update if you change the title later.

Add this code to your theme’s functions.php file (or better yet, a custom plugin to avoid losing it when you update your theme):

function get_current_page_post_title() {
    global $post;
    return get_the_title($post->ID);
}
add_shortcode('page-post-title', 'get_current_page_post_title');

Usage example:

1999年1月15日,[page-post-title] 发布了新产品。

2. Shortcode for Last Modified Date (with Custom Format)

This shortcode lets you specify a date format (using WordPress/PHP date format strings) and shows the last modified timestamp of the current page/post. It also includes a default format in case you don’t pass one.

Add this code alongside the previous one:

function get_current_page_post_modified_date($atts) {
    global $post;
    // Set default date format if no format is provided
    $atts = shortcode_atts(
        array(
            'format' => 'F j, Y', // Default format: e.g., January 15, 1999
        ),
        $atts,
        'page-post-modified-date'
    );
    return get_the_modified_date($atts['format'], $post->ID);
}
add_shortcode('page-post-modified-date', 'get_current_page_post_modified_date');

Usage examples:

本条目最后更新于 [page-post-modified-date "jS m Y"]。
或者用默认格式:本条目最后更新于 [page-post-modified-date]。

Quick Notes:

  • Always back up your functions.php file before editing it, or use a custom code snippets plugin to add these functions safely.
  • The date format uses standard PHP date characters—you can tweak the format string to match any style you need (e.g., Y-m-d for 1999-01-15).

If you run into any issues getting this set up, feel free to follow up with more details!

内容的提问来源于stack exchange,提问作者Gideon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:25:45