Php Wordpress短代码显示顺序错误的数据,即使使用return

Php Wordpress短代码显示顺序错误的数据,即使使用return,php,wordpress,custom-post-type,shortcode,Php,Wordpress,Custom Post Type,Shortcode,我试图用我的WP短代码显示一些数据,但它显示的是所有不正常的数据。在搜索了一点之后,文档说您需要在函数中使用return,但它仍然不起作用 这是密码 function dwwp_jobs_from_california($atts){ $atts = shortcode_atts(array( 'title' => 'All jobs in California:', ), $atts ); $query = new WP_Query( array(

我试图用我的WP短代码显示一些数据,但它显示的是所有不正常的数据。在搜索了一点之后,文档说您需要在函数中使用return,但它仍然不起作用

这是密码

function dwwp_jobs_from_california($atts){

  $atts = shortcode_atts(array(
    'title' => 'All jobs in California:',
    ),
    $atts
  );

  $query = new WP_Query( array('post_type' => 'job'));
  $jobs = "<h1>" . $atts['title'] . '</h1><br>';
  if( $query->have_posts()):
    while ( $query->have_posts() ) :
      $query->the_post();
      $jobs .= '<h3>' . the_title() . '</h3><br>';
    endwhile;
  endif;

  $jobs .= "End of the loop!";
  wp_reset_query();
  return $jobs;
  }

add_shortcode('jobs_california', 'dwwp_jobs_from_california');
它在页面上呈现的内容:

"Content of the page"

[jobs_california]
[jobs_california]
"Content of the page"
我还必须承认我不太习惯使用PHP,所以如果我的代码质量不好,我很抱歉


谁能告诉我我做错了什么?提前谢谢

我想你唯一的问题是你使用的是
标题()
,它将
回应
文章标题。。。但是您希望构建一个html字符串,它将由您的短代码函数返回。尝试使用:

the_title('', '', false)
或者更好:

get_the_title()

希望这有帮助

嗨!所以当你说“坏了”的时候,你是什么意思?它们的顺序是什么?您希望的顺序是什么?对不起,我的问题是短代码的“常见问题”,短代码显示在页面内容之前。我将编辑这个问题:)但是人们在使用echo而不是返回函数时经常会遇到这个问题,但这不是问题的关键,非常感谢。这正是发生的事情!修复了get_the_title()