Dynamic Custom Permalinks

admin2025-01-07  4

I have a scenario in which the intention is to make custom permalink that can be changed dynamically, for example:

  • If a country information is showing up then the URL should be

  • If the city information of that specific country is showing up then the URL should be like .

How can I achieve that?

I have a scenario in which the intention is to make custom permalink that can be changed dynamically, for example:

  • If a country information is showing up then the URL should be http://example.com/country-information

  • If the city information of that specific country is showing up then the URL should be like http://example.com/country/city-information.

How can I achieve that?

Share Improve this question edited Apr 7, 2013 at 1:01 brasofilo 22.1k8 gold badges69 silver badges264 bronze badges asked Apr 6, 2013 at 7:02 Adil AbbasAdil Abbas 112 bronze badges 4
  • Are cities and countries custom post types? – fuxia Commented Apr 6, 2013 at 8:19
  • 1 Yeah! They were meant to be custom post types but i had managed to do in another way by doing some URL rewriting. – Adil Abbas Commented Sep 21, 2013 at 7:27
  • 1 Please add your solution as an answer, so other readers can learn something. – fuxia Commented Sep 21, 2013 at 8:50
  • Please share your solution Adil - I am also trying to do this and am using rewrite but I cant see how to get a particular template to show based on example.com/england-information example.com/india-information – landed Commented Jan 13, 2017 at 18:08
Add a comment  | 

2 Answers 2

Reset to default 0

It depends on what you mean by "changed dynamically", but the simplest way to achieve what you're talking about is to use pages (or a hierarchical custom post type), and make the "city-information" pages into children of the "country-information" page they are associated with. Then set permalinks to "Post Name", and you'll get the urls you're looking for.

Did you try add_rewrite_rule()? For example:

add_action( 'init',  function() {
    add_rewrite_rule( 'country-information/([a-z0-9-]+)[/]?$', 'index.php?country-information=$matches[1]', 'top' );
} );

After adding the new rule, you have to flush the permalink, Admin > Permalinks > Save

Then add a new query var via add_filter, needed to allow custom rewrite rules using your own arguments to work, or any other custom query variables you want to be publicly available.

add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'country-information';
    return $query_vars;
} );

You can also use a custom particular template for that permalink using template_include hook.

function conutry_information_template( $template ) {
    if ( get_query_var( 'country-information' ) === false || get_query_var( 'country-information' ) == '' ) {
        return $template;
    }
 
    return get_template_directory() . '/country-information.php';
}
add_action( 'template_include', 'conutry_information_template' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256562a370.html

最新回复(0)