简体   繁体   中英

Output recent Wordpress posts in tables, 2 posts wrapped in tr tag?

I'm designing a custom wordpress theme where I'd like to display the recent posts on the home page horizontally in tables, two posts per row. It would look like this:

http://www.numenfilm.com/preview/issues.php

So I'd like my index.php file to kick out code like this:

<table>
    <tr>
      <td>
        post contents here
      </td>
      <td>
        post contents here
      </td>
    </tr>
    <tr>
      <td>
        post contents here
      </td>
      <td>
        post contents here
      </td>
    </tr>
</table>

The table and td tags are easy. The problem I've having is getting the tr tags to wrap around the posts in groups of two.

I found this post elsewhere on stack overflow:

Table, TR each 2 loop, PHP, HTML

The last suggestion seems to be what I'm looking for, but I haven't had any luck integrating it with wordpress. Here's the code from my index.php page:

<?php get_header(); ?>
  <div id="main_content">
  <h1><a href="<?php echo get_settings('home'); ?>">Numen News &amp; Blog</a></h1>
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("showposts=5&paged=$paged");
    ?>

      <?php if (!is_single() && !is_page() && !is_front_page()) : ?><h1><?php wp_title(' ', true, 'right'); ?></h1><?php endif; ?>

        <?php /* begin the loop */ if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>
      <?php if (is_page()) : /* show page contents */ ?>
                <div class="pagecontent" id="post-<?php the_ID(); ?>">
                    <h1><?php the_title(); ?></h1>
                    <?php the_content('Read more &rarr;'); ?> 
                    <p><?php wp_link_pages('next_or_number=number&pagelink=page %'); ?></p>
                    <p><?php edit_post_link('Edit', '[ ', ' ]'); ?></p>  
                </div>
                <?php comments_template(); ?>

            <?php else : /* show post contents */ ?>
                <div class="post" id="post-<?php the_ID(); ?>">
                    <div class="postcontents">
                        <?php if (!is_single()) : ?>
                            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                        <?php else : ?>
                            <h1><?php the_title(); ?></h1>
                        <?php endif; ?>

            <div class="posted">Posted <?php the_time('F jS, Y') ?> by <?php the_author_link(); ?> </div>
                        <?php the_content('Read more &rarr;'); ?> 
                        <p><?php wp_link_pages('next_or_number=number&pagelink=page %'); ?></p>    
                    </div>

                    <?php if(is_single) comments_template(); ?>
                </div>
            <?php endif; /* end if page or post */ ?>
        <?php endwhile;/* end the main loop */ ?>

        <?php /* post navigation */ ?>
        <?php if (is_single()) : ?>
            <div class="postnavigation">
                <?php previous_post_link('%link', '<span>&larr;</span> Previous post') ?>
                <?php next_post_link('%link', 'Next post <span>&rarr;</span>') ?>
            </div>
        <?php endif; ?>
        <?php if (  $wp_query->max_num_pages > 1 ) : ?>
            <div class="postnavigation">
                <?php next_posts_link('Older posts <span>&rarr;</span>') ?>  
                <?php previous_posts_link('<span>&larr;</span> Newer posts') ?>
            </div>
        <?php endif; ?>
        <?php endif; /* end if have_posts */ ?>      
  </div><!--END main_content -->
<?php /* sidebar */ get_sidebar(); ?>
<?php /* footer */ get_footer(); ?>

Any suggestions?

Don't use tables .

If you want to continue to write bad markup you need to do something like this.

<table>
//wordpress loop starts ( you left that part out)
 <td>
   <tr>
     <?php the_content('Read more &rarr;'); ?>
   </tr>
  </td>
// loop ends
</table>

try this code, the main code inside td is omitted for readability. the code I added and you should add are most in dark red color, I am sure you can figure that out.

  <table>
  <tr>
  <?php $counter=1; ?>
  <?php while (have_posts()) : the_post(); ?>
       <td>
       <?php if (is_page()) : /* show page contents */ ?>
           :
           :
        <?php endif; /* end if page or post */ ?>
        </td>
        <?php if($counter%2==0) echo '</tr><tr>';
              $counter++;
        ?>
    <?php endwhile;/* end the main loop */ ?>
    </tr>
    </table>

Here is the full code from index.php using the solution posted by bingjie2680 above. It works.

<?php get_header(); ?>

  <div id="main_content">

  <h1><a href="<?php echo get_settings('home'); ?>">Numen News &amp; Blog</a></h1>

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("showposts=5&paged=$paged");
    ?>

      <?php if (!is_single() && !is_page() && !is_front_page()) : ?><h1><?php wp_title(' ', true, 'right'); ?></h1><?php endif; ?>

        <?php /* begin the loop */ if (have_posts()) : ?>

      <table>
        <tr>
        <?php $counter=1; ?>
        <?php while (have_posts()) : the_post(); ?>
             <td>
             <?php if (is_page()) : /* show page contents */ ?>


                <div class="pagecontent" id="post-<?php the_ID(); ?>">
                    <h1><?php the_title(); ?></h1>
                    <?php the_content('Read more &rarr;'); ?> 
                    <p><?php wp_link_pages('next_or_number=number&pagelink=page %'); ?></p>
                    <p><?php edit_post_link('Edit', '[ ', ' ]'); ?></p>  
                </div>
                <?php comments_template(); ?>


              <?php else : /* show post contents */ ?>

                <div class="post" id="post-<?php the_ID(); ?>">
                    <div class="postcontents">

                        <?php if (!is_single()) : ?>
                            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                        <?php else : ?>
                            <h1><?php the_title(); ?></h1>
                        <?php endif; ?>

            <div class="posted">Posted <?php the_time('F jS, Y') ?> by <?php the_author_link(); ?> </div>

                        <?php the_content('Read more &rarr;'); ?> 
                        <p><?php wp_link_pages('next_or_number=number&pagelink=page %'); ?></p>    
                    </div>

                    <?php if(is_single) comments_template(); ?>
                </div>


        <?php endif; /* end if page or post */ ?>
            </td>
            <?php if($counter%2==0) echo '</tr><tr>';
                  $counter++;
            ?>
        <?php endwhile;/* end the main loop */ ?>
        </tr>
      </table>



        <?php /* post navigation */ ?>
        <?php if (is_single()) : ?>
            <div class="postnavigation">
                <?php previous_post_link('%link', '<span>&larr;</span> Previous post') ?>
                <?php next_post_link('%link', 'Next post <span>&rarr;</span>') ?>
            </div>
        <?php endif; ?>
        <?php if (  $wp_query->max_num_pages > 1 ) : ?>
            <div class="postnavigation">
                <?php next_posts_link('Older posts <span>&rarr;</span>') ?>  
                <?php previous_posts_link('<span>&larr;</span> Newer posts') ?>
            </div>
        <?php endif; ?>

        <?php endif; /* end if have_posts */ ?>      

  </div><!--END main_content -->

<?php /* sidebar */ get_sidebar(); ?>
<?php /* footer */ get_footer(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM