Wordpress PHP help

Discussion in 'Coding Help' started by Nugget, Jan 13, 2011.

  1. Nugget

    Nugget New Member

    Hi, I'm using both wordpress and php for the first time so please try and bear with me.
    I'm trying to extract the titles of each of my wordpress blog posts and display them in a table. Extracting the the post titles to display works fine it's doing it in such a way as to display them in the correct columns that's causing me trouble.

    The following is the snippet of code from my latest attempt to do this

    PHP:
     <table width="700" height="240" border="0" align="center">
      
      <?php if (have_posts()) : the_post(); ?>
      <tr>


          <td width="300" height="80" background="images/table5.png" style="background-repeat:no-repeat"><blockquote>
             <p class="class2"><div class="post" id="post-<?php the_ID(); ?>">
         <?php the_title(); ?></p>
           </blockquote></td>
           
           <td width="100px">
           </td>
          
             <?php while (have_posts()) : the_post(); ?>
            

          <td width="300" height="80" background="images/table5.png" style="background-repeat:no-repeat"><blockquote>
             <p class="class2"><div class="post" id="post-<?php the_ID(); ?>">
         <?php the_title(); ?></p>
           </blockquote></td>
      
        </tr>
         
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

     </table>
    This is someway close to working as it displays the first row correctly but the titles stop displaying in the second column of the following rows.

    View attachment 341


    The <while> statement seems to stop working after the first run through the loop

    As I said I'm new to this so any help would be appreciated
     
  2. Little Pig

    Little Pig New Member

    PHP and Wordpress

    Hi Nugget,

    Your loop is in the wrong place.

    Try this:

    Code:
    <!-- test  solution-->
    <?php if (have_posts()) { ?>
    
        <table width="700" height="240" border="0" align="center">
      
    
       <?php while (have_posts()) : the_post(); ?>
          <?php $cc++;
          echo ( ($cc % 2) ? "<tr>" : "" );?>
    
          <td width="300" height="80" background="images/table5.png" style="background-repeat:no-repeat">
          <blockquote>
          <p class="class2"><div class="post" id="post-<?php the_ID(); ?>">
          <?php the_title(); ?> </p>
         </blockquote></td>
            <?php  echo ( ($cc % 2) ? '<td width="100px"> </td>' : '</tr>' );?>     
    
       <?php endwhile; ?>
     </table>
     <?php } else {?>
     <p>
     <?php _e('Sorry, no posts matched your criteria.'); ?>
     </p>
     <?php } ?>
    <!-- end test solution -->
     
  3. Nugget

    Nugget New Member

    Thanks a million, that's absolutely perfect

    The one other problem I have is that this isn't returning all the post titles. It returns the first 5 but I have another 3 or so that don't appear. Is there any reason why this should be happening?
     
  4. Nugget

    Nugget New Member

    I'm gonna drag this up again as I've come across yet another problem. After using google for hours I still can't figure it out.

    This is particularly annoying as I've done this perfectly before but can't for the life of me remember how.
    I've set up my page (above) to display my post titles. Now if I press on the title it brings me to the post in the default wordpress template.
    How do I go about displaying the post in my website layout? All I need to display is the title, date and the content of the post.
     

Share This Page