The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

first theme help. incorrect post titles (5 posts)

  1. kilgoretrout
    Posted 3 years ago #

    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(); ?>
  2. Jose Castaneda
    Posted 3 years ago #

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

  3. kilgoretrout
    Posted 3 years ago #

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

  4. Jose Castaneda
    Posted 3 years ago #

    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

  5. Michael
    Forum Moderator
    Posted 3 years ago #


    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)

Topic Closed

This topic has been closed to new replies.

About this Topic