WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] in_category not working (20 posts)

  1. TrishaM
    Member
    Posted 6 years ago #

    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?

  2. boober
    Member
    Posted 6 years ago #

    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 } ?>
  3. TrishaM
    Member
    Posted 6 years ago #

    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?

  4. boober
    Member
    Posted 6 years ago #

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

  5. TrishaM
    Member
    Posted 6 years ago #

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

  6. TrishaM
    Member
    Posted 6 years ago #

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

  7. moshu
    Member
    Posted 6 years ago #

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

  8. TrishaM
    Member
    Posted 6 years ago #

    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?)

  9. moshu
    Member
    Posted 6 years ago #

    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/

  10. TrishaM
    Member
    Posted 6 years ago #

    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....

  11. moshu
    Member
    Posted 6 years ago #

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

  12. TrishaM
    Member
    Posted 6 years ago #

    Thanks - I'll go ahead and post a bug report then. Thanks for your help and time :)

  13. TrishaM
    Member
    Posted 6 years ago #

    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?

  14. Ivovic
    Member
    Posted 6 years ago #

    just add:

    global $post;

    before the first time you use $post.

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  16. TrishaM
    Member
    Posted 6 years ago #

    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)

  17. 3stripe
    Member
    Posted 6 years ago #

    Hello folks, been a while since I was on here! (Liking the new 2.5 Admin theme a lot)

    I have a similar problem - my blog template is incorrectly being applied to every page. I've double checked the category ids and that the is_child plugin is working... but... no luck...

    Any ideas?

    Here is my index.php page:

    <?php
    
     $post = $wp_query->post;
    
     if (is_child('Blog') || in_category('1')) {
    	// if blog article, use single page blog template
      	include(TEMPLATEPATH . '/index_blog.php');
     }
    else if(strstr($post->post_content,'<!--more-->')) {
    	// else if more tag found, use 2 column splitter layout
    	include(TEMPLATEPATH . '/index_image.php');
    
     } else {
    	// otherwise use default 1 column layout
    	include(TEMPLATEPATH . '/index_default.php');
    }
    
    ?>

    As suggested above I've also tried:

    <?php
    if ( have_posts() ) { the_post(); rewind_posts(); }
    
    if (is_child('Blog') || in_category('1')) {
    	// if blog article, use single page blog template
      	include(TEMPLATEPATH . '/index_blog.php');
     }
    else if(strstr($post->post_content,'<!--more-->')) {
    	// else if more tag found, use 2 column splitter layout
    	include(TEMPLATEPATH . '/index_image.php');
    
     } else {
    	// otherwise use default 1 column layout
    	include(TEMPLATEPATH . '/index_default.php');
    }
    
    ?>
  18. 3stripe
    Member
    Posted 6 years ago #

    It's like the other pages think they are in_category('1') ????

    edit - confirmed, if I put the below on a static page, the output is 'Blog'

    foreach((get_the_category()) as $category) {
        echo $category->cat_name . ' ';
    }
  19. 3stripe
    Member
    Posted 6 years ago #

    Found an easy fix :)

    if (!is_page() && (is_child('Blog') || in_category('1')))

  20. Mav3000
    Member
    Posted 5 years ago #

    Just a quick comment say that Otto's reply here has really helped me out - thanks! showing category-specific content outside of the loop as been driving me mad for a long time! But not any more!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.