WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] one post in multiple categories, Wich single template? (10 posts)

  1. juri003
    Member
    Posted 2 years ago #

    Hi all,

    Whenever you submit a post in multiple categories which are styled differently there is a problem. The post is displayed with single.php which can see the categories in which the post exists but not the one it came from, at least I could not find a way.

    Ofcourse we can use different single templates to style posts differently but when posted in multiple categories the one to use is selected by the system.

    Other than styling I would like to show the category title and give a menu highlite.

    Is there anyway to do this? Maybe pass a value from category.php to single.php or a global value which remembers the category?

    We spent lots of time figuring this out but without result. On future projects tagging & taxonomy will providemore flexibility but for current projects there has to be a way.

    Thanks!

  2. juri003
    Member
    Posted 2 years ago #

    Anybody with a bright idea on the subject? Can it be done?

  3. juri003
    Member
    Posted 2 years ago #

    The issue has been discussed on many blogs but no solution yet.

    Anybody an idea to solve it once and for all?

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/wp_get_referer

    might at least help you to know where the click came from;
    obviously no use if you use 'next post' links in single.php

  5. juri003
    Member
    Posted 2 years ago #

    Hey alchymyth,

    I think that function can only get the uri and not information like category. Could try to filter the category from the uri but that would be really unstable. Anyway I tried it in the single template and it does not work at all there.

    Thanks anyway!

    Anybody? One post in multiple categories with different styles per category?

  6. juri003
    Member
    Posted 2 years ago #

    Solved!

    The link referer was the right way to only not with the WP build-in function.

    Now I used:

    In the categaory.php or whatever-template.php:
    <?php the_permalink(); ?>?ref=bla">

    In the single.php:

    <?php
    $referer = $_REQUEST["ref"];
    echo $referer;
    if ($referer == "bla") { whatever you'd like }
    ?>

    Thanks to alchymyth & Arjen

  7. kentkoven
    Member
    Posted 2 years ago #

    juri003 -

    I am looking to solve the exact same problem, but I don't quite understand what you did there. All of my posts have 2 categories, so how does your solution determine the correct template to pull up.

    I would appreciate any help.

    Thanks.

  8. pogstar
    Member
    Posted 1 year ago #

    hi kenthoven.
    it is long ago you asked. but maybe this helps you or somebody else, cause the problem you descibed freaked me out for two days now. So finally i found another solution:

    <?
    $referer = $_SERVER['HTTP_REFERER'];
    
    if ( $referer == "http://example.com/wordpress/?cat=10" ) {
        get_template_part( 'loop', 'single1' );
    } else {
        get_template_part( 'loop', 'single2' );
    }
    
    ?>

    What happens here?
    I have one post with multiple categories. Depending on from which category (f.e. Portfolio or Shop) the user visits the post, different loops will be called.

    Thanks to alchymyth and juri003 for the referer idea.

  9. pogstar
    Member
    Posted 1 year ago #

    Ok. The code i posted only works if user comes from a specific url.
    Too bad. So i reinvestigated in your code juri003, but it did not work out in the first time.

    Finally i realized you have to get rid of the classic WP Permalink Structure:

    http://example.com/wordpress/?p=123

    and change it to pretty permalinks like

    http://example.com/wordpress/example/

    to let WP pass the "ref"-value, cause otherwise it cuts of at the first "?" and does not pass it.

    Thanks again for the cool code. Now it works great.

  10. pogstar
    Member
    Posted 1 year ago #

    If you need to keep the classic permalink structure you can call it like:

    <?php the_permalink(); ?>&ref=bla"> with & instead of ? .

Topic Closed

This topic has been closed to new replies.

About this Topic