Skip to main content

Displaying wordpress post contents in multiple columns

There are some template files for displaying post contents in excerpt or full-length form in wordpress like index.php, category.php, archive.php etc. If it is wanted to be displayed post contents in the form of multiple columns, following changes may give the desired result.

put these lines of code after <?php if ( have_posts() ) : ?> and just before <?php while ( have_posts() ) : the_post(); ?> loop:

echo "<table>";
$column = 0
;

inside the while loop, put <tr> and <td> just after <?php while ( have_posts() ) : the_post(); ?>

if ($column == 0) echo "<tr>";
echo "<td width='50%'>";


Now it comes to close <td> tag after the post content. Post content generally appears similar to <?php get_template_part( 'content', get_post_format() ); ?>

echo "</td>";
$column=$column+1;


Then if the number of columns are reached desired value, close the <tr> tag:

if ($column == 2) {
echo "</tr>";
$column=0;
}


close the <table> after the <?php endwhile; ?> loop:

echo "</table>";
These changes produce two columns. For more colums just change <td width='50%'> and if ($column == 2) with appropriate values accordingly.


Comments

Popular posts from this blog

Find and replace with sed command in Linux

Find and replace feature is always handy. It can turn into a torture when it comes to change or delete a simple constant string in a text file. There is a handy tool in linux for doing these kind of tihngs. Actually sed is not a text editor but it is used outside of the text file to make changes.