I've created a custom post type for locations on my site and I would like to display each post in full with shortcode. I'm quite new to creating shortcode and I'm struggling to wrap my head around it. I want to display the post by slug like this: [stefan_location slug="Aspley"] but I can't get it to work. I know I'm probably way off so any advise would be welcome!!
function cpt_content_func($atts){
$post = '';
$content = '';
extract( shortcode_atts( array(
'slug' => null,
), $atts ) );
$args = array(
'name' => $slug,
'post_type' => 'location',
'numberposts' => 1
);
$post = get_posts( $args );
if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}
add_shortcode('stefan_location','stefan_location');
I've created a custom post type for locations on my site and I would like to display each post in full with shortcode. I'm quite new to creating shortcode and I'm struggling to wrap my head around it. I want to display the post by slug like this: [stefan_location slug="Aspley"] but I can't get it to work. I know I'm probably way off so any advise would be welcome!!
function cpt_content_func($atts){
$post = '';
$content = '';
extract( shortcode_atts( array(
'slug' => null,
), $atts ) );
$args = array(
'name' => $slug,
'post_type' => 'location',
'numberposts' => 1
);
$post = get_posts( $args );
if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}
add_shortcode('stefan_location','stefan_location');
add_shortcode( string $tag, callable $callback );
Code :
function cpt_content_func($atts){
$post = '';
$content = '';
extract( shortcode_atts( array(
'slug' => null,
), $atts ) );
$args = array(
'post_type' => 'location',
'posts_per_page' => 1,
'post_name__in' => $atts['slug'] );
$post = get_posts( $args );
if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}
add_shortcode('stefan_location','cpt_content_func');
add_shortcode()
should becpt_content_func
. – Sally CJ Commented Mar 20, 2020 at 1:46