【问题标题】:Wordpress doesn't interpret html tag in blogpostWordpress 不解释博客文章中的 html 标记
【发布时间】:2015-01-24 20:06:54
【问题描述】:

当我在 wordpress 中使用 tinymce 插件添加 youtube 视频时,它在可视化编辑器部分效果很好。

但在我的自定义模板中,它只是显示

[embed]https://youtube.com/my-video-link[/embed]

对不起,也许有一个明显的答案,但我对 wordpress 完全陌生,而谷歌只是给了我相反的答案,关于如何从博客文章内容中转义 html..

我不知道我错过了什么,但提前谢谢你

编辑

我不使用the_content() 函数来回显我的 post_content,但我尝试并且它有效,因此该函数必须使用一个函数来转换我在 youtube iframe 中的嵌入标签。 因为我需要从某个类别中获取最后一篇文章,所以我存储了 get_posts(['category_name' => 'My Category Name', 'showpost' => 1]) 在变量中然后我做$mySpecificCategory[0]->post_content。其实不知道是不是好办法。

解决方案

好的,我在wordpress support 上找到了解决方案。即:

<?php echo apply_filters( 'the_content',  $mySpecificCategory[0]->post_content) ?>

【问题讨论】:

  • 您能分享您的确切网址格式吗?查看oembed code,URL 中需要有watchplaylist,除非您使用youtu.be 域。如果您的意思是在模板的 PHP 文件中有 [embed]https://youtube.com/my-video-link[/embed] 作为文字文本,则需要通过 do_shortcode 函数和 echo 结果运行它
  • 所以我从编辑器中的网址添加了一个新媒体。这是我使用的链接格式:“youtube.com/embed/BEG-ly9tQGk”,但它也适用于youtube.com/watch?v=BEG-ly9tQGk。当我说“它有效”时,是因为我在文章编辑器中有预览。无论链接如何,它都会在博客文章页面上显示 [embed]youtube.com/watch?v=BEG-ly9tQGk[/embed]。但我会尝试 do_shortcode。请注意,我的帖子内容中还有其他文本和 html 代码,而不仅仅是 [embed] 标记。

标签: php html wordpress tags


【解决方案1】:

使用do_shortcode()函数将embed标签转换为youtube iframe:

echo do_shortcode( $content );

查看http://codex.wordpress.org/Function_Reference/do_shortcode了解更多详情

【讨论】:

  • 好的,我试试看,但什么也没显示。我打开控制台查看呈现的 html,但什么也没有。我什至尝试像这样放置一个随机链接 youtube.com/watch?v=BEG-ly9tQGk[/embed]' ) ?> 但什么也没有。那么我错过了什么?我重复一遍,在我的 post_content 值中,我不仅有嵌入代码,还有文本和 html 标签
【解决方案2】:

解决方案

好的,我在wordpress support 上找到了解决方案。即:

<?php echo apply_filters( 'the_content',  $mySpecificCategory[0]->post_content) ?>

【讨论】:

    【解决方案3】:

    带有短代码的内容被短代码处理程序的输出所取代。

    例子

    add_filter( 'the_content', 'do_shortcode', 11 ); // From shortcodes.php
    
    // Use shortcode in a PHP file (outside the post editor).
    
    echo do_shortcode( '[gallery]' );
    
    // In case there is opening and closing shortcode.
    
    echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
    
    // Use shortcodes in text widgets.
    
    add_filter( 'widget_text', 'do_shortcode' );
    
    // Use shortcodes in form like Landing Page Template.
    
    echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
    
    // Store the short code in a variable.
    
    $var = do_shortcode( '[gallery]' );
    
    echo $var;
    

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多