PHP If Statement to hide content on Homepage

Discussion in 'PHP' started by Borduhh, Mar 13, 2011.

  1. #1
    Hello All,

    I have a wordpress blog that I want to hide widgets on only the homepage. So in order to do that I believe I would use a PHP if statement with the WP command if_front(). I have tried to do this but I keep getting errors that from my knowledge I shouldnt get.

    Here is my original code:
    <div id="sidebar">
        <div id="sidebar-top">
            <h3>Weekly Poker Training</h3>
            <div id="sidebar-content">
            <?php query_posts("showposts=2&cat=25"); $i = 1; ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="clearfloat">
                    <?php $values = get_post_custom_values("Image");
                    if (isset($values[0])) { ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                        <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
                        $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
                        alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
                    <?php } ?>
                    <div class="info">
                        <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
                        <div class="meta">
                            [<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]
                        </div>
                        <?php the_excerpt(); ?>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php 	/* Widgetized sidebar, if you have the plugin installed. */ 					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?>
            <?php endif; ?>
            </div>
        </div>
            
            
        <div id="sidebar-ads">
            <a href="http://www.outplaypoker.com"><img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/banners/sidebar-banner.png" alt="Outplay Poker Store" width="300px" height="250px" /></a>
        </div>
        
        <div id="sidebar-bottom"> 
            <?php 	/* Widgetized sidebar, if you have the plugin installed. */ 					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(4) ) : ?> 
            <?php endif; ?>	
        </div>  
    </div>
    PHP:
    Now I want to add an if statement so that the "Weekly Poker Training" articles show five on the homepage or one on all other pages so I tried this:
    
    <?php if(if_front()) { ?>
    <div id="sidebar-top">
            <h3>Weekly Poker Training</h3>
            <div id="sidebar-content">
            <?php query_posts("showposts=5&cat=25"); $i = 1; ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="clearfloat">
                    <?php $values = get_post_custom_values("Image");
                    if (isset($values[0])) { ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                        <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
                        $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
                        alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
                    <?php } ?>
                    <div class="info">
                        <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
                        <div class="meta">
                            [<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]
                        </div>
                        <?php the_excerpt(); ?>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php 	/* Widgetized sidebar, if you have the plugin installed. */ 					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?>
            </div>
        </div>
        <?php } ?>
    
        <?php else { ?>
         <div id="sidebar-top">
            <h3>Weekly Poker Training</h3>
            <div id="sidebar-content">
            <?php query_posts("showposts=1&cat=25"); $i = 1; ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="clearfloat">
                    <?php $values = get_post_custom_values("Image");
                    if (isset($values[0])) { ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                        <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
                        $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
                        alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
                    <?php } ?>
                    <div class="info">
                        <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
                        <div class="meta">
                            [<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]
                        </div>
                        <?php the_excerpt(); ?>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php 	/* Widgetized sidebar, if you have the plugin installed. */ 					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?>
            </div>
        </div>
    
         <div id="sidebar-ads">
            <a href="http://www.outplaypoker.com"><img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/banners/sidebar-banner.png" alt="Outplay Poker Store" width="300px" height="250px" /></a>
        </div>
        
        <div id="sidebar-bottom"> 
            <?php 	/* Widgetized sidebar, if you have the plugin installed. */ 					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(4) ) : ?> 
            <?php endif; ?>	
        </div>  
        <?php } ?>
        <?php endif; ?>
    
    PHP:
    But my code just keeps throwing errors in my face. I was wondering if someone could help me with the simple coding?

    Best Regards,
    Nick
     
    Borduhh, Mar 13, 2011 IP
  2. jwitt98

    jwitt98 Peon

    Messages:
    145
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    All that html code within your if/else statement needs to be echoed. Otherwise it will be sent to the browser regardless of the if/else conditions.
    For example, the first part of your code would look like:
    <?php if(if_front()) { 
    echo '<div id="sidebar-top">
            <h3>Weekly Poker Training</h3>
            <div id="sidebar-content">';
            query_posts("showposts=5&cat=25"); $i = 1;
            while (have_posts()) : the_post();
                echo '<div class="clearfloat">';
                    $values = get_post_custom_values("Image");
                    if (isset($values[0])) { 
    				// the rest of the code....
    ?>
    PHP:
     
    jwitt98, Mar 13, 2011 IP
  3. mmatthew

    mmatthew Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You're wrong, your code will yield the same results as the one below:
    <?php if(if_front()): ?>
    <div id="sidebar-top">
    <h3>Weekly Poker Training</h3>
    <div id="sidebar-content">
           <?php query_posts("showposts=5&cat=25"); $i = 1; ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="clearfloat">
                <?php
                    $values = get_post_custom_values("Image");
                    if (isset($values[0])) { 
    				// the rest of the code....
    ?>
    PHP:
    Borduhh, if the first code you posted works properly, try this:
    
    
    <?php if (if_front()): ?>
    <div id="sidebar">
        <div id="sidebar-top">
            <h3>Weekly Poker Training</h3>
            <div id="sidebar-content">
            <?php query_posts("showposts=2&cat=25"); $i = 1; ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="clearfloat">
                    <?php $values = get_post_custom_values("Image");
                    if (isset($values[0])) { ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                        <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
                        $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
                        alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
                    <?php } ?>
                    <div class="info">
                        <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
                        <div class="meta">
                            [<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]
                        </div>
                        <?php the_excerpt(); ?>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php   /* Widgetized sidebar, if you have the plugin installed. */                     if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?>
            <?php endif; ?>
            </div>
        </div>
            
            
        <div id="sidebar-ads">
            <a href="http://www.outplaypoker.com"><img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/banners/sidebar-banner.png" alt="Outplay Poker Store" width="300px" height="250px" /></a>
        </div>
        
        <div id="sidebar-bottom"> 
            <?php   /* Widgetized sidebar, if you have the plugin installed. */                     if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(4) ) : ?> 
            <?php endif; ?> 
        </div>  
    </div>
    <?php endif; /*end for if (if_front()) statement*/ ?>
    
    PHP:
     
    mmatthew, Mar 14, 2011 IP
  4. jwitt98

    jwitt98 Peon

    Messages:
    145
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I will be the first to admit when I am wrong, so here goes.
    Yes I was wrong as I verified for myself with the simple example below:
    
    <?php
    $var = 0;
    if($var == 1){
    ?>
    <p>Var = 1</p>
    <?php
    }else{
    ?>
    <p>Var != 1</p>
    <?php
    };
    ?>
    
    
    PHP:
    This will only send Var != 1 to the browser.
    I'm actually surprised that I was not aware of this. I have always echoed my html code within conditional statements. Ill chalk it up as another learning experience. In the mean time, please kindly disregard my first post.
     
    jwitt98, Mar 14, 2011 IP
  5. Borduhh

    Borduhh Well-Known Member

    Messages:
    767
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    150
    #5
    @mmatthew: I used that code, and now the sidebar just doesn't appear at all lol. I am looking into why its not appearing and my guess is that the if_home() or is_front_page() functions are working properly.
     
    Borduhh, Mar 15, 2011 IP