php - 如何通过wordpress中的简码在单页上显示所有推荐?
问题描述
我创建了简码以在一页上显示所有推荐。我创建了自定义帖子类型来添加每个推荐。谁能告诉我如何在单页上显示所有推荐。此代码用于在我提到代码下面创建 sshortcode:-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
解决方案
我认为您必须在$args数组中传递'posts_per_page'键。所以你的$args数组看起来像这样
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
我不确定您正在使用的$count变量。我想您正在尝试在那里实现分页逻辑,但由于您的问题是关于在单页上显示所有推荐,我不会进入那部分。
在$args中进行更改将返回所有推荐对象。