我正在尝试在可视化作曲中创建一个自定义元素,用于在单个页面中显示5个帖子,并在接下来的5个页面中使用分页等等 . 这是自定义元素创建的代码 .
class vc_show_post_list extends WPBakeryShortCode
{
function __construct() {
add_action('init', array($this, 'show_post_list_mapping'));
add_shortcode('vc_show_post_list', array($this, 'vc_show_post_list_callback'));
}
public function show_post_list_mapping() {
if (!defined('WPB_VC_VERSION')) {
return;
}
vc_map(
array(
'name' => __('Show posts list', 'text-domain'),
'base' => 'vc_show_post_list',
'description' => __('VC Element For showing posts', 'text-domain'),
'category' => __('Custom VC Elements for Showing post list', 'text-domain'),
'params' => array(
array(
'type' => 'textfield',
"holder" => "div",
"class" => "",
"heading" => __("Post count", "my-text-domain"),
'param_name' => 'show_post_list_count',
'description' => __('Enter No of post to be shown in a page', 'js_composer'),
)
)
)
);
}
在前端已经使用此代码来显示帖子的数量
public function vc_show_post_list_callback($atts, $content = null)
{
global $post;
$atts = extract(
shortcode_atts(
array(
'show_post_list_count' => '',
), $atts
)
);
$html = '';
$post_list = '';
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'numberposts' => $show_post_list_count,
'paged' => $paged
);
$myposts = get_posts($args);
$post_date=get_the_date('j.F Y');
foreach ($myposts as $post) : setup_postdata($post);
$post_list .= '<div class="col-md-12 border-mark">
<div class="col-md-4">
<img src="'.get_the_post_thumbnail_url($post->ID).'"/>
</div>
<div class="col-md-8">
<h3>'.$post->post_title.'</h3>
<span>'.$post_date.'</span>
<p>'.$post->post_excerpt.'</p>
<a href="'.get_post_permalink($post->ID).'">Read More</a>
</div>
</div>';
$paginate_links ='<div class="pagination">
'.get_the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Newer', 'textdomain' ),
'next_text' => __( 'Older', 'textdomain' ),
) ).'
</div>';
endforeach;
wp_reset_postdata();
$html = '<div class="post-list">'.$post_list.'</div>
<div class="paginations">'.$paginate_links.'</div>';
return $html;
}
}
new vc_show_post_list();
但只有分页没有显示帖子 . 任何想法我做错了什么?先感谢您