Support

Account

Home Forums Front-end Issues Need Help with True/False Field

Solved

Need Help with True/False Field

  • I’ve put together a gallery of work, using a custom post type and custom post type taxonomies. On a main category page like this one, I want most of the items to link to a larger version of the image associated with that post type. But, for some, I actually want it to go to the page for that post (Radio Advertising, and Television & Video Advertising).

    I created a “True/False” field for posts like these that have media on them. I’m trying to insert the permalink where they appear on this page and it’s not working. Here is my code:

    <a href="<?php $type = the_field('media');
    if($type = ('null'))
    {
    echo the_field('main_image');
    }
    else
    {
    echo the_permalink();
    }
    ?>">

    Can someone help?

  • In what sense is it not working? What happens?

    Try this:
    if (get_post_meta($post->ID, 'media', true) == '' )
    insteaf of:
    if($type = ('null'))

    Remove this, it’s not needed for the new condition above:
    $type = the_field('media');

    Don’t know if it makes a difference, but it’s worth the try 🙂

  • YES! YES! YES! You saved me! I’ve been banging my head against the wall for hours. This was great, thank you Vikestart!

  • I may have spoke too soon. This seems to work for all of the posts I created BEFORE I changed the php source code, but new ones that I save are not working correctly – they are linking to the post.

    So, I guess this didn’t work. Am I going about this wrong? Other thoughts?

  • You must have changed the code somehow. I was browsing your site about 10-20 minutes ago and everything seemed to work. The advertising ones led to a new page, while the other ones expanded the image. I see that when I visit your site now, that’s all gone; all the items lead to a new page.

    The code I provided above should work provided that the fields still have the same field names.

    You could try 0 for false:
    if (get_post_meta($post->ID, 'media', true) == '0' )

    EDIT: I just browsed your site again and now it seems to work!

  • Last night I changed it to what I had previously. Which worked for all but the two advertising items. I put in you’re code:
    if (get_post_meta($post->ID, 'media', true) == '0' )

    But take a look, now most of the items link to the permalink, not the image file as is intended, so something isn’t right. Thoughts?

  • I think the ‘0’ was the key. It is working, but for some reason I have to go back and save all of them again now to get it to work. Thanks again!

  • Hi @shawn00m

    Please remember that the_field will echo the value, and get_field will return it for use as a variable.

    Thanks
    E

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Need Help with True/False Field’ is closed to new replies.