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

前端之家收集整理的这篇文章主要介绍了php – 如何使用monthname创建永久链接而不是使用monthnum?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想重定向我的博客文章,

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

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

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

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

<?PHP
/**
* Plugin Name: Month Name
* Description: Enables the <code>%monthcode%</code> and <code>%monthname%</code> 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','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%',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]+)/?','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','filter_post_link' ),10,2 );

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

好的,这是代码.它目前支持以下格式的永久链接/ 2014 / nov / 23 / post-name或/ 2014/11月/ 23 / post-name
<?PHP
/**
* Plugin Name: Month Name Permalink
* Description: Enables use of <code>%monthcode%</code> or <code>%monthname%</code> tags in permalinks to generate a structure like <code>/2014/nov/23/post-name</code> or <code>/2014/november/23/post-name</code>
* Author: Anand Shah
* License: GPLv2
*/

/**
 * Based on the original code by Roger Chen (https://gist.github.com/rogerhub/8306875)
 * Plugin enables use of monthname (january,jun) in permalinks
 * Supports permalinks in the form of /2014/nov/23/post-name or /2014/november/23/post-name
*/

class Month_Name_Permalink {

/**
 * Month Names
 */
public static $monthnames = array(
    'january',self::$monthcodes) . ')' );
    add_rewrite_rule(
        '^([0-9]{4})/(' . implode( '|',self::$monthnames ) . ')/([0-9]{1,2})/(.*)?','index.PHP?name=$matches[4]','top'
    );
    add_rewrite_rule(
        '^([0-9]{4})/(' . implode( '|',self::$monthcodes ) . ')/([0-9]{1,'top'
    );       

}
/**
 * Filters the month name and month code tags
 */
public static function filter_post_link( $permalink,array( 'Month_Name_Permalink',2 );
原文链接:https://www.f2er.com/php/134455.html

猜你在找的PHP相关文章