Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 在functions.php中使用WP_查询只返回一个结果_Wordpress - Fatal编程技术网

Wordpress 在functions.php中使用WP_查询只返回一个结果

Wordpress 在functions.php中使用WP_查询只返回一个结果,wordpress,Wordpress,我在functions.php文件中编写了一个函数,该函数运行一个新的WP_查询类,以根据自定义页面模板的元键/值获取一些子页面。这是一种工作,但它只返回一个结果-我知道还有更多,因为在我将查询转换为函数之前,我已经在特定页面上正确运行了查询。它返回了所有正确的结果,但由于我可能在几页中需要此功能,我决定将其转换为函数 这是我的功能代码 function contact_profiles($args) { global $post; $output = ""; $t

我在functions.php文件中编写了一个函数,该函数运行一个新的WP_查询类,以根据自定义页面模板的元键/值获取一些子页面。这是一种工作,但它只返回一个结果-我知道还有更多,因为在我将查询转换为函数之前,我已经在特定页面上正确运行了查询。它返回了所有正确的结果,但由于我可能在几页中需要此功能,我决定将其转换为函数

这是我的功能代码

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;
}

我是不是在做一些不该做的事情?
global$post
位是否导致了问题,因为我不确定是否应该从函数文件调用它。

最可能的情况是,在后端阅读部分,您的每页文章设置为
1
。如果没有向自定义查询传递自定义值,则默认选项
get\u选项('posts\u per\u page')
将用作
posts\u per\u page
参数的值

您的解决方案是明确地将每页的
帖子设置为所需的数量,或设置为
-1
,以获得所有帖子

编辑 我以前错过过这个,你的连接是错误的

$output = '<div class="staff-member">'
$output=''
应该是

$output .= '<div class="staff-member">'
$output.=''
这是您的代码的更新版本

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;
}
功能联系人配置文件($args)
{
$output=“”;
$thew_query=newwp_query($args);
while($the_query->have_posts()):$the_query->the_post();
$output.=''
.''
.''
“.获取帖子元($post->ID,'job\u role',true)。”
.'';
结束时;
wp_reset_postdata();
返回$output;
}

在后端阅读部分,您很可能将每页的帖子设置为
1
。如果没有向自定义查询传递自定义值,则默认选项
get\u选项('posts\u per\u page')
将用作
posts\u per\u page
参数的值

您的解决方案是明确地将每页的
帖子设置为所需的数量,或设置为
-1
,以获得所有帖子

编辑 我以前错过过这个,你的连接是错误的

$output = '<div class="staff-member">'
$output=''
应该是

$output .= '<div class="staff-member">'
$output.=''
这是您的代码的更新版本

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;
}
功能联系人配置文件($args)
{
$output=“”;
$thew_query=newwp_query($args);
while($the_query->have_posts()):$the_query->the_post();
$output.=''
.''
.''
“.获取帖子元($post->ID,'job\u role',true)。”
.'';
结束时;
wp_reset_postdata();
返回$output;
}

Hi Pieter,我想可能是这样的,并且已经尝试过了-在$args数组中添加'posts\u per\u page'=>-1没有任何区别:-(检查我的更新,我错过了一些明显的东西:-)另外注意,
global$post在您的上下文中是不必要的谢谢Peiter,您更新的代码已经工作了:-)从我所看到的
global$post
行对于get\u post\u元行是必要的-如果我取出它,元值不会显示,但是如果我将其保留在其中,它工作正常。我还通过在while循环中使用$output变量作为数组并在返回函数中内爆来实现它。两者似乎都达到了预期的效果,但你的更整洁。谢谢你的帮助,非常感谢:-)我的荣幸。奇怪的是,删除
$post
会使post meta失败,
$post
通过
the_post()
设置在循环中,因此不需要设置它。但不管怎样,很高兴你能成功。享受:-)嗨,皮特,我想可能是这样的,我已经尝试过了-在$args数组中添加'posts\u per\u page'=>-1没有任何区别:-(检查我的更新,我错过了一些明显的东西:-)还请注意,
global$post在您的上下文中是不必要的谢谢Peiter,您更新的代码已经工作了:-)从我所看到的
global$post
行对于get\u post\u元行是必要的-如果我取出它,元值不会显示,但是如果我将其保留在其中,它工作正常。我还通过在while循环中使用$output变量作为数组并在返回函数中内爆来实现它。两者似乎都达到了预期的效果,但你的更整洁。谢谢你的帮助,非常感谢:-)我的荣幸。奇怪的是,删除
$post
会使post meta失败,
$post
通过
the_post()
设置在循环中,因此不需要设置它。但不管怎样,很高兴你能成功。享受:-)