将.html扩展名添加到自定义帖子类型

时间:2017-03-31 作者:Iurie

我已经找到了a working solution 我的问题是,但是the problem is that it neutralizes another function 这会将类别库添加到自定义的post类型permalinks中,我需要这两种类型,所以I must to merge them somehow. 如何做到这一点?

这是添加的代码。自定义帖子类型的html扩展:

//Create the rewrite rules like post-type/post-name.html
add_action( \'rewrite_rules_array\', \'rewrite_rules\' );
function rewrite_rules( $rules ) {
    $new_rules = array();
    foreach ( get_post_types() as $t )
        $new_rules[ $t . \'/([^/]+)\\.html$\' ] = \'index.php?post_type=\' . $t . \'&name=$matches[1]\';
    return $new_rules + $rules;
}

//Format the new permalink structure for these post types.
add_filter( \'post_type_link\', \'custom_post_permalink\' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
    global $post;
    $type = get_post_type( $post->ID );
    return home_url( $type . \'/\' . $post->post_name . \'.html\' );
}

//And then stop redirecting the canonical URLs to remove the trailing slash.
add_filter( \'redirect_canonical\', \'__return_false\' );
这是将类别库添加到自定义帖子类型的代码(请参见a similar solution, 和another):

//Change your rewrite to add the course query var:
\'rewrite\' => array(\'slug\' => \'courses/%course%\')

//Then filter post_type_link to insert the selected course into the permalink:
function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'course\' );
        if( $terms ){
            return str_replace( \'%course%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

让我们把这些函数翻译成英语,然后按运行顺序排列。在这个回答中,我将有意避免使用PHP,但必须对PHP有基本的了解:

When the `post_type_link` filter runs: // the add_action calls
    run: `wpa_course_post_link` as it has priority 3 // function wpa_course_post_link(....
        if the object is a post
            and that post has a course term
                search for %course% in the URL string and replace it
    Then, run `custom_post_permalink` as it has priority 10: // function custom_post_permalink(...
        Ignore the URL we got given and construct a brand new one by returning the post_type + "/" + posts name + ".html"
所以custom_post_permalink 不添加.html 在URL的末尾,它会创建一个全新的URL。如果您将优先级从3更改为11,则您的。html URL可以工作,但您的课程URL不会%course% 已替换。

幸运的是wpa_course_post_link 函数为如何执行此操作提供了更好的模板。与其抓住课程术语并替换字符串,只需添加即可。html到$post_link 字符串并返回它

因此,如果我们用简单的英语将其写成伪代码,我们可能会得到以下结果:

When the `post_type_link` filter runs:
    run: `wpa_course_post_link` as it has priority 3
        if the object is a post, then:
            if that post has a course term then:
                search for %course% in the URL string and replace it
            then add ".html" to the end
我将转换为PHP作为读者的一项任务,问题中已经提供了所有需要的PHP。

SO网友:Iurie

借助于@tom-j-nowell 我有一个可行的解决方案。我想它还不够完美,但它确实有效。

//When your create a custom post type change your rewrite to:
\'rewrite\' => array(\'slug\' => \'courses/%course_category%\')

//Then filter post_type_link to insert the category of selected course into the permalink:
function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'course_category\' );
        if( $terms ){
            $post_link = str_replace( \'%course_category%\' , $terms[0]->slug , $post_link );
            //and add the .html extension to the returned post link:
            return $post_link . \'.html\';
        }
    }
    return $post_link;  
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );

//Then create the rewrite rules for the post-type/post-category/post-name.html permalink structure:
add_action( \'rewrite_rules_array\', \'rewrite_rules\' );
function rewrite_rules( $rules ) {
    $new_rules = array();
    $new_rules[ \'courses/([^/]+)/(.+)\\.html$\' ] = \'index.php?courses=$matches[2]\';
    return $new_rules + $rules;
}

相关推荐

Permalinks - Archives

WordPress文档说:WordPress offers you the ability to create a custom URL structure for your permalinks and archives. https://codex.wordpress.org/Settings_Permalinks_Screen 我看到此屏幕将如何为特定帖子/页面创建永久链接,但我没有看到此设置屏幕上关于如何为存档帖子/页面创建链接的任何其他详细信息。有人能澄清一下吗?