Sidney Sousa
Sidney Sousa

Reputation: 3584

How to output custom field name and value in Wordpress

I created a custom post type and am adding a few custom fields into it.

enter image description here

Currently my loop looks like this:

<?php
    //* The Query
    $exec_query = new WP_Query( array (
      'post_type' => 'jobs',
      'job_role'  => 'fryking',
      'posts_per_page' => 4,
      'order' => 'ASC'
    ) );

    //* The Loop
    if ( $exec_query->have_posts() ) {       
        while ( $exec_query->have_posts() ): $exec_query->the_post();
            echo '<div class="subcategory">';
            echo '<h3 class="cat_title">';
                the_title();
            echo '</h3>';?>
                <div class="cat_content"> 

                    <div class="left">
                        <?php the_content(); 
                            $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
                            the_field('hake_and_chips');
                        ?>
                    </div>                                  
                    <div class="right">
                        <?php 
                        echo '<div class="menu_image" style="background: url('. $url.')">';
                        echo '</div>';?>
                        </div>
                    </div>
            </div>         
                <?php           
                 endwhile;        

        //* Restore original Post Data
        wp_reset_postdata();
    }
?>

I managed to get my field value with this code:

the_field('hake_and_chips');

How can I get the field name?

Hope you can help

Upvotes: 2

Views: 6216

Answers (2)

M. Inam
M. Inam

Reputation: 329

You don't need

echo get_post_meta($post->ID, 'hake_and_chips', true);

instead you can try simple approach as mentioned here by Ian Dunn

echo $post->hake_and_chips

Upvotes: 0

Gajjar Chintan
Gajjar Chintan

Reputation: 450

These fields are stored in post meta table so you can get this custom field value using get_post_meta function also.

Try this code to get single value of custom field:

echo get_post_meta($post->ID, 'hake_and_chips', true);

Hope this will helpful for you. Thanks.

Upvotes: 3

Related Questions