自定义帖子类型添加了额外的html标签

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

我有一个称为服务的自定义帖子类型,但是在显示它们时,它在代码中为每个帖子添加了两个额外的<p>标签。我不知道为什么。

这是帖子类型的注册:

    function services_post_type() {
    $args = array(
        'labels'        => array(
                        'name' => __( 'Services', 'services' ),
                        'singular_name' => __( 'Service', 'service' ),
                        'menu_name' => 'Services',
                        ),
        'description'   => 'Add a service for your website.',
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'public'        => true,
        'menu_position' => 5,
        'menu_icon'     => 'dashicons-text-page',
        'has_archive'   => true,
        'rewrite'       => array('slug' => 'services'),
    );
    register_post_type( 'services', $args );
}
add_action( 'init', 'services_post_type' );

这是帖子类型的显示:

<div class="row">
    <?php
    // The Query
    $query = new WP_Query(array('post_type' => 'services', 'order' => 'ASC'));
    query_posts( $query );

    // The Loop
    while ( $query->have_posts() ) : $query->the_post();
    ?>
    <div class="col-6">
        <h3 class="service-item-title"><?php the_title(); ?></h3>
        <p class="service-item-content"><?php the_content(); ?></p>
    </div>
    <?php
    endwhile;

    // Reset Query
    wp_reset_query();
    ?>
</div>

这是检查显示的内容:Inspect Screenshot

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

the_content()直接回显all内容,包括其HTML标记(通常为p标记),因此您不应将其放入p标记中。如果您需要添加一个类,请使用DIV标签作为其容器:

<h3 class="service-item-title"><?php the_title(); ?></h3>
<div class="service-item-content">
  <?php the_content(); ?>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.