0

How can I show the title when a checkbox is checked. I use Advanced custom fields

my field name is nav

This is what i currently have.

<?php if(get_field('nav')) { ?> 
   <li> <a href="<?php the_permalink()?>" class="active"><?php the_title();?></a>

</li> 

<?php } ?>
5
  • and the nav field returns exactly what? what type of field is it..? please try var_dump(get_field('nav'));
    – honk31
    Commented Oct 18, 2018 at 13:28
  • Hi @honk31, The field type is checkbox. Atm nav returns nothing. What i have is a checkbox that shows on every page. And what im trying to do is if that checkbox is checked echo the pages title. Would u know how?
    – Gago
    Commented Oct 18, 2018 at 13:34
  • if you only use the field for the title, consider to change it from checkbox to true/false. it will still be a checkbox, but will return true if checked and false if not. or post your setting for the select field and we check, whats wrong there..
    – honk31
    Commented Oct 18, 2018 at 13:37
  • Can you post a bit more of your code? I am interested in the context, where exactly do you call for the_title(), and why is it in a li element? Are there multiple titles?
    – djboris
    Commented Oct 18, 2018 at 19:23
  • After looking at your code I would say it should already work the way you want. The if condition evaluates to true and the title is displayd, if it gets a truthy (i.e. the checkbox is checked in a page edit screen) value. Otherwise the condition is falseand the title would not be displayed. What does var_dump(get_field('nav')); give you if you have the checkbox checked in a page edit screen? Commented Oct 21, 2018 at 19:19

2 Answers 2

0

You would like to use ACF true/false field > link

Then basically in your template wrap the title element in conditional, like this:

if( get_field( 'title_show_or_whatever' ) ) {
    // The title element
}
4
  • Thanks dboris, What I have tried is : <?php if(get_field('nav'')) { ?> <li> <a href="<?php the_permalink()?>" class="active"><?php the_title();?></a> </li> <?php } ?> But this doesn't work
    – Gago
    Commented Oct 18, 2018 at 13:06
  • my field name is nav
    – Gago
    Commented Oct 18, 2018 at 13:08
  • @Gago You have a typo in your code, in get_field('nav''), ending string with two single quotes. Consider enabling WP Debug in your wp-config.php file: define( 'WP_DEBUG', true );
    – djboris
    Commented Oct 18, 2018 at 13:13
  • I miss typed. I added code in my original question. I have done it your way but get nothing when i try to get title. My goal is when check box is checked echo that page's title. Please if u could help
    – Gago
    Commented Oct 18, 2018 at 13:21
-1

You might want to try php alternative syntax for your if statement.

<?php if (get_field('nav')): ?>
    <a href="<?php the_permalink()?>" class="active">
        <?php the_title();?> 
    </a>
<?php endif; ?>

Your Answer

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

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