I used this code to display content from my custom fields only where post category id is 5, but all data from the custom fields show above the table in a straight line like a paragraph.
<?php
$post = $wp_query->post; if ( in_category('5') ) {
echo "<table><tr><td> ".the_field('song_title')."</td></td>
<tr><td>
<b>Artist: </b> ".the_field('artist')."</td></tr>
<tr><td>
<b >Song Title:</b> ".the_field('song_title')."</td></tr>
<tr><td>
<b>Song Lenght:</b> ".the_field('song_lenght')."</td></tr></table>
";
} ?>
I used this code to display content from my custom fields only where post category id is 5, but all data from the custom fields show above the table in a straight line like a paragraph.
<?php
$post = $wp_query->post; if ( in_category('5') ) {
echo "<table><tr><td> ".the_field('song_title')."</td></td>
<tr><td>
<b>Artist: </b> ".the_field('artist')."</td></tr>
<tr><td>
<b >Song Title:</b> ".the_field('song_title')."</td></tr>
<tr><td>
<b>Song Lenght:</b> ".the_field('song_lenght')."</td></tr></table>
";
} ?>
Share
Improve this question
edited May 2, 2019 at 15:17
Welcher
3,6481 gold badge20 silver badges24 bronze badges
asked May 2, 2019 at 14:55
PrezidoPrezido
112 bronze badges
2
|
1 Answer
Reset to default 3Welcome to this community Prezido. the_field()
function has echo in it. There is no problem with in_category()
function. You need to use get_field()
function when you echo the values of fields.
I've updated your code. This version should work as expected.
<?php
$post = $wp_query->post;
if ( in_category( '5' ) ) {
echo '<table>
<tr><td> ' . get_field( 'song_title' ) . '</td></tr>
<tr><td><b>Artist: </b> ' . get_field( 'artist' ) . '</td></tr>
<tr><td><b >Song Title:</b> ' . get_field( 'song_title' ) . '</td></tr>
<tr><td><b>Song Lenght:</b> ' . get_field( 'song_lenght' ) . '</td></tr>
</table>
'; }
<tr>
is not closed in the first line, you closetd
tag twice instead oftr
– anton Commented May 2, 2019 at 15:05