最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Load a template page based on part of slug in wordpress

programmeradmin8浏览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/advice-on-twitter

domain/advice-on-facebook

domain/advice-on-instagram

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/advice-on-twitter

domain/advice-on-facebook

domain/advice-on-instagram

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 13, 2017 at 18:13 landedlanded 1316 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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;
});
发布评论

评论列表(0)

  1. 暂无评论