Support » Themes and Templates » first theme help. incorrect post titles

  • Hi. I am new to wordpress, php, and coding in general. I am working on my first theme.

    I’m having a problem with my post titles. The first post title displays correctly. The second, however seems to be just a repeat of the first. The second title is displayed where the third title should be.

    I think it may have something to do with the post counter I put in to set the css div id’s for the posts…i’m not sure if php can have an “if” within an “if”. Can someone help?

    My index.php looks like this:

    <?php get_header(); ?>
    <div id="main">
           <div id="post">
    if (have_posts()) :
       while (have_posts()) :
    if ($counter1==1) echo"<div id='post1'><!--post1 css divider-->\n";
    if ($counter1==2) echo"<div id='post2'><!--post2 css divider-->\n";
    if ($counter1==3) echo"<div id='post3'><!--post3 css divider-->\n";
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    if ($counter1==1) echo"</div><!--end post1 divider-->\n";
    if ($counter1==2) echo"</div><!--end post2 divider-->\n";
    if ($counter1==3) echo"</div><!--end post3 divider-->\n";
            </div><!-- post -->  
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Jose Castaneda



    What exactly are you attempting to do? I’m a little lost.

    I want my front page to display 3 posts. Which I’ve set it to do in wordpress settings.

    I also need to position each post on the front page. I’m trying to do that by giving each post it’s own division id. that’s what the $counter1 variable and if statements do.

    every time the loop runs it increments $counter1 by 1. thus you get div id=post1, post2, etc…

    Moderator Jose Castaneda



    One solution would be:

    $div = 0;
    // Create the initial WordPress loop and if our div is less than three it will run the loop
    while ( have_posts() && $div++<3 ): the_post(); ?>
    	<div class="post-<?php echo $div; ?>">
    		<?php the_content(); ?>
    	</div><!-- post-<?php echo $div; ?> -->
    // End the loop

    Keep in mind this a decent hack. It will keep iterating three times each time if it is in index.php



    Forum Moderator

    the_post() is virtually always used directly after while( have_posts() ) : directly at the start of the loop;

    in layman’s words it does something like: ‘let us go on to the next post and prepare all output stuff’

    (it does not actually ouput anything)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘first theme help. incorrect post titles’ is closed to new replies.