如何通过模板更改自定义帖子类型、博客帖子和页面固定链接?

时间:2018-02-09 作者:roest

具有add_permastruct 我可以更改自定义帖子类型的url。

更改permalink_structure 通过管理界面的选项与with_front => false 对于我的自定义帖子类型。

如何通过代码更改博客帖子的URL
add_permastruct 未更改帖子类型的URLpostpage.

add_action( \'init\', [ $this, \'rewriteUrls\' ], 11 );

function rewriteUrls() {
        global $wp_rewrite;
        $version = \'/product/%product%/version/%version%\';
        $wp_rewrite->add_rewrite_tag( "%product%", \'([^/]+)\', "product=" );
        $wp_rewrite->add_rewrite_tag( "%version%", \'([^/]+)\', "version=" );
        $wp_rewrite->add_permastruct( \'version\', $version, false );

        $news = \'/product/%product%/news/%postname%\';
        $wp_rewrite->add_permastruct( \'post\', $news, false );
    }

1 个回复
SO网友:roest

修改post 与修改不同的工作page 然后修改custom post type permalink。下面介绍如何仅在模板代码中修改所有这些内容,并为整个页面提供自定义url结构。

修改自定义帖子类型permalink

add_action( \'init\', \'rewriteCustomPostUrls\' );

function rewriteUrls() {
    global $wp_rewrite;

    $version = \'/product/%product%/version/%version%\';
    $wp_rewrite->add_rewrite_tag( "%product%", \'([^/]+)\', "product=" );
    $wp_rewrite->add_rewrite_tag( "%version%", \'([^/]+)\', "version=" );
    $wp_rewrite->add_permastruct( \'version\', $version, false );
}
修改后permalink
add_action( \'init\', \'rewritePostPermalink\' );

function rewritePostPermalink(){
    global $wp_rewrite;

   $wp_rewrite->permalink_structure = "/product/%product%/news/%postname%";

}
修改页面永久链接
add_action( \'init\', \'rewritePostPermalink\' );

public function rewritePagePermalink(){
    global $wp_rewrite;

    $wp_rewrite->page_structure = "/product/%product%/page/%pagename%";
}
之后需要过滤自定义变量,如%product% 从…里面get_permalink 回答对于每种类型、帖子、页面或自定义,都有不同的筛选器。小心点!page_link 筛选器参数2不是与的$postpost_type_linkpost_link 但是page\\u id。

add_filter( \'post_type_link\', \'permalinkRewrite\');
add_filter( \'post_link\', \'permalinkRewrite\' );
add_filter( \'page_link\', \'permalinkRewrite\' );

function permalinkRewrite( $permalink, $post ) {
    if (gettype($post) === "integer"){
        //page_link $post parameter is the ID instead of the object
        $post = get_post($post);
    }

    //removed code - get your variable here
    $product = "demoProduct";

    if ( $product ) {
            $permalink = str_replace( \'%product%\', $product->getSlug(), $permalink );
    }

    return $permalink;
}
最后,添加一些重写规则以捕获新的永久链接。

add_filter( \'rewrite_rules_array\', \'insertCustomRewriteRules\' );

function insertCustomRewriteRules( $rules ) {
    $customRules                                         = [];
    $customRules[ \'product/([^/]+)/version/([^/]+)/?$\' ] = \'index.php?version=$matches[2]\'; //custom post type
    $customRules[ \'product/([^/]+)/news/([^/]+)/?$\' ] = \'index.php?name=$matches[2]\'; //post
    $customRules[ \'product/([^/]+)/page/([^/]+)/?$\' ] = \'index.php?pagename=$matches[2]\'; //page

    return $customRules + $rules;
}

结束

相关推荐

How to replace permalinks

我已将wordpress项目导出并导入live server。现在的问题是,我将permalink结构作为“localhost”格式。当我单击网站中的任何链接时,它会将我重定向到localhost。我怎样才能改变这一点?我的htaccess文件如下所示<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /myproject/ RewriteRule ^index\\.php$ - [L] RewriteCo