如何使用monthname创建永久链接而不是使用monthnum?

 超级神棍小虾米 发布于 2022-12-10 13:51

我想重定向我的博客文章,

http://www.example.com/blog/2014/september/03/post-name

但在wordpress中它只允许我使用月号,

http://www.example.com/blog/2014/09/03/post-name.

我正在寻找这个,但没有找到任何有用的东西.一些未答复的帖子,他们甚至没有说,是否有可能.即使在wordpress文档中也没有参考.我找到了以下代码,但它更改了网址但未链接帖子页面.

%monthcode% and %monthname% tag for Permalinks.
* Author: Roger Chen
* License: GPLv2
*/

/**
* Enables use of monthname (january, june) and monthcode (jan, jun).
* Supports permalinks in the form of /2016-nov/61742/..slug.. or /2016-november/61742/..slug..
*/
class MonthName {

/**
 * Month Names
 */
public static $monthnames = array(
    'january',
    'february',
    'march',
    'april',
    'may',
    'june',
    'july',
    'august',
    'september',
    'october',
    'november',
    'december',
);

/**
 * Month Codes
 */
public static $monthcodes = array(
    'jan',
    'feb',
    'mar',
    'apr',
    'may',
    'jun',
    'jul',
    'aug',
    'sep',
    'oct',
    'nov',
    'dec',
);

/**
 * Registers all required hooks
 */
public static function init() {
    add_rewrite_tag( '%monthname%', '(' . implode('|', self::$monthnames) . ')' );
    add_rewrite_tag( '%monthcode%', '(' . implode('|', self::$monthcodes) . ')' );
    add_rewrite_rule(
        '^([0-9]{4})-(' . implode( '|', self::$monthnames ) . ')/([0-9]+)/?',
        'index.php?p=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        '^([0-9]{4})-(' . implode( '|', self::$monthcodes ) . ')/([0-9]+)/?',
        'index.php?p=$matches[3]',
        'top'
    );
}
/**
 * Filters the month name and month code tags
 */
public static function filter_post_link( $permalink, $post ) {
    if ( false === strpos( $permalink, '%monthname%' ) && false === strpos( $permalink, '%monthcode%' ) ) {
        return $permalink;
    }

    try {
        $monthindex = intval(get_post_time( 'n', "GMT" == false, $post->ID ));

        $monthname = self::$monthnames[$monthindex - 1];
        $monthcode = self::$monthcodes[$monthindex - 1];

        $permalink = str_replace( '%monthname%', $monthname, $permalink );
        $permalink = str_replace( '%monthcode%', $monthcode, $permalink );

        return $permalink;
    } catch (Exception $e) {
        return $permalink;
    }
}

}

add_action( 'init', array( 'MonthName', 'init' ) );
add_filter( 'post_link', array( 'MonthName', 'filter_post_link' ), 10, 2 );

有人请说是否可能.如果可能的话,请您说出解决这个问题的方法.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有