Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用monthname而不是使用monthnum创建永久链接?_Php_Wordpress_Permalinks - Fatal编程技术网

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

Php 如何使用monthname而不是使用monthnum创建永久链接?,php,wordpress,permalinks,Php,Wordpress,Permalinks,我想像这样重定向我的博客文章 但在wordpress中,它只允许我使用月数 我正在搜索这个,但没有找到任何有用的东西。一些未回复的帖子,他们甚至没有说,这是否可能。即使在wordpress文档中也没有这方面的参考。我找到了下面的代码,但它更改了url,但没有链接帖子页面 <?php /** * Plugin Name: Month Name * Description: Enables the <code>%monthcode%</code> and <c

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

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

我正在搜索这个,但没有找到任何有用的东西。一些未回复的帖子,他们甚至没有说,这是否可能。即使在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/11/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, 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 );

据我所知。这是不可能的,@质量专家哦!他们似乎错过了基本功能。是的,可能是。。由于wp仅支持monthnum,我认为它将与post_id一起工作,但不会与post_nameOk一起工作。在使用
wp_rewrite.php
进行了大量的抨击之后,并试图理解其中的黑魔法:)我已经找到了解决方案,如果你还感兴趣,请告诉我,我会发布答案。我很高兴能提供帮助:)@ana我正在使用这个插件,我也遇到了同样的问题。我刚刚将插件更新为最新版本,但仍然无法正常工作。我也试过上面的答案。请帮忙@伊鲁文克顿,你有没有试着重新保存永久林克斯结构?@anand,是的,很多次了@如果我使用自述文件中的permalink结构,它就会工作。但是,我需要在它前面有“博客/帖子”。当我将其更改为:“blog/posts/%year%/%monthname%/%day%/%postname%/”时,单个blog帖子会吸引整个blog。你知道为什么会这样吗?我非常感谢你的帮助!谢谢