如何在使用 echo 时将字符串与函数调用连接起来?

发布于 2024-08-21 00:40:56 字数 306 浏览 8 评论 0原文

我想在我的 echo'ed html 字符串中使用两个函数调用返回的值。

  • the_title()
  • 以下工作正常:

    echo '<li><a href="';
    echo the_permalink();
    echo '">';
    echo the_title();
    echo '</a></li>';
    

    ...但我如何获得它们所有这些都在一个声明中?

    I want to use the values returned from two function calls in my echo'ed html string.

    <li><a href="the_permalink()">the_title()</a></li>

    The following works fine:

    echo '<li><a href="';
    echo the_permalink();
    echo '">';
    echo the_title();
    echo '</a></li>';
    

    ... but how do I get them all in one single statement?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(6

    匿名的好友 2024-08-28 00:40:56

    您遇到问题的原因是 the_permalink()the_title() 不返回,它们会回显。而是使用 get_permalink()$post->post_title。请记住 get_permalink() 需要帖子 ID ($post->ID) 作为参数。我知道这令人恼火且违反直觉,但这就是 WordPress 的工作方式(请参阅此答案评论中的主观性。)

    这解释了为什么第二个示例在您最初的问题中有效。如果您调用从字符串内打印的函数,则回显将在字符串末尾之前输出。

    所以这:

    echo ' this should be before the link: '.the_permalink().' But it is not.';
    

    不会按预期工作。相反,它会输出:

    http://example.com this should be before the link: But it is not.
    

    In PHP you can use single and double quote.当我使用 HTML 构建字符串时,我通常以单引号开始字符串,这样,我可以在字符串中使用 HTML 兼容的双引号而无需转义。

    因此,为了将其四舍五入,它看起来像:

    echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
    

    或者按照您最初的要求,为了简单地转义它们,请在引号前添加反斜杠。像这样(单引号已被删除)

    echo "<li><a href=\"".get_permalink($post->ID)."\">".$post->post_title."</a></li>";
    

    这当然假设您在循环内调用它,否则需要比这多一点才能获得所需的输出。

    The reason you are having problems is because the_permalink() and the_title() do not return, they echo. Instead use get_permalink() and $post->post_title. Remember get_permalink() requires the post id ($post->ID) as a parameter. I know this is irritating and counter-intuitive, but it's how Wordpress works (see subjectivity in comments to this answer.)

    This explains why the second example works in your initial question. If you call function that prints from within a string, the echo will output before the end of the string.

    So this:

    echo ' this should be before the link: '.the_permalink().' But it is not.';
    

    will not work as expected. Instead, it will output this:

    http://example.com this should be before the link: But it is not.
    

    In PHP you can use both single and double quotes. When I am building strings with HTML I generally begin the string with a single quote, that way, I can use HTML-compatible double quotes within the string without escaping.

    So to round it up, it would look something like:

    echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
    

    Or as you originally requested, to simply escape them, put a backslash before the quote. Like so (the single quotes have been removed)

    echo "<li><a href=\"".get_permalink($post->ID)."\">".$post->post_title."</a></li>";
    

    This is of course assuming you are calling this from within the loop, otherwise a bit more than this would be required to get the desired output.

    墨落画卷 2024-08-28 00:40:56
    echo '<li><a href="', the_permalink(), '">', the_title(), '</a></li>';
    
    echo '<li><a href="', the_permalink(), '">', the_title(), '</a></li>';
    
    只涨不跌 2024-08-28 00:40:56
    printf( '<li><a href="%s">%s</a></li>', the_permalink(), the_title() );
    
    printf( '<li><a href="%s">%s</a></li>', the_permalink(), the_title() );
    
    三五鸿雁 2024-08-28 00:40:56

    使用串联(不需要换行符):

    echo '<li><a href="'
     . the_permalink()
     . '">'
     . the_title()
     . '</a></li>';
    

    Using concatenation (line breaks not required):

    echo '<li><a href="'
     . the_permalink()
     . '">'
     . the_title()
     . '</a></li>';
    
    煮茶煮酒煮时光 2024-08-28 00:40:56
    echo "<li><a href=".the_permalink().">".the_title()."</a></li>";
    
    echo "<li><a href=".the_permalink().">".the_title()."</a></li>";
    
    一场春暖 2024-08-28 00:40:56

    使用 。它显示或返回当前帖子的标题。它在某种程度上重复了 the_title() 的功能,但通过剥离 HTML 标签并将某些字符(包括引号)转换为其等效的字符实体,提供了标题的“干净”版本。

    Use <?php the_title_attribute() ?>. It displays or returns the title of the current post. It somewhat duplicates the functionality of the_title(), but provides a 'clean' version of the title by stripping HTML tags and converting certain characters (including quotes) to their character entity equivalent.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文