• Resolved squasher

    (@squasher)


    Hi,

    I’m creating a shortcode to apply styles to a site. The attributes determine (amongst other) the background colour so the user can influence this. However the output only is “Array”. I think I’m doing something relatively simple wrong.

    I’ve got the following code:

    function column_left_func( $atts, $content = null ) {
        $colour = shortcode_atts( array(
            'white' => '<div class="col left white">',
            'yellow' => '<div class="col left yellow">',
        ), $atts );
    
        return $colour . $content . '</div>';
    }
    add_shortcode( 'column_left', 'column_left_func' );

    The shortcode I use is: [column_left colour=”white”]

    On the website I only see “Array”. Not the content or divs from the code.

    Somebody has a hint? I must be close?

    Thanks!

    • This topic was modified 8 years, 11 months ago by squasher.
Viewing 6 replies - 1 through 6 (of 6 total)
  • I think you’re using shortcode_atts incorrectly. shortcode_atts is for providing defaults for attributes that may or may not be set. Refer to the documentation to see why: https://codex.ww.wp.xz.cn/Function_Reference/shortcode_atts

    It looks like you want to out put a different div based on an attribute on the shortcode. A way to do that would be:

    function column_left_func( $atts, $content = null ) {
    	switch ( $atts['colour'] ) {
    		case 'yellow':
    			$colour = '<div class="col left yellow">';
    			break;
    		case 'white':
    		default:
    			$colour = '<div class="col left white">';
    			break;
    	}
    
    	return $colour . $content . '</div>';
    }
    add_shortcode( 'column_left', 'column_left_func' );
    

    Then you’d use either [column_left colour="yellow"] or [column_left colour="white"]. Because of the default: case, [column_left] would output the white version.

    • This reply was modified 8 years, 11 months ago by Jacob Peattie.
    Thread Starter squasher

    (@squasher)

    Hi Jacob,

    Thanks, you’re absolutely right! That was wat I’m trying to do and your solution works like a charm.

    By the way, one (WordPress) thing is annoying: the shortcode is placed on a new line and in the frontend it outputs an empty <p>-tag. I can not get rid of it. Any ideas how to loose this <p>-tag?

    Thanks again!

    Thread Starter squasher

    (@squasher)

    I found this solution for the p-tags: remove_filter( ‘the_content’, ‘wpautop’ );

    It works, but is this wise to do? Not sure…

    No it’s probably not wise, you’ll mess with linebreaks throughout your site.

    The thing to do would be to just use the shortcode inline, like so:

    [column_left colour="yellow"]Content goes here.
    
    New paragraph here.[/column_left]
    

    That should do it.

    Thread Starter squasher

    (@squasher)

    I was afraid of that. However, I tried your suggestion as well, but everytime the page reloads the shortcode gets placed on the next line…

    This is my code:
    <div class="col-container">[column_left colour="yellow"]<h1 class="blockquote">...

    And after a look at the preview the code is changed to:

    <div class="col-container">
    
    [kolom_links kleur="wit"]
    <h1 class="blockquote">

    And back is the <p></p> above the H1!

    Frustrating!

    • This reply was modified 8 years, 11 months ago by squasher.
    Thread Starter squasher

    (@squasher)

    Well, solved it by adding some Javascript to remove empty p-tags…

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Shortcode only produces text “Array”’ is closed to new replies.