Just quick background on the “why” of this – site has two sections, different theme in each (using jonradio multiple theme plugin). There is a decent amount of content that’s duplicated in both areas. Rather than maintain two copies of a bunch of pages (actually woo products, but that’s not terribly relevant), I’d like to display the same page at multiple URLs.

As a quick example, consider these URLs (minus the host part), with “product” being a page and widget1 and widget2 being children of that page:

/product/widget1
/product/widget2

I’d like to then have these URLs also show the same content, where “widgetworld” is the parent page:

/widgetworld/widget1
/widgetworld/widget2

What looks like the simplest method to create a virtual set of pages like this is to either play with the 404 template to grab any “/widgetworld/somepage” that doesn’t really exist and then look up the right page and send the user there or to hook into the 404 handler and do the same.

Here’s what I have, and it does work. It just feels a little too hackish. From the theme’s functions.php:

<?php 

function site404_router() {

global $route,$wp_query,$window_title,$lastpath;
error_reporting(E_ALL);
if ( $wp_query->is_404 ) // page not found? OK, let's check again
    {
        $wp_query->is_404 = false;
        $cUrl="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $targetUrl="";
        switch($cUrl)
                {
                /* sample code had some other conditions, just using this */
        default :
            {
            $pathTrimmed = trim($cUrl, '/'); // normalise with no leading or trailing slash
            $pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
            $lastpath=end($pathTokens);
            $template= get_template_directory() . '/prodtest.php';
            $window_title = 'test passing var to template';
            if ($template) {
                load_template($template);
                die;
            }
            }
        }
    }
}

add_action( 'wp', 'site404_router');
?>

And prodtest.php:

<?php
/*
* test product alias
*/

get_header();

// grab variables from the 'site404_router' function that called this
global $window_title;
global $lastpath;

// $lastpath is the last chunk of the URL, so /widgetworld/widget1 or 
// /widgetworld/widget1/ would result in $lastpath being "widget1"
// we then look for a post with the slug "widget1" (this slug is 
// unique, at least for woo products
$params = array(
'posts_per_page' => 1,
'post_type' => 'product',  // woo
'name' => "$lastpath", 
 );

// run the query    
$query = new WP_Query($params);
$posts = $query->posts;
foreach($posts as $post){  // output some info, just testing
    $id = $post->ID;
    echo "ID: " . $post->ID . "<br />";
    echo "post_title: " . $post->post_title . "<br />";
    echo "post_date: " . $post->post_date . "<br />";
    echo "post_name: " . $post->post_name . "<br />";
    echo "post_content: " . $post->post_content . "<br />";
    echo "product_image_gallery: ";
    $gallery = get_post_meta( $id, '_product_image_gallery');
    print_r($gallery);
    echo "<br />lead time: " . get_field('lead_time');
    //do_action( 'woocommerce_product_thumbnails' );
}

wp_reset_postdata();

?>

Is this a sound method? I see some problems to fix:

  • Make sure the URL is clean
  • Change status to ‘200’ in response
  • Handle real 404s properly

Interested in any alternate methods, but this one doesn’t completely scare me yet. Of note, there are many options to just make a single virtual “page”, but those really are strictly about making one or two pages, not a whole slew of them.

Read more here: How to create multiple virtual pages?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: