Fabien
Fabien

Reputation: 59

Shortcode issue

I am currently creating a shortcode in order to display custom taxonomy terms as a list in my template :

// First we create a function
function list_terms_forme_juridique_taxonomy( $atts ) {

// Inside the function we extract custom taxonomy parameter of our 
shortcode

extract( shortcode_atts( array(
'custom_taxonomy' => 'forme_juridique',
), 
                    $atts ) );

// arguments for function wp_list_categories
$args = array( 
taxonomy => $custom_taxonomy,
title_li => ''
);

// We wrap it in unordered list 
echo '<ul>'; 
echo wp_list_categories($args);
echo '</ul>';
}

// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy' 
);

I run in the 2 following issues :

Any help appreciated!

Thanks

Upvotes: 2

Views: 626

Answers (1)

M Hayward
M Hayward

Reputation: 126

Firstly the output of your shortcode is displaying at the top of your page because you're echoing the output. You should create a $output variable and build it up with what you want to display, and then return it. For example:

$output = '';
$output .= '<ul>'; 
$output .= wp_list_categories($args);
$output .= '</ul>';
return $output;

Secondly you're getting the errors because you've not quoted the keys in your array declaration. Therefore PHP assumes they should be constants that were previously defined.

$args = array( 
    taxonomy => $custom_taxonomy,
    title_li => ''
);

Should be:

$args = array( 
    'taxonomy' => $custom_taxonomy,
    'title_li' => ''
);

Upvotes: 2

Related Questions