I want to show the category name of the post when I write [category] shortcode in the post body. I'm using this code but only showing array error.
Sorry, I'm not an expert in coding. Perhaps you can redefine my code? Please.
function cat_post() {
return get_the_category();
}
add_shortcode('categorypost', 'cat_post');
I want to show the category name of the post when I write [category] shortcode in the post body. I'm using this code but only showing array error.
Sorry, I'm not an expert in coding. Perhaps you can redefine my code? Please.
function cat_post() {
return get_the_category();
}
add_shortcode('categorypost', 'cat_post');
Share
Improve this question
edited May 28, 2019 at 21:50
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked May 28, 2019 at 13:50
ChrisChris
131 silver badge2 bronze badges
2
|
2 Answers
Reset to default 0Put the code below in your functions.php file:
function register_theme_shortcodes() {
add_shortcode( 'category',
/**
* category shortcode function.
*
* @param array $atts
*
* @return string
*/
function ( $atts ) {
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
/**
* @var int $id Optional Post ID to get categories from.
*/
extract( $atts );
if ( $id ) {
$post_id = $id;
} else {
global $post;
if ( $post instanceof WP_Post ) {
$post_id = $post->ID;
}
}
if ( empty( $post_id ) ) {
$output = '';
} else {
$output = [];
if ( $post_categories = get_the_category() ) {
/**
* @var WP_Term $category
*/
foreach ( $post_categories as $category ) {
// Builds the category name with its link.
$output[] = "<a href='" . get_term_link( $category->term_id ) . "'>{$category->name}</a>";
// Build just the category name.
// $output[] = $category->name;
}
}
$output = implode( ', ', $output );
}
return $output;
}
);
}
add_action( 'init', 'register_theme_shortcodes' );
Make sure you pay attention to the comments along the code as you can have the category names output with their links or not.
And if you want to print the category of any other post, outside the post, the code above also supports the following format: [category id=1234]
.
The main problem with your code is that you register [categorypost]
shortcode, but you want to register it as [category]
.
Another problem is that your shortcode should return HTML code - the output of shortcode. And as you can see here, the get_the_category
function returns an array of terms.
So how to write it correctly?
The easiest way will be to use the_category()
template tag. So here's the code:
function category_shortcode_callback( $atts ) {
// set default params for the_category
$a = shortcode_atts( array(
'separator' => '',
'parents' => '',
'post_id' => false
), $atts );
ob_start(); // start output buffering
the_category( $a['separator'], $a['parents'], $a['post_id'] ); // print categories
return ob_get_clean(); // grab the output and return it
}
add_shortcode( 'category', 'category_shortcode_callback' );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453543a4628381.html
[categorypost]
rather than[category]
. Also,get_the_category()
will need to know what post you're asking about, so you need to specifyglobal $post
above and callget_the_category($post->ID)
. Finally,get_the_category()
will return an array, so you shouldn't return it directly, you'll need to parse it and determine how you want to display the results. – WebElaine Commented May 28, 2019 at 13:54get_the_category()
doesn't need post_ID - by default it will use global/current post. So it makes sense in this case - it is shortcode, which is used inside of post, so the global $post is set. – Krzysiek Dróżdż Commented May 28, 2019 at 21:49