0

I'm trying to push a shortcode parameter value to a template file but for some reason I can't get the array value that I'm targeting into a single variable. Here is my code and the shortcode I'm using:

function content_table($atts) {
    $atts = shortcode_atts(array(
        'tablepageid' => ''
    ), $atts);

    include(locate_template('table-layout.php'));
}
add_shortcode('ContentTable', 'content_table');

Edit I removed the quotes from my shortcode and I'm now able to see the value: [ContentTable tablepageid=4799]

I created a page to test the shortcode function:

print_r(array_values($atts));
Array ( [0] => 4799 )

The value from the shortcode is available but I'm having some trouble getting the value into a variable for my template to use:

$arr = $atts[0];

The $arrTable variable is empty...I can't figure out why it's not 4799.

2 Answers 2

1

You are probably confused by the output of array_values() which will always get you only array values without keys while $atts variable is actually an associative array.

To extract your attribute use $arr = $atts['tablepageid'];

Cheers

3
  • I'm not going to down vote this, because it is part of the process obviously. However, you should make it more complete in my opinion. Rewrite and explain the line of code you are talking about. Commented Apr 26, 2016 at 7:13
  • @NathanPowell please do what you feel is the right thing to do. I'm simply addressing that exact part of the question that asker has explicitly pointed as problematic for him.
    – Z. Zlatev
    Commented Apr 26, 2016 at 7:20
  • You incompletely address his debugging process. Commented Apr 26, 2016 at 7:23
-1

Shortcodes are intended to return values, or echo them. You can use the include to the file you have, but unless you serve the intended $atts to the function there you won't see anything.

Basically no one can help you unless we see the rest of your code. My best guess is that you need to declare the variable globally from the attributes in the shortcode and then claim it the template.

Doing the above is just shy of the sillyness that is creating a function that accepts the attribute as a parameter to pass to the file that redeclares that functions' return value as a local variable.

The list goes on in PHP. If you show your include file it would help.

1
  • Not to mention the intent of what is happening here. When I write code sometimes I think everyone is on the same page. I have no clue what the idea is here. More code/context this is a simple problem. Commented Apr 26, 2016 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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