PHP中的转义不起作用


Escaping in PHP does not work

我在Wordpress:中有这个片段

<?php if ( is_user_logged_in() ) {
      echo " <span class=blabla>
<a href="<?php echo home_url() . '/author/' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ); ?>" >My personal page</a>
        </span> "; } ?>

因为它有两个echo,所以我使用了这个:

<?php if ( is_user_logged_in() ) {
      echo " <span class=blabla>
<a href='"home_url() . '/author/' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ); '" >My personal page</a>
        </span> "; } ?>

由于某种原因,它不起作用。我做错了什么?

为了使其可读,将PHP与HTML分离,并拆分长行

<?php if ( is_user_logged_in() ) { 
    $url  = home_url() . '/author/'; 
    $url .= get_the_author_meta( 'user_login', wp_get_current_user()->ID );
?>
<span class=blabla>
    <a href="<?php echo $url ?>">My personal page</a>
</span>
<? } ?>

试试这个:

<?php if ( is_user_logged_in() ) : ?>
    <span class="blabla">
        <a href="<?php echo home_url( '/author/' . get_the_author_meta( 'user_login', get_current_user_id() ) ) ?>">My personal page</a>
    </span>
<?php endif; ?>

您有几个语法错误。

<?php
if ( is_user_logged_in() ) {
    echo "<span class=blabla>"
       . "<a href='"" . home_url() . "/author/"
       . get_the_author_meta('user_login', wp_get_current_user()->ID)
       . "'" >My personal page</a></span> "; 
}

要仔细检查文件是否有错误,请在命令行中运行以下命令:

php -l [filename]

它应该返回

No syntax errors detected in [filename]

您需要关闭home_url()之前的字符串,并在->ID ). 中的concat运算符.之后加引号

<?php if ( is_user_logged_in() ) {
  echo " <span class=blabla>
<a href='"".home_url() . '/author/' . get_the_author_meta( 'user_login', 
    wp_get_current_user()->ID )." '" >My personal page</a>
    </span> "; } ?>

尝试:

<?php if ( is_user_logged_in() ) {
      echo ' <span class="blabla"><a href="'.home_url() . '/author/' . get_the_author_meta( "user_login", wp_get_current_user()->ID ).'>My personal page</a></span> '; } 
?>