Php Wordpress短代码-第一个短代码已断开

Php Wordpress短代码-第一个短代码已断开,php,wordpress,hyperlink,shortcode,Php,Wordpress,Hyperlink,Shortcode,我有一个页面,我想在日程表页面中添加一些音乐会。 为了做到这一点,我决定使用短代码,这样用户就可以提供必要的数据,我将解析它并在短代码函数中生成html 首先我想告诉你为什么我认为这与wordpress有关。我已将此代码直接添加到页面php文件中: <h4>This year hardcoded</h4> <a href='#' class='p-link'> <h2>01.02.2018 / RHCP</h2> <

我有一个页面,我想在日程表页面中添加一些音乐会。 为了做到这一点,我决定使用短代码,这样用户就可以提供必要的数据,我将解析它并在短代码函数中生成html

首先我想告诉你为什么我认为这与wordpress有关。我已将此代码直接添加到页面php文件中:

<h4>This year hardcoded</h4>
<a href='#' class='p-link'>
    <h2>01.02.2018 / RHCP</h2>
    <p>20:00 / NY</p>
</a>
<a href='#' class='p-link'>
    <h2>03.04.2018 / The Rolling Stones</h2>
    <p>21:00 / LONDON</p>
</a>
<a href='#' class='p-link'>
    <h2>11.12.2018 / Pink Floyd</h2>
    <p>22:00 / TOKIO</p>
</a>
这是输出

您可以注意到,第1节和第2节并不像第3节那样包装在a标记中

在返回之前转储$html,表明不存在任何问题:

我所做的黑客解决方案是在标记之前添加空的p标记

$html = "<p style='display: none;'></p>
        <a href='{$shortcode_atts['link']}' class='p-link'>
            <h2>{$shortcode_atts['date']} / {$shortcode_atts['title']}</h2>
            <p>{$shortcode_atts['time']} / {$shortcode_atts['location']}</p>
        </a>";
return $html;
但我不想在我的代码中有这样的黑客行为,我找不到发生这种情况的原因。 如果有人能帮助我,我将非常感激

编辑1

代码有HTML5声明->

因此,标签包装应该是允许的

编辑2

我刚刚注意到在生成的html中有一个幻影p标记

谢谢大家!

尝试删除$html变量值中的多余空格

因此,改变这一点:

$html = "<a href='{$shortcode_atts['link']}' class='p-link'>
            <h2>{$shortcode_atts['date']} / {$shortcode_atts['title']}</h2>
            <p>{$shortcode_atts['time']} / {$shortcode_atts['location']}</p>
        </a>";
…对这一个:

$html = "<a href='" . esc_url( $shortcode_atts['link'] ) . "' class='p-link'>" .
    "<h2>{$shortcode_atts['date']} / {$shortcode_atts['title']}</h2>" .
    "<p>{$shortcode_atts['time']} / {$shortcode_atts['location']}</p>" .
"</a>";

PS:esc_url地址始终是一个很好的做法。

使用而不是,你这样做的方式只有在HTML5中才允许,如果你的html文档是以正常的html打开开始的,那么它将不会以你尝试的方式工作,无论如何,在这两种情况下,使用而不是已经用span尝试过了,但它不起作用。代码中有HTML5声明->感谢@sally cj提供的解决方案和esc_url建议。
$html = "<a href='{$shortcode_atts['link']}' class='p-link'>
            <h2>{$shortcode_atts['date']} / {$shortcode_atts['title']}</h2>
            <p>{$shortcode_atts['time']} / {$shortcode_atts['location']}</p>
        </a>";
$html = "<a href='" . esc_url( $shortcode_atts['link'] ) . "' class='p-link'>" .
    "<h2>{$shortcode_atts['date']} / {$shortcode_atts['title']}</h2>" .
    "<p>{$shortcode_atts['time']} / {$shortcode_atts['location']}</p>" .
"</a>";