Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using a widget and adding this:

<div id="testimonials"><h4>[acf field="testimonial" ]
<strong>[acf field="name" ]</strong></h4>

<h4>[acf field="testimonial_2" ]
<strong>[acf field="name2" ]</strong>
</h4></div>

The issue is that I am getting empty tags when there are NO testimonials, and thus some styling is being picked up in the empty tags.

I've messed around with some css/jquery, but I am thinking it's probably better to actually generate the html tags as well but I am not sure how to go about that.

Is there a way to generate tags using shortcodes?

I wouldn't know how to do this but I was thinking an if statement in loop, get_field(); would work too?

Any ideas are appreciated.

share|improve this question
    
It relativly easy - but if your not in a single.php file? meaning not in post? are you planing to release this widget to the public or is it for personal use? isnt it better to check if value exists and if so get the tags ELSE echo 'no tags found'; ? –  Sagive SEO Aug 14 '13 at 16:21
    
it's for personal use. yeah, I think it will be from the single.php file, posts and pages. I agree it's better to check, I can't think it in pseudo code but just not sure how to add it. I want to add it to the sidebar though so somehow hook it into the sidebar widget –  user31344 Aug 14 '13 at 17:20
    
it should be easy enough to build a simple widget... but, i think there is a problem in the way you are thinking about it... are you just looking for a text widget you can implement shortcodes in? cuz there is a snippet that activates that. i am sorry, its hard to understand what would that widget display. how can i construct a widget that checks if X exists without knowing what X is and where the data is coming from... i can only reccomend you come back better preppered or maybe read about building a simple widget: goo.gl/VwP54 –  Sagive SEO Aug 14 '13 at 22:04
add comment

1 Answer

up vote 0 down vote accepted

Your shortcode is designed to print results directly on the page so it isn't possible to first check if the output is empty before printing. However, the shortcode runs a specific function and you can use that function directly in a couple ways.

  • Method 1: You can add some additional PHP logic to your template code.
  • Method 2: You can create a custom shortcode with the additional HTML you need.

The function used by [acf field="something"] is called the_field(). Like I mentioned above the_field() prints directly to the page which doesn't work for your purposes. Instead you can use get_field() and print it when you choose.

Using get_field() is very similar to using the shortcode. It accepts two parameters; 1. the name of the field, and 2. the post ID.

get_field('FieldName','ID')

Method 1

Some simple PHP logic to check if the value is empty before printing to the page would look like this.

<?php
$testimony_text = get_field('testimony', $post->ID );
$testimony_name = get_field('name', $post->ID );
if ( $testimony_text != '' && $testimony_name != '' ) {
    echo "<h4>{$testimony_text} <strong>{$testimony_name}</strong></h4>";
}
?>

Method 2

A custom shortcode that wraps your HTML around the output might look like this. Place this in your theme's functions.php file.

function acfcustom_func($atts) {
       extract( shortcode_atts( array(
        'text' => '',
        'name' => '',
    ), $atts ) );

    $id = get_the_id();
    $testimony_text = get_field($text, $id );
    $testimony_name = get_field($name, $id );

    if ( $testimony_text == '' || $testimony_name == '' ) return; 

    $output = "<h4>{$testimony_text} <strong>{$testimony_name}</strong></h4>";

    return $output;
}
add_shortcode( 'acfcustom', 'acfcustom_func' );

To use the shortcode paste it in your post body or sidebar widget like this.

[acfcustom text="testimonial" name="name"]

NOTE: A shortcode won't normally work in a sidebar widget but this can be changed. There are plugins available that makes it possible to run shortcodes in sidebar widgets.

Description of how method 2 works

This could get really long really fast. Here's a quick overview. Ok, so we first build a simple shortcode function. The Wordpress Codex has some examples here. We pass into the function the $atts array which holds the parameters (testimonial and name) we are going to specify in the shortcode. We then extract the parameters from the $atts array and specify variable names for them. Now we can use the parameters in the get_field() function. The function is specific to the Advanced Custom Fields plugin you are using. The results of get_field() are set to new variables ($testimony_text and $testimony_name). Now using some IF logic we check if either of the variables is empty. If it is we end the function and return nothing. If both variables have content we concatenate them into the HTML string and then return the string to be printed on the page.

share|improve this answer
    
wow, that (method 2) worked brilliantly!!! Thank you!!!! Could you pls explain what that function does. :) –  user31344 Aug 15 '13 at 19:20
    
@user31344 I've added a brief description of method 2 to my response. –  epilektric Aug 15 '13 at 19:46
    
cool, thanks so much!! –  user31344 Aug 15 '13 at 19:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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