shortcode – Show in an entry a Custom Post Type associated to a post
This is the information of my problem:
Technologies used: Divi Theme – WordPress
Scope:
Show an Advanced Custom Field of type post_object related to a specific post.
Details:
- I have a custom post type called countries which is a list of countries
- I have an Advanced Custom Field called country of type post object that refers to that list of countries from the CPT
- I have a Custom Post Type called News where I assign a country to each news using the Advanced Custom Field Country
What I wish:
I want to show in the entry of each news post the country associated with that entry. When I try to use Divi’s dynamic content I get the error: “Object of class WP_Post could not be converted to string”.
My code:
I have made this shortcode but it shows me a list of countries and doesn’t show the country assigned to the custom post type in the post entry. In the image, you can see what I want along with what I have at the moment.
This is my code:
add_shortcode( 'showpost', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'countries',
'$post->ID'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
$string .= '<ul>';
while( $query->have_posts() ){
$query->the_post();
$string .= '<li>' . get_the_title() . '</li>';
}
$string .= '</ul>';
}
wp_reset_postdata();
return $string;
}
Any ideas?
Leave an answer