Same content, different navigation

In this imperfect world, I often get involved in project after decisions have been made and content has been created. This is one of those scenarios.

What we have is an online textbook. It has a sidebar menu for each section and at the bottom of each page are buttons for the previous/next page. Pretty straight forward so far. The issue that’s come up is that  some of the pages will be reused in different sections. Naturally we don’t want to duplicate content. How can we deal with the previous/next buttons in a way that contextual aware of the unit they’re part of at the time?

Strike One

As I thought about it, I had a temporarily great idea! We have the URLs needed for the previous/next buttons in the existing sidebar menu and, since that sidebar menu differs in each unit, we don’t need to do any magic. We just have to know what page we’re on and what URLs are on either side of that URL in the sidebar menu. I built that out and started to write this post congratulating myself on my cleverness . . . but as I wrote it I realized this would not work at all. The menu is associated with the page and can only have one association in this scenario. Ah well. Might come in handy some other day so I’m including it below. With some tweaking it would have removed the need to hand-code the previous/next buttons so it’ll come up again I’m sure.

See the Pen
context dependent next button
by Tom (@twwoodward)
on CodePen.

Bunt

I don’t consider this a homerun let along a grandslam. It’s a strategic solution. Often that’s what I do. It’s not perfect. It doesn’t make me happy but it’s workable in the situation that I exist in. That idea of making reasonable choices given the circumstances is something I find irritating but necessary. Would I rather rebuild this site from scratch? Absolutely. Is that reasonable or even feasible? Absolutely not. So this is some duck tape1

Since I need separate pages to associate with the sidebar menu, we are going to have to create a duplicate page BUT we aren’t going to put any content in it. We’ll just name the page the same thing and we’ll use an ACF post object field to chose the page whose content you want to mirror. There’s some stuff we’ll need to work out procedurally to keep confusion to a minimum but this will answer the problem with minimal drama.2

The code below is using the content filter function to replace the content with whatever page is chosen via the ACF field.

//duplicate content via filter based on ACF assignation 
add_filter( 'the_content', 'ssr_duplicate_content', 1);

function ssr_duplicate_content($content){
    $post_id = get_the_ID();//what's the post ID
    if (get_field('duplicate_content', $post_id)){
        $source_id = get_field('duplicate_content', $post_id);//get the acf field value - which is the ID of the source 
        $source_post = get_post($source_id);//get the source
        $source_content = $source_post->post_content;//get the source content 
        return $source_content;//return 
    }
}

And Then . . .

This was the point that I realized I need both of these things to make this work right. Blog posts are nice for helping you realize you are a moron.


1 I know you’re eager to correct me. But like most things, there is no single right answer. Now on to how to pronounce gif.

2 There’s some way I might do this with referrer URLs but I worry about all kinds of exceptions and replacing the menu on the fly seems like a possible source of problems. There might also be path with setting cookies but I worry about privacy settings and it all just seems messier than this way.

Comments on this post

  1. Alsn said on April 24, 2022 at 4:24 pm

    Is the sidebar manually created? I was thinking that it’s maybe done via the Pagelist plugin and built from the page hierarchy but this does not allow for reuse of a same page in a different order.

    It almost seems you would need all pages as independent content you sequence together as needed by id but that’s the other non-imperfect world.

    I enjoy seeing the out loud thinking here, plus I know now what Wisconsin pewter on a roll is.

    • Tom Woodward said on April 24, 2022 at 4:46 pm

      It’s something of a mess. The menu is built via the regular WP menu management but then assigned to a particular widget via another plugin and made into a exandible plugin via yet another plugin. But you get what you get . . .

  2. carpetbomberz said on April 28, 2022 at 10:26 am

    Indeed one goes to work with the spaghetti strands of WP plugins/widgets you have, not the structured WP you _wish_ you had.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL