Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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

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
Php Wordpress短码函数_Php_Wordpress_Function_Custom Fields - Fatal编程技术网

Php Wordpress短码函数

Php Wordpress短码函数,php,wordpress,function,custom-fields,Php,Wordpress,Function,Custom Fields,有人能解释为什么只返回第一个结果吗。我想返回与当前url具有相同自定义字段值的所有结果。它只返回1。是否需要为每一个或某件事指定一个值?谢谢 <?php add_shortcode( 'feed', 'display_custom_post_type' ); function display_custom_post_type(){ $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

有人能解释为什么只返回第一个结果吗。我想返回与当前url具有相同自定义字段值的所有结果。它只返回1。是否需要为每一个或某件事指定一个值?谢谢

<?php add_shortcode( 'feed', 'display_custom_post_type' );

    function display_custom_post_type(){

    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

    $args = array( 
       'post_type'       => 'custom_post_type',
       'posts_per_page'  => -1  
    );

    $new_query = new WP_Query($args);

    while($new_query->have_posts()) : $new_query->the_post();

    return get_title();

    endwhile;

};?>

您可以在while循环中的第一个元素之后从函数中“返回”

返回所有帖子的示例:

$args = array(
    'post_type' => 'custom_post_type',
    'posts_per_page'  => -1  

);
$results = get_posts($args);
return $results;
您可以在while循环中的第一个元素之后从函数中“返回”

返回所有帖子的示例:

$args = array(
    'post_type' => 'custom_post_type',
    'posts_per_page'  => -1  

);
$results = get_posts($args);
return $results;

因为您已经在循环中添加了返回,所以它将只显示最后一个返回。必须将回路放置在回路外部,才能显示所有

仅举一个小例子,若要查看它的工作情况,请更改此位:

while($new_query->have_posts()) : $new_query->the_post();

return get_title();

endwhile;
为此:

while($new_query->have_posts()) : $new_query->the_post();

$all_titles .= get_title();

endwhile;

return $all_titles;

它很可能会在一行中显示所有标题,所以只需按照您的意愿设置格式即可

因为您在循环中添加了返回,所以它将只显示最后一个返回。必须将回路放置在回路外部,才能显示所有

仅举一个小例子,若要查看它的工作情况,请更改此位:

while($new_query->have_posts()) : $new_query->the_post();

return get_title();

endwhile;
为此:

while($new_query->have_posts()) : $new_query->the_post();

$all_titles .= get_title();

endwhile;

return $all_titles;

它很可能会在一行中显示所有标题,所以只需按照您的意愿设置格式即可

为了简单起见,我从示例中删除了元查询。无论是否包含元查询,都会出现相同的问题。谢谢为了简单起见,我从示例中删除了元查询。无论是否包含元查询,都会出现相同的问题。谢谢对不起,这是我问题上的一个输入错误。这并不能解决单次返回而不是返回多个输出的问题。很抱歉,这是我问题中的一个输入错误。这并不能解决单次返回而不是返回多个输出的问题。