PHP函数:HTML没有显示


php function: the html did not display

我创建了一个名为portfoliofunction,但不幸的是,codehtml没有显示,而wordpress的功能正在工作。

function portfolio($id){
        $output = "<ul class='"recentWorks port'">";
            if (have_posts()) : {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            query_posts("&cat={echo $id;}&showposts=6&paged=$paged&order=ASC");
            }
            while(have_posts()) : the_post();
            $output .= "<li>";
                $output.= "<span>";
                the_title();
                $output .= "</span>";
          $output .= "</li>";
          endwhile;
         endif;
          $output .= "</ul>";
          return $output;
      }

只有the_title()显示,而htmlwrapthe_title()都不见了。

不要将HTML存储在$output变量中,并在函数末尾返回该变量,而是尝试在需要时打印出HTML。例如

print "<span>";
the_title();
print "</span>";

原因是因为the_title();是立即打印的,但你的HTML不是,它只是进入一个变量,并由函数返回。然后调用函数将负责打印变量,此时它将在the_title();

之后打印。

试试这个:-

while(have_posts()) : the_post();
            $output .= "<li>";
                $output.= "<span>";
                $output .= the_title(); // edited this line
                $output .= "</span>";
          $output .= "</li>";
          endwhile;

试试这个:

function portfolio($id) {
    $output = "<ul class='"recentWorks port'">";
    if (have_posts()) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts("&cat={echo $id;}&showposts=6&paged=$paged&order=ASC");
        while (have_posts()) {
            $the_post = the_post();
            $output .= "
            <li>
                <span>
                    $the_post
                </span>
            </li>";
        }
    }
    $output .= "</ul>";
    return $output;
} 

Codex - The Loop.

使用此代码

function portfolio($id){
            $output = '<ul class='"recentWorks port'">';
                if (have_posts()) : {
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts("&cat={$id}&showposts=6&paged=$paged&order=ASC");
                }
                while(have_posts()) : the_post();
                $output .= '<li>';
                    $output.= '<span>';
                    the_title();
                    $output .= '</span>';
              $output .= '</li>';
              endwhile;
             endif;
              $output .= '</ul>';
              return $output;
          }