(Wordpress)如何获取带有 html 标签的帖子的全部内容 - 未剥离

2022-01-18 00:00:00 tags php wordpress html qtranslate

我正在为我的网站使用带有 qtranslate 插件的 WordPress,并且我正在尝试在每个帖子中显示语言标志.

I'm using WordPress for my site with the qtranslate plugin and i'm trying to display language flags in each post.

Qtranslate 将 html 标签插入到内容和标题中,例如"!--:zh-->"对于我在每篇文章中使用的每种语言

Qtranslate inserts html tags to the content and title like "!--:en-->" for each language that i used in each post

所以我需要一个条件来检查这些 html 标记中的哪些包含在内容中,以便我可以打印特定的标志

So i need a conditional that checks which of these html tags are included in the content so i can print the specific flags

类似这样的:

function language_pick(){
    $qt_dir = "http://localhost/MY-SITE/wp-content/plugins/qtranslate-xp/flags/";
    $cr_url = "http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    $en_url = esc_html($cr_url."&lang=en");
    $fr_url = esc_html($cr_url."&lang=fr");
    $it_url = esc_html($cr_url."&lang=it");
    $es_url = esc_html($cr_url."&lang=es");

    $query = get_post(get_the_ID()); 
    $content = apply_filters('the_content', $query->post_content);

    if(get_permalink() != $cr_url) { echo '<a style="margin-left:15px;" href="'.$cr_url.'" /><img src="'.$qt_dir.'gr.png"></a>'; }
    if (strpos($content, '<!--:en-->') === true) {
         if(get_permalink() != $en_url) { echo '<a style="margin-left:15px;" href="'.$en_url.'" /><img src="'.$qt_dir.'gb.png"></a>'; } }
    if(strpos($content,'<!--:fr-->') === true) {
        if(get_permalink() != $fr_url) { echo '<a style="margin-left:15px;" href="'.$fr_url.'" /><img src="'.$qt_dir.'fr.png"></a>'; } }
    if(strpos($content,'<!--:it-->') === true) {
        if(get_permalink() != $it_url) { echo '<a style="margin-left:15px;" href="'.$it_url.'" /><img src="'.$qt_dir.'it.png"></a>'; } }
    if(strpos($content,'<!--:es-->') === true) {
        if(get_permalink() != $es_url) { echo '<a style="margin-left:15px;" href="'.$es_url.'" /><img src="'.$qt_dir.'es.png"></a>'; } }
}

推荐答案

很简单的添加 <?= apply_filters('the_content', $content);?>

Google 上有很多关于此的引用.

There are loads of references to this on Google.

编辑所以在这种情况下:

$query = get_post(get_the_ID()); 
$content = apply_filters('the_content', $query->post_content);

echo $content;

相关文章