gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:56:44 26 4
gpt4 key购买 nike

我想像这样重定向我的博客文章,

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

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

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

我正在搜索这个但没有找到任何有用的东西。一些未回复的帖子,他们甚至没有说是否可能。即使在 wordpress 文档中也没有对此的引用。我找到了以下代码,但它更改了 url 但没有链接帖子页面。

<?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',
'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 );

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

最佳答案

好的,这是代码。它目前支持以下格式的永久链接 /2014/nov/23/post-name/2014/november/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, june) and monthcode (jan, 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',
'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]{1,2})/(.*)?',
'index.php?name=$matches[4]',
'top'
);
add_rewrite_rule(
'^([0-9]{4})/(' . implode( '|', self::$monthcodes ) . ')/([0-9]{1,2})/(.*)?',
'index.php?name=$matches[4]',
'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( 'Month_Name_Permalink', 'init' ) );
add_filter( 'post_link', array( 'Month_Name_Permalink', 'filter_post_link' ), 10, 2 );

关于php - 如何使用 monthname 而不是使用 monthnum 创建永久链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com