Works for a Living
Works for a Living

Reputation: 1293

If attribute's value is one thing, echo another in shortcode

Here's a shortcode of mine:

    function sc_link( $atts ) {
    extract( shortcode_atts(
        array(
            'page' => '',
            'style' => 'button',
            'window' => 'self',
            'label' => 'Missing Label Tag: label=""',
        ), $atts )
    ); 
    return '<a href="' . $page . '" class="' . $style. '" target="_' . $window . '">' . $label . '</a>';
}
add_shortcode( 'mylink', 'sc_link' );

What I want to be able to do is a conditional before the return: if $window = 'new' then echo 'blank'.

Upvotes: 0

Views: 1581

Answers (2)

Dava Gordon
Dava Gordon

Reputation: 192

Think this is what your trying to do what id suggest doing is leaving window blank and just doing an if statement on that

How you want it

    function sc_link( $atts ) {
    extract( shortcode_atts(
        array(
            'page' => '',
            'style' => 'button',
            'window' => 'self',
            'label' => 'Missing Label Tag: label=""',
        ), $atts )
    ); 

    if($window == "new"){
    $link = '<a href="' . $page . '" class="' . $style. '" target="_blank">' . $label . '</a>';
    }else{
    $link = '<a href="' . $page . '" class="' . $style. '" target="_self">' . $label . '</a>';    
    }

    return $link;
}
add_shortcode( 'mylink', 'sc_link' );

How it should be

    function sc_link( $atts ) {
    extract( shortcode_atts(
        array(
            'page' => '',
            'style' => 'button',
            'window' => '',
            'label' => 'Missing Label Tag: label=""',
        ), $atts )
    ); 

    if(!$window){
        $link = '<a href="' . $page . '" class="' . $style. '" target="_self">' . $label . '</a>';
    }else{
        $link = '<a href="' . $page . '" class="' . $style. '" target="' . $window . '">' . $label . '</a>';    
    }

    return $link;
}
add_shortcode( 'mylink', 'sc_link' );

The your shortcode will be link 

[shorcode page="" window="" label=""]

Upvotes: 1

Rottingham
Rottingham

Reputation: 2604

Here is an easy short code conditional output example. I would recommend being sparing with this type of syntax because it can get lost in the fray.

echo ($window === 'new') ? $window : '';

This format is conditional ? what to do if true : what to do if false

Upvotes: 0

Related Questions