在 Wordpress 的标题中显示发布日期 [英] Display the post date in title in Wordpress

查看:43
本文介绍了在 Wordpress 的标题中显示发布日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WP 新手,我想更改门户上的标题显示,以使用过滤器在括号中显示发布日期?我该怎么做 ?当我尝试这个时(@Dre 的解决方案);我还可以使用顶部菜单约会:

I am very new in WP, I want to change the title display on the portal to show also the post date in brackets using a filter ? how can I do it ? when I try this (solution of @Dre) ; I get also date with the top menu:

function my_add_date_to_title($title, $id) {
    $date_format = get_option('date_format');
    $date = get_the_date($date_format, $id); // Should return a string
    return $title . ' (' . $date . ')';
}
add_filter('the_title','my_add_date_to_title',10,2);

推荐答案

您最好编辑页面模板以简单地输出日期;它更快,并且更明显,以后更容易找到.通过过滤器应用内容会使跟踪内容来自哪里变得更加困难.

You'd probably be better off editing your page templates to simply output the date; it's quicker and makes it much more obvious and easier to find later on. Apply content via filters can make it harder to tracker down where content is coming from.

话虽如此,如果您决定通过过滤器来实现,以下是您需要添加到您的 functions.php 文件中的内容:

Having said that, if you're determined to do it via filters, here's what you'd need to add to your functions.php file:

/* Adds date to end of title 
 * @uses Hooked to 'the_title' filter
 * @args $title(string) - Incoming title
 * @args $id(int) - The post ID
 */
function my_add_date_to_title($title, $id) {

    // Check if we're in the loop or not
    // This should exclude menu items
    if ( !is_admin() && in_the_loop() ) {

        // First get the default date format
        // Alternatively, you can specify your 
        // own date format instead
        $date_format = get_option('date_format');

        // Now get the date
        $date = get_the_date($date_format, $id); // Should return a string

        // Now put our string together and return it
        // You can of course tweak the markup here if you want
        $title .= ' (' . $date . ')';
     }

    // Now return the string
    return $title;
}

// Hook our function to the 'the_title' filter
// Note the last arg: we specify '2' because we want the filter
// to pass us both the title AND the ID to our function
add_filter('the_title','my_add_date_to_title',10,2);

未经测试,但应该可以工作.

Not tested, but should work.

这篇关于在 Wordpress 的标题中显示发布日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆