我制作了自己的自定义帖子类型和输出自定义帖子类型的快捷码。到目前为止一切都很好,但一旦我尝试添加更多帖子,我仍然只能在页面上看到一篇,而不是更多。posts_per_page
设置为3或更多,但仍然为零。
// Custom Post Type for Use Cases page slider
function create_post_type() {
register_post_type( \'use_cases\',
array(
\'labels\' => array(
\'name\' => __( \'Use Cases\' ),
\'singular_name\' => __( \'Use_Case\' )
),
\'public\' => true,
\'has_archive\' => true,
)
);
}
add_action( \'init\', \'create_post_type\' );
// Shortcode for Use Cases caption
add_shortcode(\'Use_Case\', \'use_case_shortcode_query\');
function use_case_shortcode_query($atts, $content){
echo "working";
extract(shortcode_atts(array( // a few default values
\'posts_per_page\' => \'3\',
\'post_type\' => \'use_cases\',
)
, $atts));
global $post;
$posts = new WP_Query(array( // a few default values
\'posts_per_page\' => \'3\',
\'post_type\' => \'use_cases\'
));
//print_r($posts);
$output = \'\';
if ($posts->have_posts()):
while ($posts->have_posts()):
//echo var_dump($posts->the_post());
$posts->the_post();
$out = \'<div class="use_case_boxes">
<h4>Name: <a href="\'.get_permalink().\'" title="\' . get_the_title() . \'">\'.get_the_title() .\'</a></h4>
<p class="Film_desc">\'.get_the_content().\'</p>\';
// add here more...
$out .=\'</div>\';
/* these arguments will be available from inside $content
get_permalink()
get_the_content()
get_the_category_list(\', \')
get_the_title()
and custom fields
get_post_meta($post->ID, \'field_name\', true);
*/
endwhile;
else:
return; // no posts found
endif;
wp_reset_query();
return html_entity_decode($out);
}
最合适的回答,由SO网友:Visakh B Sujathan 整理而成
您正在初始化out变量,这就是为什么它只显示一个产品
// Custom Post Type for Use Cases page slider
function create_post_type() {
register_post_type( \'use_cases\',
array(
\'labels\' => array(
\'name\' => __( \'Use Cases\' ),
\'singular_name\' => __( \'Use_Case\' )
),
\'public\' => true,
\'has_archive\' => true,
)
);
}
add_action( \'init\', \'create_post_type\' );
// Shortcode for Use Cases caption
add_shortcode(\'Use_Case\', \'use_case_shortcode_query\');
function use_case_shortcode_query($atts, $content){
echo "working";
extract(shortcode_atts(array( // a few default values
\'posts_per_page\' => \'3\',
\'post_type\' => \'use_cases\',
)
, $atts));
global $post;
$posts = new WP_Query(array( // a few default values
\'posts_per_page\' => \'3\',
\'post_type\' => \'use_cases\'
));
//print_r($posts);
$output = \'\';
$out = \'\';
if ($posts->have_posts()):
while ($posts->have_posts()):
//echo var_dump($posts->the_post());
$posts->the_post();
$out .= \'<div class="use_case_boxes">
<h4>Name: <a href="\'.get_permalink().\'" title="\' . get_the_title() . \'">\'.get_the_title() .\'</a></h4>
<p class="Film_desc">\'.get_the_content().\'</p>\';
// add here more...
$out .=\'</div>\';
/* these arguments will be available from inside $content
get_permalink()
get_the_content()
get_the_category_list(\', \')
get_the_title()
and custom fields
get_post_meta($post->ID, \'field_name\', true);
*/
endwhile;
else:
return; // no posts found
endif;
wp_reset_query();
return html_entity_decode($out);
}