我正在使用此查询来获取活动自定义帖子
<?php
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<p><?php echo get_post_meta($post->ID, 'events_photo', true); ?></p>
<?php endforeach; ?>
现在我想为这个查询创建一个短代码。我正在使用此代码
function get_events(){
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$list_events= '';
$event_title= get_the_title();
$event_photo= get_post_meta($post->ID, 'event_photo', true);
$list_events.='
<h2>'.$event_title.'</h2>
<p>'.$event_photo.'</p>
';
$list_events= '';
endforeach;
}
function insert_event($atts, $content=null){
$events_all= get_events();
return $events_all;
}
add_shortcode('all_events', 'insert_event');
但是,它不起作用。有人可以帮忙吗?