WordPress.org

Ready to get started?Download WordPress

Forums

Trouble Using template_redirect (14 posts)

  1. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Hello Wp community. I am normally a web designer (Photoshop, Illustrator and so on ) that has recently come to be forced to learn php and code out Wp themes myself. Which I am only marginally good at. So I want to apologize first off if this topic has been covered to death. I searched a bit and was unable to find a solution so I find myself here asking this.
    I am building a theme for a client and for the first time a single - single.php file wont take care of my post display for the different categories I have. Some will be normal "Latest Article" posts that will use the standard single.php file and that works well. I have a couple of categories that will be like real estate listings and they will use a file called saleposts.php which I have included the use of custom fields to list things like bedrooms, bathrooms, address ect... You know like a real estate site, lol. So anyways I have tried this several ways and have not been able to get it to work properly, my listing posts always go to the single.php file instead. So here is how I did it,
    First try was like this from a tutorial here The tut I used

    function my_template() {
        if (is_category() && get_query_var('cat') == get_cat_id('for-sale')) {
            include (TEMPLATEPATH . '/salesposts.php');
            exit;
        }
    }
    
    add_action('template_redirect', 'my_template');

    This did not work, and I am not sure why....

    Then I tried this as a test based on the same tut and it worked but I cant do this for ecery post

    function my_template() {
        if (is_single()) {
            global $post;
            if ($post->ID == 881) {
                include (TEMPLATEPATH . '/salesposts.php');
                exit;
            }
    
        }
    }
    
    add_action('template_redirect', 'my_template');

    the 881 is the post id. Can anyone help me out with why the first code would not work properly? I am sure that I have everything I need in my header and my functions.php file to make it work properly? Or could I be missing something????

    [You'll reduce the chance of receiving help if you reply to your thread. You remove it from the 'No replies' list. Some volunteers proiritise threads on that list over others]

  2. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Did I post this in the wrong place? Just wondering if anyone can help me with this or if I need to post it in another section of the forums???

    Note: wait at least a day before responding to your own post. Otherwise, the people who look for topics without replies will see that your post has been replied to, and may pass over it.

    There are several things here; the first one is that, if you're trying to target a single blog post that has a certain category, then is_category() will never return true. The is_category() conditional returns true when a category archive index page is being displayed. You probably want to use the in_category() conditional instead.

    However, I think there are other, better ways to do what you're trying to accomplish.

    The easiest is probably using template-part files. Inside of your single.php, abstract all of the loop code into a separate, template-part file, named loop.php. Then include that template-part file via get_template_part(). So, like so:

    /**
     * Single blog post
     */
    get_header();
    
    get_template_part( 'loop' );
    
    get_footer();

    Now, abstract the loop part of salesposts.php into a file called loop-sales.php.

    Then, include one or the other of loop.php and loop-sales.php, using an appropriate conditional:

    if ( in_category( get_cat_id( 'for-sale' ) ) ) {
        get_template_part( 'loop', 'sales' );
    } else {
        get_template_part( 'loop' );
    }

    That's probably the approach I would take, anyway.

  3. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Hello there Chip Bennett, and thank you for your reply.

    Note: wait at least a day before responding to your own post. Otherwise, the people who look for topics without replies will see that your post has been replied to, and may pass over it.

    Sorry, im new here and have not had the need to ask questions until now, I always followed the codex and tutorials and they have all worked for me so far, so my apologies.
    What I have is a single.php for the normal blog posts or articles that my client will post like a blog or like articles. Then I have a salesposts.php that will take only the posts from real estate listings posts that he creates and display them using the salesposts.php. The reason is that with the normal blog posts I do not have or will not use any custom fields whereas the salesposts.php will display all of the posts that fall under certain categories (like for rent, for sale, and commercial properties). These will be real estate listings that will include a few custom fields in the posts. Since I dont know php well enough to write some over inflated file "single.php" with tons of code, I simply decided to separate the 2 and use the codex method of calling forth the custom fields and displaying them in the salesposts.php with the content of that post. Basically fields like the address or the property, the square feet, number of bedrooms and so on. I have it all styled out and it looks good on the salesposts.php, just cant get the posts to actually go there. the second bit of code was simply a test I did following the codex for a single post, kind of hard written for just one post, but I need 3 cats to follow suite here and I simply can not get it to work. The tutorial seems to work for everyone but me, lol. Thanks for the input, but I am not php savy (less than 5 months with php) and I really want this method to work.

  4. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Well, if you insist on using template_redirect:

    function my_template() {
        if ( is_single() && in_category( get_cat_id( 'for-sale' ) ) ) {
            include ( get_template_directory() . '/salesposts.php' );
            exit;
        }
    }
    add_action('template_redirect', 'my_template');
  5. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Oh, basically I would like to make this work if possible, but take a look and note that i am trying to include 3 cats. The worst part is that I have not even gotten the one to work yet, lol, heres what I would like to do.

    function my_template() {
        if (is_category() && get_query_var('cat') == get_cat_id('for-sale')) {
            include (TEMPLATEPATH . '/salesposts.php');
            exit;
        }if (is_category() && get_query_var('cat') == get_cat_id('for-rent')) {
            include (TEMPLATEPATH . '/salesposts.php');
            exit;
        }if (is_category() && get_query_var('cat') == get_cat_id('commercial')) {
            include (TEMPLATEPATH . '/salesposts.php');
            exit;
        }
    }
    
    add_action('template_redirect', 'my_template');

    and leave the normal blog posts to single.php. Hope this helps.

  6. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Also, here is the theme I am building, the yellowish colored places indicate things I am still working on, so they do not look as intended yet.
    http://www.entechpc.com

    building it for a guy who only wants to spend 400 bucks, feel like I am getting jipped but oh well. I need to learn this stuff anyways I guess, lol. Thanks for the help Chip, greatly appreciated.

  7. Keyboard_Headaches
    Member
    Posted 1 year ago #

    ALthough its still not working, lol. Ugh!!

  8. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Oh, bye the way... Just to be sure you know... I am not trying to get a single post in a category, I am trying to get all of the ones in the 3 categories to use this php. file.

  9. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Tried your way chip, just made everything a white blank page, even the homepage

  10. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Tried your way chip, just made everything a white blank page, even the homepage

    Which way did you try, and what fatal error(s) did you get?

  11. Keyboard_Headaches
    Member
    Posted 1 year ago #

    well, I created a loop.,php and a loop-sales.php and then used get template part to call the loop.php for the index and page.php files and used get template part for the salesposts.php and the entire site went blank white, no errors just blank.

  12. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    ...and the entire site went blank white, no errors just blank.

    Enable WP-DEBUG, and report back the fatal error(s).

    See also:
    http://codex.wordpress.org/Debugging_in_WordPress

  13. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Also, i am reading http://codex.wordpress.org/The_Loop_in_Action right now, not sure I understand it all but I am trying.

  14. Keyboard_Headaches
    Member
    Posted 1 year ago #

    Ok, will do that

Topic Closed

This topic has been closed to new replies.

About this Topic