WordPress.org

Ready to get started?Download WordPress

Forums

first theme help. incorrect post titles (5 posts)

  1. kilgoretrout
    Member
    Posted 1 year 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">
    <?php
    $counter1=0;
    if (have_posts()) :
       while (have_posts()) :
    	$counter1++;
       ?>
    
    <?php
    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>
    
    <?php
          the_post();
          the_content();
    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";
       endwhile;
    endif;
    
    ?>
            </div><!-- post -->  
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. Jose Castaneda
    Member
    Posted 1 year ago #

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

  3. kilgoretrout
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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; ?> -->
    <?php
    // End the loop
    endwhile;

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

  5. alchymyth
    Forum Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/the_post

    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