• Resolved TrishaM

    (@trisham)


    Using WP 2.5 and trying to deliver a different page if the single post being clicked on is in a specific category, else deliver the standard single page.

    This code came directly from the Codex and modified only to use my actual category ID and include file

    <?php
         $post = $wp_query->post;
          if (in_category(16)) {
             include(TEMPLATEPATH . '/single-cat-16.php');
             }
    	     else {
             include(TEMPLATEPATH . '/single.php');
             }
    ?>

    However, even though I am positive that a post is within the category specified (16) – I even checked it using phpMyAdmin to be certain – WP is still using single.php, not single-cat-16.php for all single posts.

    In searching through the Codex and forums I came across information that said to use “is_category” instead of “in_category” if you are not using it within the Loop, so I tried that as well – made no difference.

    Is there something different about 2.5 and this function?

Viewing 15 replies - 1 through 15 (of 19 total)
  • boober

    (@boober)

    did you actually make the single-cat-16.php page template? you need to have that file in your template.

    also, i dont know how youre styling this other category, but it might be easier to do this with a different stylesheet, rather than a different php template file.

    <?php if ( is_category('15') ) {
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/cat-15.css"
    type="text/css" media="screen" />;
    <?php } else { ?>
       <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"
    type="text/css" media="screen" />
    <?php } ?>
    Thread Starter TrishaM

    (@trisham)

    Hi boober – thanks for responding. It’s not something that can be done with a different stylesheet as it is actually an entirely differently setup page….

    yes I do have that file on the server – it’s not technically a template, it’s just called as an ‘include’ file so the code in that file would be incorporated into the single.php file, otherwise the code in the other file would be included (the other file is actually called singleproduct.php, not single.php as shown above and is called correctly, not as shown in my example above).

    So just to clarify I have three files:
    1. single.php
    2. single-cat-16.php
    3. singleproduct.php

    The code in single.php (in it’s entirety) is:

    <?php get_header(); ?>
    <?php include (TEMPLATEPATH . "/sidebar-left.php"); ?>
    <?php
         $post = $wp_query->post;
          if (is_category(16)) {
             include(TEMPLATEPATH . '/single-cat-16.php');
             }
    	     else {
             include(TEMPLATEPATH . '/singleproduct.php');
             }
    ?>
    <?php include (TEMPLATEPATH . "/sidebar-right.php"); ?>
    <?php get_footer(); ?>

    So you can see it checks first to see what the post’s category is – if it’s 16, then it’s supposed to include the file single-cat-16.php, then finish with the right sidebar and footer. If it’s not in category 16, it should instead include the file singleproduct.php, then finish with the right sidebar and footer.

    However, even when the post is absolutely definitely in category 16, and only category 16, it is still serving up the file singleproduct.php. As mentioned I’ve tried both is_category and in_category and neither one works….

    Any ideas?

    boober

    (@boober)

    you dont need single quotes around cat number?
    if (is_category('16'))

    Thread Starter TrishaM

    (@trisham)

    Nope – you can use it either way according to this Codex page (under Parameters) – if you’re using an integer.

    Thread Starter TrishaM

    (@trisham)

    Another bump…….I am still having trouble with this problem as well……

    moshu

    (@moshu)

    It should definitely be in_category if it is a single post view.

    Thread Starter TrishaM

    (@trisham)

    yep it’s a single post view – although I tried both (neither works) I definitely have it set to in_category now. But it still just shows the page listed second in all cases, regardless of the fact that some posts are definitely in category id 16.

    To test this, I’ve reversed the pages listed (so it shows singleproduct.php if the category id is 16, and single-cat-16.php if not, and it shows single-cat-16.php in all cases.

    So the problem is not with the files being called to include, it’s definitely with the statement if (in_category(16));

    I suspect it might be a 2.5 bug, but don’t have any idea how it could be fixed, or if there is a workaround (using some other query/statement?)

    moshu

    (@moshu)

    It might be a bug… although I don’t see any report about it in the trac.
    If you don’t find one either – submit a bug report.
    http://trac.wordpress.org/

    Thread Starter TrishaM

    (@trisham)

    Thanks – can I ask your opinion?

    I searched through the Trac and although I found similar problems with in_category, I didn’t see a bug report about it not working at all, so I think I should post a bug report.

    But I’m wondering if you could perhaps try to reproduce this (make a single.php file calling one include-file for a category, and another for all other categories). I would just like to find out if it’s just me or not ( 🙁 ) before I post a bug report

    For now I’m using a modified version of the custom post template plugin mentioned in the Codex page for in_category…….it’s working but I like to keep my plugins to a minimum, especially if something in the WP core is intended to do this anyway….

    moshu

    (@moshu)

    It shouold work without a plugin but it doesn’t. Checked.

    Thread Starter TrishaM

    (@trisham)

    Thanks – I’ll go ahead and post a bug report then. Thanks for your help and time 🙂

    Thread Starter TrishaM

    (@trisham)

    I am bumping this again because Otto42 closed the ticket I opened, saying that it “works for (him)” – his note:

    status changed from new to closed.
    resolution set to worksforme.
    Tested and that does work if you add global $post to the front of it, to ensure you’re modifying the right $post variable.
    The codex probably needs to be clarified on this point.

    However I am not sure how to add global $post to the front of it and don’t know how long it will be until the Codex is clarified…….

    Can anyone post how the code snippet should look with the addition of global $post?

    ivovic

    (@ivovic)

    just add:

    global $post;

    before the first time you use $post.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    In your specific case, make sure that you’re using global $post, $wp_query; to specify the right ones.

    Also, reading more of this thread, you definitely want in_category, not “is”.

    However, I don’t much care for that codex article. That code won’t necessarily work before the Loop. It depends too heavily on side effects that may or may not be the case in certain situations.

    A better way to do this sort of thing is like this:

    <?php
     if ( have_posts() ) { the_post(); rewind_posts(); }
     if ( in_category('17') ) {
     include(TEMPLATEPATH . '/single2.php');
     } else {
     include(TEMPLATEPATH . '/single1.php');
     }
     ?>

    I use code similar to this in my header, to access post data. This has the benefit of being safe in almost all cases, because you’re starting the Loop with the_post(), but then stopping it and rewinding prematurely. This technique will actually let all “Loop only” functions work outside the main Loop, for the first post in that Loop.

    Thread Starter TrishaM

    (@trisham)

    Otto42 – a big huge THANK YOU!

    The code you posted works great – that solves this particular problem and another one that’s been bugging me for a while.

    I’m a very happy camper now. (big grin)

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘in_category not working’ is closed to new replies.