WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Adding conditional statement

[Resolved] Adding conditional statement

  • I am creating a theme and I need my index.php to behave in the following manner:

    >> If you are on the home or front page the file: slideshow.php is used.
    >> If it is not the home or front page, the file: archive.php is used.

    Basically, I would like the user to be able to designate a “posts” page in the settings (in this case, I chose a page named “blog”) without having to designate a home page, so that they may use the home page template w/o having to create a page and assign a home page template. I would like this to happen automatically. The only thing they will need to do is assign a “posts” page.

    Here is the code I came up with on the index.php page. It behaves as I wish for the homepage, but I cannot get my desired results for the “blog” page. Currently this code is producing the slideshow.php template on both the home page and the “blog” page that I have selected.

    <?php if ( is_home() || is_front_page()) { include (‘slideshow.php’); }
    elseif ( is_page()) { include (‘archive.php’); }

    ?>

    Please let me know if you need more explanation.

Viewing 15 replies - 1 through 15 (of 36 total)
  • Try removing either the is_home or is_front_page. I would try removing the is_home first.

    And change the elseif statement to use “!” not.

    Something like this:

    <?php 
    
    if ( is_front_page()) { include ('slideshow.php'); }
    elseif ( !is_front_page()) { include ('archive.php'); }
    
    ?>

    Wait, there may be something wrong with that, maybe with the grouping. Test and reply back.

    Thanks for the suggestion. Unfortunately, that didn’t work either. It produced the opposite results. Now both pages (home and blog) are producing the archive page.

    wpismypuppet

    @wordpressismypuppet

    It sounds to me like you want the index.php to use ‘slideshow.php’ for the home or front page, otherwise always use ‘archive.php’. Is that correct? Because you have an elseif in there which is only used to add a possible third outcome. Why not just do:

    <?php
       if(is_home() || is_front_page()) include ('slideshow.php');
       else include ('archive.php');
    ?>

    That is what I am trying to do! I just tested yours and no luck either! Now both pages are pulling in the slideshow.php

    wpismypuppet

    @wordpressismypuppet

    What do you mean by “both pages”? It’s either the home/front page or everything else! Give me an example of the two pages in question. Or possible links?

    Here is the link to where I am testing my theme:

    Home
    http://go-parkcity.com/themebuilder/

    I am trying to get the “BLOG” page to pull in the latest posts on the archive.php template.

    Here is the blog page, but as you can see it is using the slideshow.php template as well:
    http://go-parkcity.com/themebuilder/?page_id=30

    wpismypuppet

    @wordpressismypuppet

    Ok… well that’s because http://go-parkcity.com/themebuilder/?page_id=30 is acting like your “is_front_page()”. Try this:

    <?php
       if(is_home()) include ('slideshow.php');
       else include ('archive.php');
    ?>

    Or if you need to make other pages different too…

    <?php
       if(is_home()) include ('slideshow.php');
       elseif(is_front_page()) include ('archive.php');
       else [some other include here]
    ?>

    Still no luck… could it be something I have on the slideshow.php or b/c I am using the dashboard to declare that the latest posts should be going to the “BLOG” page? Might there be a better way to do this all together?

    I’m stumped. your code makes perfect sense, yet it doesn’t seem to work for me!

    wpismypuppet

    @wordpressismypuppet

    well, the only other thing that I can think of is to try this:

    <?php
       if(is_page(30)) include('archive.php');
       else include('slideshow.php');
    ?>

    This will check that “page_id=30” piece you have and hopefully trigger the response we want!

    Under settings->reading do you have a “posts page” selected? You could always try setting that back to “Select”. Unless you are using a specific page as a template?

    wow – that didn’t work either. I must have something weird going on somewhere else!

    I set the posts page back to “select” and now it is on the right template (but I’m sure it’s the page.php), but a blank page, of course!

    I just want to be able to pull in the latest posts via the top nav like I have it set up now on a page that looks like this (using the archive template) – is there another way to do this? I could use the custom menu I’m sure, but is there a standard link to find the latest posts?

    wpismypuppet

    @wordpressismypuppet

    does the archive.php page have a proper loop set up? I doubt that “page.php” would be trying to show what you see on the blog section. “page.php” is usually used to show “pages”, not “posts”. Since it’s using the right template, and archive.php is being included, there must be something wrong with archive.php. Any chance you could post the code somewhere?

    I don’t mind posting the code, but I’m sure it is correct, b/c it works fine when I pull up an archive page: ex: http://go-parkcity.com/themebuilder/?m=201201

    I think I’m just going about this the wrong way. Right now, I’m trying to use a page to pull in the blog, b/c I can’t figure out how to find the blog (latest posts) any other way. I want the site work and focus more on the pages, but still have the functionality of the blog…

    Here is the code

    [Code moderated as per the Forum Rules. Please use the pastebin]

    wpismypuppet

    @wordpressismypuppet

    well, what I typically do (because almost all of the blogs we create are so unique) is to create a physical page (in your case “archive.php” and at the very top add <?php // Template Name: Blog ?>. Then, create a page through wordpress called “whatever” and choose “Blog” from the page attribute->template drop down. Then, whatever you put on “archive.php” will be displayed when you visit “whatever”.

    Maybe that’s what you are trying to do? So you technically don’t even need the index to have that conditional comment… because the index.php page is simply a loop… that shows either a page or a post using the loop. If you want to make pages look different then posts, this is usually a good start.

    wpismypuppet

    @wordpressismypuppet

    This is typically my index.php files…

    <?php
       if(have_posts()) :
          get_header();
          while(have_posts()) :
             the_post();
             the_content();
          endwhile;
          get_footer();
       else :
          header('Location: 404.php');
       endif;
    ?>

    And my blog pages look a lot like yours…

Viewing 15 replies - 1 through 15 (of 36 total)
  • The topic ‘[Resolved] Adding conditional statement’ is closed to new replies.
Skip to toolbar