【问题标题】:Wordpress Category for pages页面的 Wordpress 类别
【发布时间】:2017-07-07 23:02:37
【问题描述】:

我在接收来自%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 );

如何在$wp_rewrite->page_structure 中接收%category%

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

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


注意页面类别运行良好:

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

【问题讨论】:

    标签: php wordpress custom-wordpress-pages


    【解决方案1】:

    解决方案可能是:

    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 中添加类别(当它是页面时)。

    问候,丹尼

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 2015-06-27
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多