• Resolved juri003

    (@juri003)


    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!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter juri003

    (@juri003)

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

    Thread Starter juri003

    (@juri003)

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

    Anybody an idea to solve it once and for all?

    https://codex.www.ads-software.com/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

    Thread Starter juri003

    (@juri003)

    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?

    Thread Starter juri003

    (@juri003)

    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

    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.

    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 == "https://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.

    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:

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

    and change it to pretty permalinks like

    https://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.

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

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

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘one post in multiple categories, Wich single template?’ is closed to new replies.