1

I create custom field type select

enter image description here

Need display values from custom field outside <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> like bootstrap tabs.

Code 1:

<?php
            global $wp_query;
            $postid = $wp_query->post->ID;
            var_dump($postid);
            echo get_post_meta($postid, 'employee_category', true); 
            wp_reset_query();
        ?>

return - int(269) - id page.

Code 2:

<?php
        $value = get_field( "employee_category" );
        if( $value ) {
            echo $value;
        } else {
            echo 'empty';
        }
        ?>

return - empty.

Code 2 inside loop work correctly.

How display values outside loop?

UPD

Need to display all categorys.

This code

<?php
            global $wp_query;
            $postid = $wp_query->post->ID;
            $value = get_field( "employee_category", 269 );
            var_dump($value);
            if( $value ) {
                echo $value;
            } else {
                echo 'empty';
            }
            wp_reset_query();
        ?>

return null

  • This field belongs to a post or page? you can just try to do global $post on the top of your code, and get the $post->ID. That should work for you. If don't please elaborate your question. – Cesar Henrique Damascena Jul 10 '17 at 12:44
  • This field belongs to page – voronovam Jul 11 '17 at 3:16
2

You need to pass $postid to your get_field function.

<?php
    $value = get_field( "employee_category", $postid );
    if( $value ) {
        echo $value;
    } else {
        echo 'empty';
    }
?>

See docs: https://www.advancedcustomfields.com/resources/get_field/

| improve this answer | |
  • thanks for help, but not working outside loop – voronovam Jul 10 '17 at 11:07
1

You can access a custom field by supplying the post ID in the second arguement.

get_field($selector, [$post_id]);

So in your case you would do the following:

$value = get_field( "employee_category", $postid );

You can view the full documentation here: https://www.advancedcustomfields.com/resources/get_field/

| improve this answer | |
  • 1
    thanks for help, but not working outside loop – voronovam Jul 10 '17 at 11:08
  • Are you getting the postid beforehand, try replacing $postid with 269. So $value = get_field( "employee_category", 269 ); – Erbilacx Jul 10 '17 at 11:12
  • yes, i make this, but it return null. i update my question – voronovam Jul 10 '17 at 11:18
0

If you are trying to echo out all of the choices for that ACF select field you need to use get_field_object('employee_category')not get_field()

<?php
    $value = get_field_object("employee_category");
    if( $value['choices'] ) {
        echo $value['choices'];
    } else {
        echo 'empty';
    }
?>

https://www.advancedcustomfields.com/resources/get_field_object/

| improve this answer | |
  • thank you for version, but not working for me – voronovam Jul 11 '17 at 3:10
0

I make this:

<ul class="nav nav-tabs d-flex justify-content-center flex-wrap team-navs">
                <?php $loop = new WP_Query( array( 'post_type' => 'employee', 'post_status'=>'publish', 'posts_per_page' => -1 ) ); ?>
                <?php
                $counter = 0;
                while ( $loop->have_posts() ) : $loop->the_post();
                    $counter++;
                    $value = get_field( "employee_category" );
                    ?>
                    <li class="nav-item post-<?php the_ID(); ?> <?=($counter == 1) ? 'active' : ''?>">
                        <a class="nav-link" role="tab" href="#<?php echo $value; ?>" aria-controls="home" role="tab" data-toggle="tab"><?php echo $value; ?></a>
                    </li>
                <?php endwhile; wp_reset_query(); ?>
            </ul>

This code display category. But at the same time its dublicates tabs, if if there is post of the same category. And need to make switching tabs...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.