2

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This code does the same as mine is doing so far, which is display the value of the custom field. The custom field has a name( in this case Hake and chips ) and a value. I need to display the name as well
then you can try like this: $field_object = get_field_object( 'hake_and_chips', $post->ID ); echo $field_object['label'];
This related to ACF, a 3rd party plugin and NOT wordpress which uses get post meta. Your solution doesn't show how to get the custom field name.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.