WordPress.org

Ready to get started?Download WordPress

Forums

NextGEN Custom Fields
[resolved] conditional statements based on custom field content (4 posts)

  1. Grumbledook
    Member
    Posted 3 years ago #

    Hi,

    This plugin is a great help for some custom design/functionality I wanted to implement on my new website.

    I've got it working as I'd like fine already though I think with some php know how it could be easier.

    I've got some custom fields on my images and then on the gallery template there is some code wrapped around what goes in the custom field. The trouble with this is that that extra code wrapped around still shows up for those images where the custom field is blank.

    There probably is an easy way to do an if statement to check if the custom field is empty and if it is then don't display anything, else if there is something in the custom field, wrap this code around it. I'm just a html+css monkey though and I've given writing an if statement a go, though I can't seem to work out the right syntax.

    The current work around means putting the whole code each time into the custom field. That isn't very elegant compared to just putting the url I wanted to link to in the custom field though.

    For example:

    If custom field isn't empty do:

    <a class="more-icon" title="Read Article" href="http://<?php echo $image->ngg_custom_fields["link"]; ?>">Read more</a>

    else do:

    nothing

    Then I could just put
    www.abc123.com
    into the custom field.

    Instead of putting
    <a class="more-icon" title="Read Article" href="http://www.abc123.com">Read more</a>
    into the custom field.

  2. Grumbledook
    Member
    Posted 3 years ago #

    I think I've worked it out.

    <?php
    if ($image->ngg_custom_fields["link"]!=NULL)
      {
      echo '<a class="more-icon" title="Read Article" href="http://';
      echo $image->ngg_custom_fields["link"];
      echo '">Read More';
      }
    ?>

    I don't know if that's the best way of doing things but it seems to work. If anyone more clued up on php wants to tidy it up or give some pointers then I'm all ears!

    Obviously if you wanted to check your custom field contained something specific rather than it being empty you can change !=NULL to =="123" assuming you wanted to check that the custom field was 123. The above code I got it to check if it wasn't empty, and if it wasn't that insert the start of the link, then insert the content from the custom field and then insert the end of the link. If the custom field is empty then it does nothing.

  3. shauno
    Member
    Plugin Author

    Posted 3 years ago #

    Hey Grumbledook

    Nice work on making a plan to figure it out. Just a suggestion; By checking if it is NULL, it might show 'blank' fields still (silly programming thing about NULL not being equal to FALSE or [BLANK STRING] or [ZERO].
    Enough with the geeky, here's what you can do:

    <?php
    if ($image->ngg_custom_fields["link"])
      {
      echo '<a class="more-icon" title="Read Article" href="http://';
      echo $image->ngg_custom_fields["link"];
      echo '">Read More';
      }
    ?>

    All I've done is remove the !=NULL, and now it should work a little more as expected :)

  4. Grumbledook
    Member
    Posted 3 years ago #

    Cheers shauno,

    That's something I doubt I'd ever have worked out if I'd come into a problem with it. So you change to the if statement just checks that there is something in the field correct?

    I don't know if anyone else has mentioned it, but they also work if you use a gallery made up of nextgen image tags as well which is really helpful imo.

    Was really pleased to find you had made this plugin, I was thinking over the weekend the easiest way I could implement my idea was if nextgen had custom fields. I was about to email alex about implementing them but thankfully a quick google for them before I did showed me you'd already done the job!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic