How to assign php file(template) to several pages with same prefix page name/slug

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

Topic functions slug pages Wordpress

Category Web


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' );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.