Support » Fixing WordPress » Different Permalink Settings For Post and Page

  • Hello,

    How to make different permalink settings for posts and pages ?

    Example:
    – For Posts, all starts with /post/
    So it will be: example.com/post/post-name
    – For Pages, starts with /page/
    So it will be: example.com/page/page-name

    Please advice.

    Thank you

    • This topic was modified 3 months, 4 weeks ago by Jan Dembowski.
Viewing 2 replies - 1 through 2 (of 2 total)
  • threadi

    (@threadi)

    You can solve this yourself with the means provided by WordPress.

    Under Settings > Permalinks you define the appearance of the post URLs. There you write a /post/ in front of the individual URL.

    For Pages you set the URL at the permalink of the page. There is no automatism for this. So you can simply set /page/ in the permalink settings for each page.

    If you don’t want to bother with the pages, you can also add the following script to the functions.php of your child theme, your own plugin or a code snippet plugin:

    function my_page_rewrite() {
    	add_rewrite_rule('^page/([^/]*)/?', 'index.php?pagename=$matches[1]', 'top');
    }
    add_action( 'init', 'my_page_rewrite' );
    
    function my_pages_permalink( $permalink_structure, $post_id ) {
    	if ( empty( $post_id ) ) return $permalink_structure ;
    	return ( "/page".get_relative_permalink($permalink_structure) );
    }
    add_filter( 'page_link', 'my_pages_permalink', 10, 2 );
    
    function get_relative_permalink( $url ) {
    	return str_replace( home_url(), "", $url );
    }

    I have only roughly tested this.

    Thread Starter Generasi Muda Khonghucu Indonesia

    (@generasikhonghucu)

    Thank you @threadi for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.