WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding conditional statement (37 posts)

  1. staceyzav
    Member
    Posted 2 years ago #

    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.

  2. MickeyRoush
    Member
    Posted 2 years ago #

    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.

  3. staceyzav
    Member
    Posted 2 years ago #

    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.

  4. wpismypuppet
    Member
    Posted 2 years ago #

    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');
    ?>
  5. staceyzav
    Member
    Posted 2 years ago #

    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

  6. wpismypuppet
    Member
    Posted 2 years ago #

    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?

  7. staceyzav
    Member
    Posted 2 years ago #

    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

  8. wpismypuppet
    Member
    Posted 2 years ago #

    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]
    ?>
  9. staceyzav
    Member
    Posted 2 years ago #

    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!

  10. wpismypuppet
    Member
    Posted 2 years ago #

    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?

  11. staceyzav
    Member
    Posted 2 years ago #

    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?

  12. wpismypuppet
    Member
    Posted 2 years ago #

    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?

  13. staceyzav
    Member
    Posted 2 years ago #

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

  14. staceyzav
    Member
    Posted 2 years ago #

    Here is the code

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

  15. wpismypuppet
    Member
    Posted 2 years ago #

    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.

  16. wpismypuppet
    Member
    Posted 2 years ago #

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

  17. staceyzav
    Member
    Posted 2 years ago #

    I was thinking of going that route if I couldn't get the conditional to work - I just thought the conditional statement would have been easier for the end user. But, really - I guess they have would have had to declare settings in either case!

    Thank you anyway for your help! I appreciate your time!

  18. wpismypuppet
    Member
    Posted 2 years ago #

    Trust me... I get it. That's all we do where I work... try to make things easier for the end user. In fact, the easier it is for them to use the system, the more coding it is on our end!

    Good luck and let me know if there's anything else I can help with.

  19. staceyzav
    Member
    Posted 2 years ago #

    Thanks again!

    Actually, now I remember why I didn't want to go the page template route - when I declare the slideshow.php as a a page template and selected that for the front page, it doesn't pull in the latest posts anymore on the bottom of the page, as I intended. Now it pulls in the info from that page.

    What I want is a the slideshow as you see, with the widgets as you see them and then below that the 2 latest "posts"

    Any ideas on how to achieve this and still have a way to have the blog page as it is now?

  20. wpismypuppet
    Member
    Posted 2 years ago #

    Can you post the code for slideshow.php?

  21. staceyzav
    Member
    Posted 2 years ago #

    It's really long but here is the loop - basically the same loop.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>  
    
           <div class="exerptpost">
    
     <?php the_post_thumbnail(array(230,230), array('class' => 'archimg')); ?>
     <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1><div class="entrydate"><?php the_time('l, F jS, Y') ?> </div>
         <p><?php echo excerpt(145); ?></p></div>
    
        <?php endwhile; ?>  
    
       <div class="navigation">
            <?php posts_nav_link(); ?>
    
            <?php endif; ?>
  22. wpismypuppet
    Member
    Posted 2 years ago #

    Gotcha... so here's the deal... you'll need a second loop, outside the main loop, where you want these top posts to go... So where ever you want it to show, add:

    <?php
    $args=array(
       'post_type' => 'post',
       'post_status' => 'publish',
       'posts_per_page' => 2
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if($my_query->have_posts()) :
       while($my_query->have_posts()) :
          $my_query->the_post();
          { Your code here }
       endwhile;
    endif;
    ?>

    Your first loop will pull the content from the page itself... this second loop with get the latest 2 posts. Not tested, but should be closer to what you are looking for...

  23. wpismypuppet
    Member
    Posted 2 years ago #

    Or since you aren't showing any "page" content on the homepage (looks like just a slideshow) you could probably replace the one loop with my code... or just this:

    http://codex.wordpress.org/Template_Tags/get_posts

    to get just the top two posts... many options!

  24. deepbevel
    Member
    Posted 2 years ago #

    i did my best to follow this thread, if I follow correctly, you might try this in slideshow.php

    <?php	 	
    
    $post = $wp_query->post;
    if (!is_page('home')) {
    	include (TEMPLATEPATH.'/archives.php');
    	return;
    }
    
    get_header(); ?>

    As for the two latest posts, just use query_posts in a static home page with the slider, you can still exclude the page from using archive.php in the conditional.

  25. staceyzav
    Member
    Posted 2 years ago #

    I think I'm following you - just not sure what to put in the { Your code here } part.

    I also think I need some sleep! Thanks again - I'll let you know tomorrow if I get it working !

  26. deepbevel
    Member
    Posted 2 years ago #

    Get to bed. When you wake up, replace get_header in slideshow.php with that code. I have a feeling you're due much joy.

  27. wpismypuppet
    Member
    Posted 2 years ago #

    Looks like, according to your posted code, you'd add something like this in the { Your Code Here }:

    <div class="exerptpost">
     <?php the_post_thumbnail(array(230,230), array('class' => 'archimg')); ?>
     <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
     <div class="entrydate"><?php the_time('l, F jS, Y') ?></div>
     <p><?php echo excerpt(145); ?></p></div>

    Good luck and let me know... check out that get_posts() link I sent you... might be easier than what I'm suggesting. It's late and now I'm over complicating things :)

  28. staceyzav
    Member
    Posted 2 years ago #

    YESSSS! IT WORKS!!!!

    Thank you both so much for your help.

    I tried all three suggestions and here is what happened:

    @deepbevel - it still pulled in the archive.php for both pages. Again - I'm baffled by this, b/c the logic seems spot on. I must have something else going on that is messing with it!

    @wpismypuppet - I couldn't figure out the more complicated one that you gave me but the get_posts() link you sent worked like a charm! I now have the 2 latest posts and being pulled into the home page via the slideshow script (that I turned into a page template) and the blog page is using the archive.php

    THANK YOU AGAIN!

  29. wpismypuppet
    Member
    Posted 2 years ago #

    You are welcome... see, when you work too long at a particular issue things start to get blurry. The code I sent you was one that I used for a much more complex issue... but it was so late I forgot about the simple get_posts() function. Glad it worked out! From now on we'll all get more sleep :)

  30. deepbevel
    Member
    Posted 2 years ago #

    Glad to hear you made progrss.
    If the page template is still an issue, you might try the opposite, put similar code in archives.php instead of slideshow.php. it should say "if it's home, use slideshow.php" Like this:

    At the very top of archives.php:

    <?php	 	
    
    $post = $wp_query->post;
    if (is_page('home')) {
    	include (TEMPLATEPATH.'/slideshow.php');
    	return;
    }
    
    get_header(); ?>

    also, two ways to specify, might try both.

    (is_page('home'))

    or

    if (is_home())

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.