Support » Fixing WordPress » Post with multiple categories assigned a defined post template(s)

  • Tommy White


    First let me give a quick run down on what my site does. I have 3 sections setup and each one has it’s own single-XXX.php file assigned based on the category which the post is located in. My single.php file contains if ( in_category( arrays that determine which category the post is in and what single-XXX.php (theme file) to use.

    single.php (tells which file below gets assigned to the post based on category location)

    Now I have a custom function that if a a post is in multiple categories it will update the permalink structure based on where the visitor clicked the post link from.

    For example if a link called Gaming is fun is clicked from different sections of my site it will automatically adjust the structure as follows:

    So for every category I list the post in (section1 and etc) it adjusts the permalink.

    Now my problem is if I have a post in both section1 and section2. When either link is clicked it uses the first theme file (single-section1.php) I have assigned in my single.php because the category is listed in both “if is” statements for section1 and section2.

    Is there a way that if a user clicks from section2 that it will ignore that the post is also in another category section1 and pull in the right template file I made, in this example single-section2.php, even though I have it in 2 categories with 2 custom single-XXX.php files for each of the categories the post is in? Basically it’s using the first one it comes in contact with through my “if is” statements in single.php.

    Man I hope I explained it clear enough lol. I have a lot of custom stuff going on with my wordpress site!

    One last explanation in another form.

    If I have a post called Biscuits and I add to it categories Breakfast and Lunch then in my single.php file I have

    $post = $wp_query->post;
    if ( in_category( array('breakfast','menu')) ) {include(TEMPLATEPATH . '/single-breakfast.php');}
    elseif ( in_category('lunch') ) {include(TEMPLATEPATH . '/single-lunch.php');}
    else {include(TEMPLATEPATH . '/single-all.php');}

    Then because I have 2 different single-XXX.php files assigned and the post appears in both, it uses the first one no matter if the visitor clicks the link from Breakfast or Lunch category section… Basically I need it to understand which category the query is taking place from..

  • The topic ‘Post with multiple categories assigned a defined post template(s)’ is closed to new replies.