我有一页,我想把一些音乐会添加到日程页面。为了做到这一点,我决定使用短代码,这样用户就可以提供必要的数据,我将解析它,并在短代码函数中生成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>而且效果很好。

由于我的短代码函数返回相同的结果,所以我希望得到相同的结果,但没有得到结果。第一个短代码结果被破坏。
这就是我添加到functions.php中的内容:
function shortcode_issue_func( $atts ) {
$shortcode_atts = shortcode_atts( array(
'date' => '',
'time' => '',
'title' => '',
'location' => '',
'link' => '#'
), $atts );
$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>";
return $html;
}
add_shortcode( 'shortcode-issue', 'shortcode_issue_func' );在wordpress页面编辑中,我添加了以下内容:
[shortcode-issue date="19.07.2018" time="20:00" title="RHCP" location="NY" link="http://localhost/wordpress/rep-3/" ]
[shortcode-issue date="21.08.2018" time="21:00" title="The Rolling Stones" location="London" link="http://localhost/wordpress/rep-3/" ]
[shortcode-issue date="23.11.2018" time="22:00" title="Pink Floyd" location="NY" link="http://localhost/wordpress/rep-3/" ]这是输出

您可以注意到,第1节和第2节并不像第3节那样封装在"a“标记中。
在返回前转储$html,显示不存在任何问题:

我所做的解决方案是在"a“标记之前添加空的"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声明--> <!DOCTYPE html>
因此,应该允许"a“标签包装。
编辑2
我只是注意到结果html中有一个“幻影”"p“标记。

谢谢!
发布于 2018-04-22 10:14:55
尝试删除$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>";..to这个:
$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()地址一直是一个很好的实践。
https://stackoverflow.com/questions/49964547
复制相似问题