首页 > 解决方案 > 在自定义字段之后显示自定义帖子类型的 get_content

问题描述

我创建了自己的简码来显示来自自定义帖子类型的数据。我可以在我的页面上使用自定义值显示不同的汽车类型。但我的 CPT 还支持编辑器(WPBakery Page Builder),管理员可以在其中为每辆车添加/自定义额外内容。(附加信息)。

我的问题:

我想在自定义字段下方添加/显示编辑器的内容(=> the_content)。但是,一旦我输出“the_content”,它就会出现在每种类型汽车的页面顶部。我可以使用“get_the_content”在正确的位置('.$content.')显示数据,但它不会包含任何格式,也不会显示图片,而是输出整个 img-tag + 信息。

有没有办法解决这个问题?

先感谢您!

使用 the_content() 输出

在此处输入图像描述

使用 get_the_content() 输出

在此处输入图像描述 在我的页面上显示内容的简码:

<?php
if(!defined('ABSPATH')){
   exit;
}

add_shortcode('fahrzeuge', 'display_fahrzeuge');

function display_fahrzeuge($atts, $content = null) {

$output = '';

$args = array(
'post_type'   => 'fahrzeuge',
'post_status' => 'publish',
'tax_query'   => array(
 array(
    'taxonomy' => 'Fahrzeugtypen',
    'field'    => 'slug',
    'terms'    => 'kommandofahrzeuge')));

$output.= '';
$kommandofahrzeuge = new WP_Query( $args );
if( $kommandofahrzeuge->have_posts() ) :
   while( $kommandofahrzeuge->have_posts() ) :

    $kommandofahrzeuge->the_post();
    $post_id = get_the_ID();
    $taktischeBezeichnung = esc_html(get_post_meta($post_id,'ff_ried_fahrzeuge_taktischeBezeichnung', true));

    $featured_img_url = get_the_post_thumbnail_url($post_id,'full'); 

    $output.='
    <div>'.$taktischeBezeichnung.'</div>
    <img class="fahrzeugeFeaturedImage" src='.$featured_img_url.'></img>
    <div>'.$content.'</div>
    ';        

  endwhile;

  wp_reset_postdata();

else :
  esc_html_e( 'Keine Fahrzeuge gefunden', 'text-domain' );
endif;

echo $output;   
}

?>

标签: wordpresscustom-post-type

解决方案


解决方案:

将此函数插入​​到functions.php中

function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    return $content;
}

显示格式化的内容:

$content = get_the_content_with_formatting();
echo $content;

来源:http ://www.web-templates.nu/2008/08/31/get_the_content-with-formatting/index.html


推荐阅读