Can I assign WP php file (specific template) to several pages with same prefix name/slug using (if is_page(){}) ?

If it's possible it could be useful. Is there a php function to do like css selector : [class*="activity-"] for example ??: if(is_page('[activity-]')){ include "activity.php"; }

the goal is to not repeat 4 times the same function : if(is_page('activity-sport')){ include "activity.php"; }

activity.php assign to :

  • website/activity-sport
  • website/activity-culture
  • website/activity-charity
  • website/activity-work
  • are you aware of page templates or template parts?
    – Tom J Nowell
    Commented May 8, 2022 at 13:35
  • Yes, I know perfectly. But I use wordpress only for content, I use php file to create new templates . I can't use "page attributes" and "model" in wp right side menu! Precisely I want to use a template for several pages. But I will use many different templates by major categories: activities, news, businesses, companies, etc. The layout of the content and the php queries will not be the same. I prefer to have several php pages (templates) to distinguish each major category and not repeat unnecessary queries. maybe I'll use subdomains later.
    – imagIne
    Commented May 8, 2022 at 17:01
  • I will still test the page template using model, but I think I would still be limited
    – imagIne
    Commented May 8, 2022 at 17:05

1 Answer 1


How about this?

For PHP 8 (faster)

if ( str_contains( get_post_field( 'post_name' ), 'activity') ) { 
    get_template_part( 'activity' );

For older versions of PHP

if ( strpos(get_post_field( 'post_name' ), 'activity') !== false ) { 
    get_template_part( 'activity' );
  • thank you so much
    – imagIne
    Commented May 9, 2022 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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