【问题标题】:Wordpress stripping <a href> from shortcodesWordpress 从短代码中剥离 <a href>
【发布时间】:2011-10-21 14:00:10
【问题描述】:

我有一个相当直截了当的短代码,用于制作一个名为:

[jasminesays quote="blah de blah"]

很容易。但是,当我尝试在其中放置链接时,wordpress 根本不会返回报价。我尝试过的所有其他 HTML 似乎都很好,它似乎只是因为以下内容而失败:

[jasminesays quote="blah <a href="#">de</a> blah"]

类似

[jasminesays quote="blah <p>de</p> blah"]

工作正常。

处理短代码的代码是:

function mm_jasmineSays( $atts ) { 
extract( shortcode_atts( array(  
        "quote" => '',
       ), $atts ) );

return '<link href="'.get_bloginfo( 'template_directory' ).'/css/shortcodes.css" rel="stylesheet" type="text/css" />
        <div class="jasmine-says">
            <h2>Jasmine says...</h2>
            <div class="jasmine-says-quote">
                <p><img src="'.get_bloginfo( 'template_directory' ).'/imgs/shortcodes/quote-1.jpg" /></p>
                <p class="quote">'.$quote.'</p>
                <p><img src="'.get_bloginfo( 'template_directory' ).'/imgs/shortcodes/quote-2.jpg" /></p>
            </div>
        </div>';
}
add_shortcode('jasminesays', 'mm_jasmineSays');

但我不认为这是问题所在,我猜 wordpress 正在某个地方过滤某些东西,我需要禁用它。有人有什么想法吗?

感谢您的帮助。

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    来自WordPress Codex

    短代码处理函数的返回值被插入到 发布内容输出代替短代码宏。记得使用 return 而不是 echo - 任何被回显的东西都将输出到 浏览器,但它不会出现在页面上的正确位置。

    在 wpautop 和 wptexturize 后格式化之后解析短代码 已应用(但请参阅下面关于 2.5.0 和 2.5.1 的注释 差异)。这意味着您的简码输出 HTML 不会 自动应用大引号,添加 p 和 br 标签,等等 上。如果您确实希望格式化短代码输出,则应该 返回输出时直接调用 wpautop() 或 wptexturize() 来自您的简码处理程序。

    wpautop 识别简码语法并尝试不包装 p 或 br 标记围绕在单独一行中的短代码。 旨在以这种方式使用的简码应确保 输出被包装在适当的块标签中,例如 p 或 div。

    【讨论】:

    • 添加 $quote = apply_filters('the_content', $quote);到简码功能似乎不起作用,这似乎很奇怪。它似乎只是专门挑选 标签,没有别的。
    【解决方案2】:

    不确定这是否会有所帮助,但您是否尝试过将外引号更改为单引号?

    [jasminesays quote='blah <a href="#">de</a> blah']
    

    还是去掉内引号?

    [jasminesays quote="blah <a href=#>de</a> blah"]
    

    【讨论】:

      【解决方案3】:

      为什么不在短代码中添加 url 选项?

      类似添加的东西:

          function mm_jasmineSays( $atts ) { 
      extract( shortcode_atts( array(  
      "quote" => '',
      "url" => '',
      ), $atts ) );
      

      然后添加

      <a href="'.$url.'"> <h2>Jasmine says...</h2></a>
      

      也许它可以工作.. 或使用 $output 代替返回:

      global $post;
      $output .= '<div class="jasmine-says">';
      if($quote !== '')
      $output .= '<a href="'.$url.'"><h2>Jasmine says...</h2>';
              $output .= '<div class="jasmine-says-quote">'
                  $output .='<p><img src="'.get_bloginfo( 'template_directory' ).'/imgs/shortcodes/quote-1.jpg" /></p>';
                  $output .='<p class="quote">'.$quote.'</p>';
                  $output .='<p><img src="'.get_bloginfo( 'template_directory' ).'/imgs/shortcodes/quote-2.jpg" /></p>';
              $output .='</div>';
          $output .='</div>';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多