为WordPress设置固定链接 使搜索引擎更容易收录

 

Wordpress 固定链接形式:

wordpress 固定链接一共几种形式 我们到底应该用哪一个?

1、/%year%/%monthnum%/%day%/%postname%/

2、/%year%/%monthnum%/%postname%/

3、/%year%/%monthnum%/%day%/%postname%.html

4、/%year%/%monthnum%/%postname%.html

5、/%category%/%postname%.html

6、/%post_id%.html

7、/%post_id%/

8、/%postname%/

9、/%postname%.html

上网攻略了一番 大家常用的有两种

1.  /%post_id%.html 
2.  /%postname%.html

博主使用的是/%post_id%.html

【实现方法】

第一步:wordPress后台 设置-> 固定链接

20170627154529.png

第二步:向function.php中添加如下代码

/**
 * 设置多种自定义文章类型的固定链接结构为 ID.html
 * https://www.wpdaxue.com/custom-post-type-permalink-code.html
 */
$mytypes = array(//根据需要添加你的自定义文章类型
    'type1' => 'slug1',
    'type2' => 'slug2',
    'type3' => 'slug3'
    );
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
    global $mytypes;
    if ( in_array( $post->post_type,array_keys($mytypes) ) ){
        return home_url( $mytypes[$post->post_type].'/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
    global $mytypes;
    foreach( $mytypes as $k => $v ) {
        add_rewrite_rule(
            $v.'/([0-9]+)?.html$',
            'index.php?post_type='.$k.'&p=$matches[1]',
            'top'
            );
        add_rewrite_rule(
            $v.'/([0-9]+)?.html/comment-page-([0-9]{1,})$',
            'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]',
            'top'
            );
    }
}

第三步:检查主机是否支持伪静态

这里本博客用的是LNMP

vi /usr/local/nginx/conf/nginx.conf

在root /home/xxx/xxx;这一行下面添加:

include wordpress.conf;

wordpress.conf为伪静态文件
如需要其他伪静态文件 自己创建个并上传到/usr/local/nginx/conf/ 目录下

并include 伪静态.conf; 修改完保存

执行:

/etc/init.d/nginx restart 

重启生效

如果报错可能是添加有误或伪静态规则有误。

 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容