页面的Wordpress类别

时间:2017-02-18 11:27:58

标签: php wordpress custom-wordpress-pages

我在%category%中收到$wp_rewrite->page_structure的值时出现问题:

function custom_page_rules() {
    global $wp_rewrite;
    $wp_rewrite->page_structure = $wp_rewrite->root .'/%category%/%pagename%'; 
    //flush_rewrite_rules();
}
add_action( 'init', 'custom_page_rules',1 );

如何在%category%

中收到$wp_rewrite->page_structure

现在它返回: http://example.com/%category%/the-page-slug/

相反: http://example.com/my-cat-slug/the-page-slug/

注意页面的类别运作良好:

http://example.com/my-cat-slug/ 返回此类别中的所有页面。

1 个答案:

答案 0 :(得分:0)

解决方案可能是:

function rudr_post_permalink( $url, $post ){
    if( !is_object( $post ) )
        $post = get_post( $post_id );

    $replace = $post->post_name;

    /* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */

    if( $post->ID == 1 ) 
        $replace = 'hello-planet';
    if( $post->ID == 12 ) 
        $replace = 'Contacts';

    $url = str_replace($post->post_name, $replace, $url );
    return $url;
}

add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );

解释here

您可以将if( $post->ID == 1 )更改为您需要的任何内容。

我已经更改了整个代码,以便在网页为URL时添加类别。

问候,丹尼