米扑博客,在用WordPress写文章时,遇到一个问题:

每次写好文章后,定时在将来某个时刻发表,但是预览文章时,发现文章链接为post_id,不是post_name

这就让我复制链接很纠结,本想复制自定义的post_name组成的文章链接,却往往是post_id

 

WordPress 4.8版本,定时文章预览的文章链接为 post_id ,如下图

https://blog.mimvp.com/?p=22213

 

修改成自定义的固定链接 post_name 形式,方法如下:

1) 打开文章模板 single.php 找到文章函数

vim single.php

找到文章函数

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to '); ?><?php the_title(); ?>"><?php the_title(); ?></a></h2>

 

2)查找函数 the_permalink()

vim wp-includes/link-template.php 

找到函数定义

function get_permalink( $post = 0, $leavename = false ) {
        $rewritecode = array(
                '%year%',
                '%monthnum%',
                '%day%',
                '%hour%',
                '%minute%',
                '%second%',
                $leavename? '' : '%postname%',
                '%post_id%',
                '%category%',
                '%author%',
                $leavename? '' : '%pagename%',
        );   

。。。。

//      if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
        if ( '' != $permalink ) {
                $unixtime = strtotime($post->post_date);

                $category = '';
                if ( strpos($permalink, '%category%') !== false ) {
                        $cats = get_the_category($post->ID);
                        if ( $cats ) {
                                $cats = wp_list_sort( $cats, array(
                                        'term_id' => 'ASC',
                                ) );

                                /**
                                 * Filters the category that gets used in the %category% permalink token.
                                 *
                                 * @since 3.5.0
                                 *
                                 * @param WP_Term  $cat  The category to use in the permalink.
                                 * @param array    $cats Array of all categories (WP_Term objects) associated with the post.
                                 * @param WP_Post  $post The post in question.
                                 */
                                $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

                                $category_object = get_term( $category_object, 'category' );
                                $category = $category_object->slug;
                                if ( $parent = $category_object->parent )
                                        $category = get_category_parents($parent, false, '/', true) . $category;
                        }
                        // show default category in permalinks, without
                        // having to assign it explicitly
                        if ( empty($category) ) {
                                $default_category = get_term( get_option( 'default_category' ), 'category' );
                                if ( $default_category && ! is_wp_error( $default_category ) ) {
                                        $category = $default_category->slug;
                                }
                        }
                }

                $author = '';
                if ( strpos($permalink, '%author%') !== false ) {
                        $authordata = get_userdata($post->post_author);
                        $author = $authordata->user_nicename;
                }

                $date = explode(" ",date('Y m d H i s', $unixtime));
                $rewritereplace =
                array(
                        $date[0],
                        $date[1],
                        $date[2],
                        $date[3],
                        $date[4],
                        $date[5],
                        $post->post_name,
                        $post->ID,
                        $category,
                        $author,
                        $post->post_name,
                );
                $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
                $permalink = user_trailingslashit($permalink, 'single');
        } else { // if they're not using the fancy permalink option
                $permalink = home_url('?p=' . $post->ID);
        }
        
	。。。。
}

 

3)注释掉判断语句

如上步骤2

注释掉

// if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {

增加一行判断

if ( '' != $permalink ) {

其代码判断意思已经很明了,自己看就好了

 

补充:

WordPress 4.2 版本判断条件如下

if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {

比 WordPress 4.8 版本少一个判断条件 'future' 也就是定时发布的链接,这也是激励我们的米扑博客研究源码的动力

 

4)验证修改效果

https://blog.mimvp.com/2018/04/wordpress-fa-biao-de-ding-shi-cao-gao-deng-wen-zhang-shi-yong-zi-ding-yi-gu-ding-lian-jie/

定时、草稿文章等预览链接,变为了自定义的固定链接格式,OK