Forums

[resolved] [closed] What's wrong with my code? (conditional statements) (2 posts)

  1. tjobbe
    Member
    Posted 2 years ago #

    OK, what I'm trying to do is have a PAGE as my home page in wordpress, this I have enabled in the settings.

    On the home page I would also like to show the latest three posts from a specific category. There will not be a sidebar.

    I only want the three posts to show up on the home page, along with whatever content the home page has. Is this possible?

    So I'd have a conditional statement that says, "if it's the home page, show the content AND the latest 3 posts from category 3" OR "just show the page content"

    This code sort of does that, but somehow shows whatever is in my ELSE statement too.

    What have I done wrong?

    <?php get_header(); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php if (is_front_page()) { ?>
    	<div id="content">
    		<div class="entry">
    			<h2><?php the_title(); ?></h2>
    			<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    		</div>
    	</div>
    	<?php
        $blog_query = 'showposts=3&cat=3&paged='.$paged;
    	$posts = query_posts($blog_query);
    	while (have_posts()) : the_post(); ?>
    
    	<?php endwhile; ?>
    <?php } else { ?>
    <div id="content">
    	<div class="entry">
    	<h2><?php the_title(); ?></h2>
    	<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    	</div>
    </div>
    <?php } ?>
    
    <?php endwhile; endif; ?>
    
    <?php get_footer(); ?>
  2. tjobbe
    Member
    Posted 2 years ago #

    Right, so I've now edited the above code slightly, and it does give me pages as I want them - except for the home page.

    The home page now shows me an empty <div id="sidebar"></div> as well as a populated one - which is what I want.

    I don't understand why I'm getting this empty sidebar div in my html?

    Sorry, testing locally so unable to provide a link, but here's my page.php:

    <?php get_header(); ?>
    
    <div id="content">
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php if (is_front_page()) { ?>
    	<h2><?php the_title(); ?></h2>
    	<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    	<?php
    	$blog_query = 'showposts=3&cat=3&paged='.$paged;
    	$posts = query_posts($blog_query);
    	while (have_posts()) : the_post(); ?>
    </div>
    <div id="sideBar">
    	<?php endwhile; ?>
    	<?php } else { ?>
    
    	<h2><?php the_title(); ?></h2>
    	<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    	<?php } ?>
    
    	<?php endwhile; endif; ?>
    	</div>
    
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.