在自定义字段后显示自定义帖子类型的 get_content

问题描述 投票:0回答:1

我创建了自己的短代码来显示自定义帖子类型的数据。 我可以在页面上显示具有自定义值的不同汽车类型。但我的 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;   
}

?>
wordpress custom-post-type
1个回答
0
投票

解决方案:

将此函数插入到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;
© www.soinside.com 2019 - 2024. All rights reserved.