admin管理员组

文章数量:1023242

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 First <tr> is not closed in the first line, you close td tag twice instead of tr – anton Commented May 2, 2019 at 15:05
  • Welcome to WordPress Stack Exchange! We love to help. But code reviews are considered off-topic as they simply are too localized, whereas this Q+A site seeks to provide canonical answers that also help future readers. – norman.lol Commented May 2, 2019 at 15:37
Add a comment  | 

1 Answer 1

Reset to default 3

Welcome 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>
    ';  }

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 First <tr> is not closed in the first line, you close td tag twice instead of tr – anton Commented May 2, 2019 at 15:05
  • Welcome to WordPress Stack Exchange! We love to help. But code reviews are considered off-topic as they simply are too localized, whereas this Q+A site seeks to provide canonical answers that also help future readers. – norman.lol Commented May 2, 2019 at 15:37
Add a comment  | 

1 Answer 1

Reset to default 3

Welcome 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>
    ';  }

本文标签: postsHaving Issues with incategoryneed help