WordPress.org

Ready to get started?Download WordPress

Forums

Unique Headers for Tags/Archives/Category Templates (10 posts)

  1. sethhay
    Member
    Posted 10 months ago #

    Hello,

    Trying to setup the tags/archive/category templates to call unique navigations depending on what page was visited.

    For an example, the page I am on has a blue navigation and I click on the archive for 2013, this takes me to the archive page with the blue navigation. On a page with the red navigation will take me to the archive page with red navigation. I am probably over thinking this in my head but any help will help!

  2. bcworkz
    Member
    Posted 10 months ago #

    I think you would want to pass the current color as an URL parameter so the next template can check this parameter and load the correct content associated with that color.

    So when a red page is being loaded, for example, all URL links should have ?color=red appended to them. When one of those links are clicked, the requested template uses $_GET['color'] to get the current color and determine that it should send out 'red' content.

  3. sethhay
    Member
    Posted 10 months ago #

    Interesting. I should have explained my example better. Instead of colors, I want to use template php file like the following:

    <?php include (TEMPLATEPATH . '/nav-usa.php'); ?>
    If coming from a page using a page with nav-usa.php.

    <?php include (TEMPLATEPATH . '/nav-uk.php'); ?>
    If coming from a page using a page with nav-uk.php.

  4. esmi
    Forum Moderator
    Posted 10 months ago #

    Do not use TEMPLATEPATH. It should only ever be used internally by WordPress core.

  5. sethhay
    Member
    Posted 10 months ago #

    What is an alternative? Also would that alternative work for what I need?

  6. esmi
    Forum Moderator
    Posted 10 months ago #

    Try using get_template_part().

  7. bcworkz
    Member
    Posted 10 months ago #

    As for passing URL parameters, I was pretty sure you were not really trying to pass colors :) The concept still applies though, any meaningful text string can be passed, like the name of the template, or a security hash, or....

    In case you can discern the pertinent information from a permalink, you can get the permalink of the page that sent the user to the current page from $_SERVER['HTTP_REFERER'].

  8. sethhay
    Member
    Posted 10 months ago #

    Excellent!

    Thank you esmi for the updated template path fix and thank you bcworkz for the server script.

  9. sethhay
    Member
    Posted 10 months ago #

    Back again (sorry!). I was able to get quite the distance with the $_SERVER code, though I ran into a little issue when going from "archive.php" to "single.php" (single post). Apparently going two levels deep, code gets ignored and no navigation shows up.

    Below is what I have thus far.

    <?php
    	if (strpos("http://web.com/usa/", $_SERVER['HTTP_REFERER']) !== false) {
        	echo get_template_part('nav','template');
    
    }
    	elseif (strpos("http://web.com/uk/", $_SERVER['HTTP_REFERER']) !== false) {
        	echo get_template_part('nav','uk');
    
    }
    ?>
  10. bcworkz
    Member
    Posted 10 months ago #

    Can't be too many causes. Either the string match is not found for some reason or the template is not found. Some value is likely not what you expect. Double check the referer field is really what you expect. Consider only providing a needle parameter for strpos() that is essential for identifying the proper template to eliminate stray differences that do not matter, such as an URL like 'www.web.com/usa/' which is valid for the match but yet will not match. Perhaps search just for '.com/usa' and '.com/uk'?

    To convince yourself that code is or is not being executed, insert error_log() lines at key points with various informative messages that will build a sort of trace path in your error log.

    If it is the template not being found, it may seem silly, but you could even trace through the core code and check key variables in order to identify exactly why a template is not found.

Reply

You must log in to post.

About this Topic