I’m working on a WordPress site – I have a static page created with a page builder of a theme that I’ve purchased (slug: ‘custom-page’) and a category page (slug: ‘news’).
What I want to achieve is when a user navigates to the url /news
, WP should render the content of the ‘custom-page’ as if the user had navigated directly to it, but without performing any visible redirects. The url should still display /news
.
I’m working on a plugin that should do it – I’ve tried using the template_redirect
hook – it partially worked, the ‘custom-page’ content displayed, but an additional header from the theme also appeared, which is not part of this page, and is not displayed if I navigate directly to ‘/custom-page’.
Could anyone suggest a solution or point out what I might be missing?
Thanks
2
Answers
To achieve this in WordPress, you can use the template_redirect hook and modify the query to load the content from the ‘custom-page’ when the user visits ‘/news’. Below is an example code snippet for your plugin:
This code checks if the current page is the ‘news’ category page and, if so, modifies the query to load the content from the ‘custom-page’. It should be added to your theme’s functions.php file or in your custom plugin file.
Note: This approach assumes that the ‘custom-page’ is a page and not a post. If it’s a post, you might need to adjust the code accordingly.
Additionally, keep in mind that modifying the query in this way might have other implications, and you should thoroughly test to ensure it works well with your theme and other plugins.
If
custom-page
is not from a custom post type you could try add_rewrite_rule.Example
The only visible change you might see is the addition of a trailing slash to a URL that didn’t have one. How trailing slashes are handled is up to you.