wordpress收费下载资源主题
wordpress付费下载资源插件

wordpress 修改自定义文章类型的固定链接结构

开发模板的过程中,难免会需要添加自定义文章类型,但是默认的链接结构都是类似forum/标题 这种结构,可能对搜索引擎不友好,所以我们需要做些修改,下面有两种方法。将代码贴进主题的functions.php里,然后更新下固定链接即可!

方法一:

add_filter('post_type_link', 'custom_blog_link', 1, 3);
function custom_blog_link( $link, $post = 0 ){
 if ( $post->post_type == 'blog' ){
 return home_url( 'blog/' . $post->ID .'.html' );
 } else {
 return $link;
 }
}
add_action( 'init', 'custom_blog_rewrites_init' );
function custom_blog_rewrites_init(){
 add_rewrite_rule(
 'blog/([0-9]+)?.html$',
 'index.php?post_type=blog&p=$matches[1]',
 'top' );
}

方法二:

add_action('init', 'custom_blog_rewrite');
function custom_blog_rewrite() {
 global $wp_rewrite;
 $queryarg = 'post_type=blog&p=';
 $wp_rewrite->add_rewrite_tag('%qid%', '([^/]+)', $queryarg);
 $wp_rewrite->add_permastruct('blog', '/blog/%qid%.html', false);
}
 
add_filter('post_type_link', 'custom_blog_permalink', 1, 3);
function custom_blog_permalink($post_link, $post = 0) {
 global $wp_rewrite;
 if ( $post->post_type == 'blog' ){
 $post = &get_post($id);
 if ( is_wp_error( $post ) )
 return $post;
 $newlink = $wp_rewrite->get_extra_permastruct('blog');
 $newlink = str_replace("%qid%", $post->ID, $newlink);
 $newlink = home_url(user_trailingslashit($newlink));
 return $newlink;
 } else {
 return $post_link;
 }
}

不过,以上两种方法都会出现一个问题,那就是发表评论后报404,所以模板兔在这里推荐使用插件来解决这个问题。

custom post type permalinks WordPress插件

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情