PHP之外的PHP文本

时间:2015-05-15 00:57:09

标签: php wordpress

我正在构建一个wordpress主题,出于某种原因,当我查看源代码时,我的页面列表显示在li之外

<ul class="tabs">
    <?php 

        while ( $queryObject->have_posts()) : $queryObject->the_post(); 
            echo sprintf('<li>%s</li>', the_title());
        endwhile; 

    ?>
</ul>

当我在chrome中检查检查器中的源

<ul class="tabs">
    Deans’ Corner<li></li>
    Education Plan<li></li>
    Counselors’ Corner<li></li>
</ul>

我错过了一些简单的事情

3 个答案:

答案 0 :(得分:6)

查看the_title()的文档:

https://codex.wordpress.org/Function_Reference/the_title

  

显示或返回当前帖子的标题。

     

[...]

     

$回波

     

(布尔值)(可选)显示标题(TRUE)或返回它以便在PHP中使用(FALSE)。

     

默认值:TRUE

the_title()使用可选参数$echo来确定它是应该返回还是回显页面标题。由于您没有填写它并且它默认为echo选项,因此您的代码不起作用。

答案 1 :(得分:1)

如果the_title()回显,您将无法直接使用sprintf()。

因此,您可以只使用HTML ...

<li><?= the_title(); ?></li>

下面的例子是不必要的,因为在Timosta的回答中指出了the_title()的可选参数。

如果你需要使用the_title()作为函数参数,你可以使用输出缓冲:

ob_start()
the_title();
$title = ob_get_clean();
echo sprintf('<li>%s</li>', $title);

答案 2 :(得分:-1)

问题是默认情况下the_title()回显标题而不是返回标题。您实际上可以指定标题前后回显的内容,因此无需使用sprintf()。 这应该有效:

<ul class="tabs">
    <?php 

        while ( $queryObject->have_posts()) : $queryObject->the_post(); 
            the_title('<li>', '</li>');
        endwhile; 

    ?>
</ul>

检查WordPress Codex: http://codex.wordpress.org/Function_Reference/the_title