0

How can I load a particular template based on part of a slug ?

Say I have 3 urls that I want to load the same template for so I want to match based on the 'advice-on' Please note I don't want a single page for each creating in the backend. I want to generate the content dynamic.

This is more specific part of the wider question here- Dynamic URL generates dynamic content


domain.com/advice-on-twitter

domain.com/advice-on-facebook

domain.com/advice-on-instagram

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

You can filter template_include, check the request URL for your search words and return a custom template.

Here is a primitive example:

add_filter( 'template_include', function( $template ) {

    $template_map = [
        'advice-on' => 'advice',
        'links-' => 'link-collection',
    ];

    foreach ( $template_map as $find => $match ) 
    {
        if ( 0 === strpos( $_SERVER[ 'REQUEST_URI' ], $find ) )
            return get_template_directory() . "/$match.php"; 
    }

    return $template;
});
  • Thanks that's a great starting point for me to research. I see there are some good blogs around on this subject for more. – landed Jan 16 '17 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.