Use same page-mypage.php template for several pages

To re-use the same template for different kinds of pages you can use the template_include filter. Example given from the docs: add_filter( ‘template_include’, ‘portfolio_page_template’, 99 ); function portfolio_page_template( $template ) { if ( is_page( ‘portfolio’ ) ) { $new_template = locate_template( array( ‘portfolio-page-template.php’ ) ); if ( !empty( $new_template ) ) { return $new_template; } … Read more

Template Loop – add switch case php

if ( $query->have_posts() ) : ?> <ul id=”list-con”> <?php while ($query->have_posts()) : $query->the_post(); ?> <?php switch ( get_post_type( $post->ID ) ) { case “firstcase”: ?> <li>code as defined above</li> <?php break; case “secondcase”: ?> <li>another code as defined above</li> <?php break; } ?> <?php endwhile; ?> </ul> <?php endif;

How do I assign a block template (.html) to a custom post type?

Classic and block(-based) themes both use the same template hierarchy, so for example in classic themes, we can add a template named single-<post type>.php which would be used as the default template for displaying single posts in a specific post type, and in block themes, the equivalent template is named single-<post type>.html, but that template … Read more

Plugin custom post template; without overriding all posts

Not sure if there’s a better way to do it, but you can use the theme_<post type>_templates filter to add your custom template to the “Template” dropdown, and then use the template_include filter to ensure the correct file is loaded. So something like this should work: add_filter( ‘template_include’, ‘my_events_template_include’ ); function my_events_template_include( $template ) { … Read more

How can I create custom URL routes?

Add this to your theme’s functions.php, or put it in a plugin. add_action( ‘init’, ‘wpse26388_rewrites_init’ ); function wpse26388_rewrites_init(){ add_rewrite_rule( ‘properties/([0-9]+)/?$’, ‘index.php?pagename=properties&property_id=$matches[1]’, ‘top’ ); } add_filter( ‘query_vars’, ‘wpse26388_query_vars’ ); function wpse26388_query_vars( $query_vars ){ $query_vars[] = ‘property_id’; return $query_vars; } This adds a rewrite rule which directs requests to /properties/ with any combination of numbers following to … Read more