0

I have the following shortcode for pullquotes:

//Pullquote shortcode
function pullQuotes($atts, $content = null) {
extract(shortcode_atts(array(
    "author" => ''
), $atts));
return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';
}
add_shortcode("pullquote", "pullQuotes");

Basically I dont want the paragraph containing the $author attribute to be returned if there is no author set (ie: the default '' value).

Any ideas?

0

All you need is some very simple IF logic.

The $author variable is set to be empty here "author" => '' unless an author name is used in your shortcode.

So now that you know the default value of $author you can use IF logic to check if it is empty or not. Two logic operators that are useful are == (equal to) and != (not equal to). We can use either operator but we only need one.

So first let's check if $author is empty.

if ( $author == '' ) {

    //do something here

}

But now we need to do something if $author is not empty. So we can add else to our logic.

if ( $author == '' ) {

    //do something here if $author is empty

} else {

    //do something here if $author is not empty

}

The part of your shortcode function that send the result to the page is this.

return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

So now we can put that inside our IF logic.

if ( $author == '' ) {

    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

} else {


    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

}

But right now the shortcode function returns the same results to the page. We need to adjust the first return line for when there is no author. That looks like this.

if ( $author == '' ) {

    return '<blockquote>'.$content.'</blockquote>';

} else {


    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

}

And the final shortcode.

//Pullquote shortcode
function pullQuotes($atts, $content = null) {
    extract(shortcode_atts(array(
        "author" => ''
    ), $atts));
    if ( $author == '' ) {
        return '<blockquote>'.$content.'</blockquote>';
    } else {
        return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';
    }
}
add_shortcode("pullquote", "pullQuotes");
0

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.