• Sorry if this post title really confused. I can’t think of a better title than above. because my english skills are very lacking

    what i expected was,
    say i have accessed this url https://example.com/i-want-open-this-page

    so, when the slug/url i-want-open-this-page accessed it will automatically use the page-showit.php template.

    the slug i want to use is more than 10+ slug and always use page-showit.php template.

    can someone help make this function?

    • This topic was modified 6 years, 5 months ago by lynecker.
    • This topic was modified 6 years, 5 months ago by lynecker.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming these pages to show are indeed page post types, you can specify which page template to use right on the page edit screen. This option is only visible when there is a choice of page templates. Many themes only offer one choice, so the option is hidden. For your own templates to be listed for selection, they must be a page template for global use. This gives users much more control over the appearance of current and future pages over forcing a particular template for specific slugs.

    If your pages are not page post types, or it’s important users do not have control, you can use the ‘template_include’ filter to manage what template is used for any request. The conditional works much better if you work off IDs instead of post slugs, but the same concept can be used with slugs if necessary.

    add_filter('template_include', 'my_template_override');
    function my_template_override( $template ) {
    	if ( is_single() && in_array( get_queried_object_id(), [ 123, 321, 456, 654 ]))
    		return '/full/path/to/your/templates/page-showit.php';
    	// use the default template
    	return $template;
    }

    If you must use slugs, check if get_queried_object()->post_name is in an array of slugs.

Viewing 1 replies (of 1 total)
  • The topic ‘What Conditional use for specify template with custom slug?’ is closed to new replies.