WordPress.org

Forums

[resolved] Content conditionally displayed depending upon last page visited? (6 posts)

  1. tictok
    Member
    Posted 5 years ago #

    Hi - hope I've posted this in the correct forum?

    I like to know if (and how) it is possible to conditionally display content on a page depending upon which page the user has come from within my site? (would this be by referrer?)

    Or, as an alternative is it possible to pass a $variable from a link clicked on page 'A' into an variable within an expression on page 'B', thus making page B show content relating to page A?

    I realise the simplest option would be to use a tag archive page, but that would break my navigation because the highlighted current page would no longer show up in my top and side nav bars as the visitor would have moved away from my page structure..

    I'm building a site which has is using WordPress as a dynamic CMS. The site is still locally hosted in Mamp so I can't yet show you, but I'll try and describe the scenario.

    I have four 'product pages' within the site each showcase one of four products. There is also a 'reviews page' containing a long list of customer reviews for all of the 4 products. These reviews are tagged with their own custom taxonomy to allow them to be sorted by product.

    I'd like the 'customer reviews' page to show reviews for a certain product first (above the other reviews) if the visitor has previously been viewing the 'product page' for that specific product.

    My initial thoughts are to somehow use the page referer info to trigger a conditional template tag, or to pass a specific taxonomy tag into a query. I don't know if this is possible? Maybe an easier solution (again, no idea if its possible) would be to somehow pass a variable from a link clicked on the product page into a condition tag on the review page?

    I'm new to wordpress and have learnt so much about wordpress and PHP in the past few weeks but this is beyond me. I'd really appreciate any help or pointers if this is doable.

    Many thanks
    Stef

  2. Michael Fields
    Themer
    Posted 5 years ago #

    Stef, This wordked for me in 2.9.1:

    switch( wp_get_referer() ) {
    	case 'http://example.org/' :
    		/* Do Stuff here */
    		break;
    	default :
    		/* Don't forget to add a default case :) */
    		break;
    }
  3. tictok
    Member
    Posted 5 years ago #

    mfiled, Fantastic - thanks for your help! It easy when you know how!
    I had a look in the codex for referrer but didn't find much. Referer with one r is the secret ;)

    Thanks again.

  4. Michael Fields
    Themer
    Posted 5 years ago #

    tictock - spelling seems to be one of my downfalls as well ;) Glad I could help.

  5. handyguyspodcast
    Member
    Posted 5 years ago #

    mfields, tictok - Thanks for this!!! I have a question.
    How would I say

    case 'http://example.org/*.*ANYPARTOFSITE*.*' :

    Make sense? the way mfields switch is written it only works if there is an exact match to the referrer URL. I want to "do stuff here" for any traffic coming from that domain, not just a specific URL.

    Thanks!

  6. handyguyspodcast
    Member
    Posted 5 years ago #

    i had to do this:

    $ref1 = parse_url(wp_get_referer()); //getting the referring URL
    switch( $ref1["host"] ) {  //looking only at the host
    	case 'www.example.org' : //this is the host url i am matching
    			/* Do Stuff here */
    		break;
    	default :
    		/* Don't forget to add a default case :) */
    		break;
    	}

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic