Created
November 29, 2024 03:46
-
-
Save Kevinlearynet/cb4b4bbde7a41560b0756aa6de69e6d6 to your computer and use it in GitHub Desktop.
Custom rewrite rules for /quote/:ticker/ WordPress route that uses a single custom page template for all tickers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_quote_rewrite_rule() { | |
add_rewrite_rule( | |
'^quote/([^/]+)/?$', | |
'index.php?pagename=quote"e_ticker=$matches[1]', | |
'top' | |
); | |
} | |
add_action('init', 'custom_quote_rewrite_rule'); | |
function custom_quote_query_vars($vars) { | |
$vars[] = 'quote_ticker'; | |
return $vars; | |
} | |
add_filter('query_vars', 'custom_quote_query_vars'); | |
function custom_quote_template_include($template) { | |
if (get_query_var('quote_ticker') && get_query_var('pagename') === 'quote') { | |
$custom_template = locate_template('quote-template.php'); | |
if ($custom_template) { | |
return $custom_template; | |
} | |
} | |
return $template; | |
} | |
add_filter('template_include', 'custom_quote_template_include'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Template Name: Quote Template */ | |
get_header(); | |
$ticker = get_query_var('quote_ticker'); // Get the 'quote_ticker' query variable | |
?> | |
<div class="quote-container"> | |
<h1>Quote Details for: <?php echo esc_html($ticker); ?></h1> | |
<?php if ($ticker): ?> | |
<div class="quote-details"> | |
<p>Displaying information for ticker: <strong><?php echo esc_html($ticker); ?></strong></p> | |
<!-- Add custom logic here, such as fetching API data or database content --> | |
</div> | |
<?php else: ?> | |
<p>No ticker specified. Please provide a valid ticker in the URL.</p> | |
<?php endif; ?> | |
</div> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment