0

Please if someone could help ill be very greatful.

I know how to use the if statement using Advanced Custom Fields on posts - but this time I want to pull the value from the Advanced Custom Fields in the category, I have managed to put this code together - but can't work out how to make it an if statement.

This code pulls the value from the category custom field - how can i convert it to if value empty do "X" if true do "Z"

<?php
    $queried_object = get_queried_object();
    $taxonomy       = $queried_object->taxonomy;
    $term_id        = $queried_object->term_id;
    the_field( 'logo_src', $taxonomy . '_' . $term_id );
?>

My failed attempt:

<?php
    $queried_object = get_queried_object();
    $taxonomy       = $queried_object->taxonomy;
    $term_id        = $queried_object->term_id;
    if ( get_field('logo_src', 'true', $taxonomy . '_' . $term_id ) ) {
?>
  do x 
<?php } else { ?>
   do z 
<?php } ?>

Thanks in advance!

  • the_field echoes, you have to use get_field. – Milo Mar 24 '16 at 20:05
  • Hi Thanks alot - well - im not longer getting a php error - but it dont work? – Danb91 Mar 24 '16 at 20:25
  • ` <?php $queried_object = get_queried_object(); $taxonomy = $queried_object->taxonomy; $term_id = $queried_object->term_id; if (get_field('logo_src', 'true', $taxonomy . '_' . $term_id)) {?> do x <?php } else {?> do z <?php }?>` – Danb91 Mar 24 '16 at 20:26
  • @Milo can you help? – Danb91 Mar 24 '16 at 20:36
  • 1
    If (empty ( $value )) { } else { } – jgraup Mar 25 '16 at 0:56
0

You're on the right track.

However your parameters for get_field() are incorrect in your example. The second value should be an ID or false (true is not valid):

// Incorrect. Uses 'true' as second parameter:
if ( get_field('logo_src', 'true', $taxonomy . '_' . $term_id ) ) {

Try this. Use get_field() to return the value of the field in your conditional statement argument. Use the_field() to print the value of the field:

$queried_object = get_queried_object();
$taxonomy       = $queried_object->taxonomy;
$term_id        = $queried_object->term_id;

if ( get_field( 'logo_src', $taxonomy . '_' . $term_id ) )
{
    the_field( 'logo_src', $taxonomy . '_' . $term_id );
}
else
{
    echo 'Nope.';
}

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.