WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Blog.php template: How to call for if/else statement (11 posts)

  1. kwestalot
    Member
    Posted 1 year ago #

    I have created a site using WordPress where I didn't want the blog to be the home page. Instead, I wanted it to be http://sitename.com/blog/ - which I've successfully done.

    What I've done is I've created a blog.php page and made it a template using:

    <?php
    /*
    Template Name: Blog
    */
    ?>

    I then created a WordPress Page and selected the 'Blog' template from the right hand side of the page. This successfully linked the template to the permalink (http://sitename.com/blog/) and so now I have my index.php as my home page and my blog.php acting as my Blog home page.

    Now, to explain the problem...

    I'm trying to create an if/else statement that will display a particular image based upon what the page is.

    Here's the if/else statement I have so far:

    <?php if (is_page('attorneys') ) { ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/railing.jpg">
    
    <?php } elseif (is_page_template('blog.php') { ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/sunset.jpg">
    
    <?php } ?>

    I've tried to call the blog template in numerous ways, including:
    is_page('114'), is_page(114), is_page('blog'), is_page_template('blog.php') ... but none of these seem to work!

    I've messed around a little bit with is_front_page and is_home, but these aren't really working either...and I've played around with the Settings > Reading - Front Page and Home Page, and that doesn't work either.

    Can anyone help? How do I correctly call the blog template page so it will display the correct image?

  2. SNSD Photo
    Member
    Posted 1 year ago #

    <?php } elseif (is_page_template('blog.php') { ?>

    i think it should be

    <?php } elseif (is_page_template('blog.php')) { ?>

    don't forget the last bracket.

    normal usage is = is_page_template('blog.php')

    if/else statement = if (is_page_template('blog.php'))

  3. kwestalot
    Member
    Posted 1 year ago #

    Thanks for your response.

    Let me expand on my example because you are right - I did input it incorrectly.

    Here's the active code right now:

    <?php } else if ( is_page_template('blog.php') || is_page('privacy-policy') || is_404() || is_page('legal-disclaimer') || is_page('sitemap')) { ?>

    As you can see I've placed it in a string where if all these pages are chosen, they will display the appropriate image. Obviously, I have left out a lot of the code, but now you can see my exact code.

  4. SNSD Photo
    Member
    Posted 1 year ago #

    1. you can make new page template for each template you want for easier maintenance and you can re-use the page template for another page not only legal disclaimer slug.

    2. nope, it's normal and still readable.

  5. kwestalot
    Member
    Posted 1 year ago #

    Everything is working on the site and it's live ... So I don't see the need to change too much. It's just a matter of making the correct call out for the blog.php template.

  6. SNSD Photo
    Member
    Posted 1 year ago #

    alright, then you can mark this topic as resolved if you think so.

  7. kwestalot
    Member
    Posted 1 year ago #

    Sorry if I wrote something to sound like it's been resolved...just wanted to say that this is one of the few things that is not working on the site, but everything else is launched.

    I still need the correct image to show up on the home page -- all the other calls, including is_single (), is_category (), are working fine...

    Just this blog.php problem.

  8. SNSD Photo
    Member
    Posted 1 year ago #

    let's say i've three conditions.

    1. i want page with "attorney" slug show "railing.jpg"
    2. and for page_template "blog.php" until page with "sitemap" slug show "sunset.jpg"
    3. any page exclude from criteria above will show "snow.jpg" (this is include home, front, etc)

    <?php if (is_page('attorneys') ) { ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/railing.jpg">

    <?php } else if ( is_page_template('blog.php') || is_page('privacy-policy') || is_404() || is_page('legal-disclaimer') || is_page('sitemap')) { ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/sunset.jpg">

    <?php } else : ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/snow.jpg">

    <?php endif; ?>

  9. kwestalot
    Member
    Posted 1 year ago #

    I have done what you have placed in the code above...it's not a problem with how I'm doing the if/else statement. It's a problem with how I'm calling the blog.php page ONLY.

    Again, I've tried is_page('114'), is_page(114), is_page_template("blog.php"), is_page('blog') ... and the is_home() and is_front_page() isn't specified in the Settings >Reading area, so that isn't necessary. Any other thoughts?

  10. SNSD Photo
    Member
    Posted 1 year ago #

    is_page('114') = page with slug "114"

    is_page_template("blog.php") = i`m not sure if this call could be done, as long as i know, page-blog.php

    Due in first post you said :

    I then created a WordPress Page and selected the 'Blog' template from the right hand side of the page.

    page-(template_name).php file name only which will be avaiable in page template from the right sidebar (in post editor).

  11. kwestalot
    Member
    Posted 1 year ago #

    Ok, so I've figured it out!

    I had to put a wp_reset_query(); at the end of the loop where it pulls blog posts within my blog.php template.

    <?php query_posts( array( 'cat' => -0, 'paged'=> $paged ) ); ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php get_sidebar(); ?> 
    
                <div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<p class="postmetadata">Posted by <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y') ?> &bull; Categorized as <?php the_category(', ') ?> &bull; <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    				</p>
    				<div class="entry">
    					<?php the_excerpt(); ?>
    				</div>
    			</div>
    
    	<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<h1 class="center">Not Found</h1>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	<?php endif; wp_reset_query(); ?>

    Resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic