WordPress: Display a table with custom fields at the end of a post

If you have defined a few custom fields in your post and want them appended in a tabular form to the post, you have two possibilities:

  1. Add some code in content.php just after the call to the_content() to display the table.
  2. Add a filter for ‘the_content’ in order to append the table to the content.

The difference is basically that:

  • In the second solution, the table will be in the entry-content DIV tag. If you want to have it outside of this div, you need to make the change in content.php.
  • With the first solution, if you have another plugin (e.g. a related post plugin) using the second solution to append something to the content, this will come between the original content and the table.

At first I used the first solution because it was straight forward. But after installing the YARPP plugin, the related posts were displayed between the content of the post and the table. So I switched to the second solution.

For this you need to create a new function in functions.php getting the content as parameter and appending the HTML code for the table. And you need to add a filter for ‘the_content’ using this function.

function add_product_spec_to_content($content) {
	if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '<table id="product-specification" cellspacing="0" border="0" style="border-collapse:collapse;"><tbody>'; }

	if(get_field('befestigung')) { $content = $content . '<tr><td>Befestigung</td><td>' . get_field('befestigung') . '</td></tr>'; }
	if(get_field('gurtsystem')) { $content = $content . '<tr><td>Gurtsystem</td><td>' . get_field('gurtsystem') . '</td></tr>'; }
	if(get_field('bezug')) { $content = $content . '<tr><td>Bezug</td><td>' . get_field('bezug') . '</td></tr>'; }
	if(get_field('gruppe')) { $content = $content . '<tr><td>Gruppe</td><td>' . get_field('gruppe') . '</td></tr>'; }
	if(get_field('typ')) { $content = $content . '<tr><td>Typ</td><td>' . get_field('typ') . '</td></tr>'; }
	if(get_field('gewicht')) { $content = $content . '<tr><td>Gewicht</td><td>' . get_field('gewicht') . ' kg</td></tr>'; }
	if(get_field('amazon')) { $content = $content . '<tr><td>Preis</td><td>' . get_field('amazon') . '</td></tr>'; }

	if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '</tbody></table>'; }

	return $content;
}

add_filter( 'the_content', 'add_product_spec_to_content', 1150 );

I here use 1150, because I know YARPP uses 1200. And since I want to append my table before YARPP appends the related posts, I need to use a value less than 1200. But the value should be high enough so that the table is displayed after any other appended things.

Here an example of how it looks like (with the YARPP plugin enabled).

3 thoughts on “WordPress: Display a table with custom fields at the end of a post

  1. Hello! I simply would like to give you a huge thumbs up for your excellent
    info you have got here on this post. I am coming back to your web site for more
    soon.

Leave a Reply

Your email address will not be published. Required fields are marked *