skip to Main Content

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


  1. 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:

    function custom_page_for_news() {
        // Check if we are on the 'news' category page
        if (is_category('news')) {
            // Set the query to load the content of 'custom-page'
            $custom_page = get_page_by_path('custom-page');
            if ($custom_page) {
                $GLOBALS['wp_query']->queried_object_id = $custom_page->ID;
                $GLOBALS['wp_query']->post = $custom_page;
                $GLOBALS['wp_query']->found_posts = 1;
                $GLOBALS['wp_query']->post_count = 1;
                $GLOBALS['wp_query']->max_num_pages = 1;
                $GLOBALS['wp_query']->is_page = true;
                $GLOBALS['wp_query']->is_singular = true;
                $GLOBALS['wp_query']->is_single = false;
                $GLOBALS['wp_query']->is_category = false;
                $GLOBALS['wp_query']->is_archive = false;
                $GLOBALS['wp_query']->is_home = false;
                $GLOBALS['wp_query']->is_404 = false;
            }
        }
    }
    
    add_action('template_redirect', 'custom_page_for_news');
    

    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.

    Login or Signup to reply.
  2. If custom-page is not from a custom post type you could try add_rewrite_rule.

    Example

    add_action( 'init',  function() {
        add_rewrite_rule( 'news[/]?$', 'index.php?pagename=custom-page', 'top' );
    } );
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search