Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created March 4, 2020 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulgibbs/38caa8d28dbd383211cccb3872b631d2 to your computer and use it in GitHub Desktop.
Save paulgibbs/38caa8d28dbd383211cccb3872b631d2 to your computer and use it in GitHub Desktop.
replace a taxonomy template with a rendered post template
<?php
/**
* Plugin Name: Render a specific Post on a Taxonomy archive page.
* Plugin Author: Paul Gibbs.
* License: public domain
*/
namespace paul;
use WP_Post;
// Change this to a POST that exists!!
const TAX_GUTENBERG_POST_REPLACEMENT_ID = 45;
/**
* Replace category/taxonomy archive template with a Post template.
*
* See: get_taxonomy_template() -> get_query_template()
*/
add_filter( 'template_include', function( $template ) {
if ( ! is_category() ) {
// You might need to check is_tax() here as well?
return $template;
}
$gutenberg_post = get_post( TAX_GUTENBERG_POST_REPLACEMENT_ID );
// If more tweaks are needed here, BuddyPress' bp_theme_compat_reset_post() may inspire.
global $wp_query;
$wp_query->post = $gutenberg_post;
$wp_query->post_count = 1; // Prevent comments form from appearing.
$wp_query->post_type = 'post';
$wp_query->posts = [ $wp_query->post ];
$wp_query->is_archive = false;
$wp_query->is_single = true;
$wp_query->is_tax = false;
// Load theme's single post template.
return locate_template( [ 'singular.php' ] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment