我在我的functions.php文件中编写了一个函数,它运行一个新的WP_Query类,根据它们的元键/值在我的自定义页面模板上获取一些子页面。这是一种工作,但它只返回一个结果-我知道还有更多,因为我让查询在特定的页面上正确运行,然后我把它变成一个函数。它返回了所有正确的结果,但是由于我可能需要在几个页面中使用这个功能,所以我决定将它转换为一个函数。
这是我的函数代码…
function contact_profiles($args) {
global $post;
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output = '<div class="staff-member">'
.'<a href="' . get_the_permalink() . '" title="Get in touch with ' . get_the_title() . '">' . get_the_post_thumbnail() . '</a>'
.'<h2 class="name"><a href="' . get_the_permalink() . '" title="Get in touch with ' . get_the_title() . '">' . get_the_title() . '</a></h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}下面是我如何在自定义页面模板…中调用它
$myarray = array('meta_key' => 'job_area', 'meta_value' => 'Online', 'post_type' => 'page',);
echo contact_profiles($myarray);我做了什么不该做的明显的事吗?是global $post位导致了问题,因为我不确定是否应该从函数文件中调用它。
发布于 2015-05-19 12:48:55
最可能的情况是,在后端阅读部分中,每个页面的文章设置为1。如果没有向自定义查询传递自定义值,则默认选项get_option( 'posts_per_page' )将用作posts_per_page参数的值。
您的解决方案是将posts_per_page设置为期望的数量,或将-1设置为获取所有帖子
编辑
我以前没注意到,你的连接是错的。
$output = '<div class="staff-member">'应该是
$output .= '<div class="staff-member">'下面是您的代码的更新版本
function contact_profiles($args)
{
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output .= '<div class="staff-member">'
.'<a href="' . get_the_permalink() . '" title="Get in touch with ' . get_the_title() . '">' . get_the_post_thumbnail() . '</a>'
.'<h2 class="name"><a href="' . get_the_permalink() . '" title="Get in touch with ' . get_the_title() . '">' . get_the_title() . '</a></h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}https://stackoverflow.com/questions/30325170
复制相似问题