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

2
  • 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. Commented Jul 10, 2017 at 12:44
  • This field belongs to page Commented Jul 11, 2017 at 3:16

4 Answers 4

3

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/

0
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/

2
  • Are you getting the postid beforehand, try replacing $postid with 269. So $value = get_field( "employee_category", 269 ); Commented Jul 10, 2017 at 11:12
  • yes, i make this, but it return null. i update my question Commented Jul 10, 2017 at 11:18
1

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...

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/

0

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.