You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义carousel文章类型首页轮播无法获取标题摘要链接如何解决

问题原因&解决方案

自定义文章类型注册修正

你当前的注册代码遗漏了关键的钩子挂载逻辑,可能导致carousel文章类型根本没有成功注册,另外supports参数里的permalink是无效值,无需填写,修正后的代码如下:

<?php
function my_function() {
    $labels = array(
        'name'               => __( 'slideshow' ),
        'singular_name'      => __( 'slideshow' ),
        'menu_name'          => __( 'slideshow' ),
        'name_admin_bar'     => __( 'slideshow' ),
        'add_new'            => __( 'add picture ' ),
        'add_new_item'       => __( 'slideshow' ),
        'new_item'           => __( 'new post' ),
        'edit_item'          => __( 'edit post' ),
        'view_item'          => __( 'view post' ),
        'all_items'          => __( 'all picturs' ),
        'search_items'       => __( 'search' ),
        'parent_item_colon'  => __( 'parrent' ),
        'not_found'          => __( 'not found' ),
        'not_found_in_trash' => __( 'not found in trash' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'carousel post type' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'carousel' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' )
    );

    register_post_type( 'carousel', $args );
}
// 新增挂载逻辑,将自定义文章类型注册到WordPress
add_action( 'init', 'my_function' );

首页调用代码修正

你当前调用代码存在三个核心错误:

  • WordPress的文章对象没有post_permalink属性,永久链接需要通过get_permalink(文章ID)函数获取
  • 直接取$slide->post_excerpt只能拿到手动填写的摘要,且没有做安全转义
  • 标题、图片链接等输出内容没有做安全转义,存在XSS风险

修正后的调用代码如下:

<?php $slider = get_posts(array('post_type' => 'carousel', 'posts_per_page' => 3)); ?>
<?php $count = 0; ?>
<?php foreach ($slider as $slide) : ?>
<div class="carousel-item <?php echo ($count == 0) ? 'active' : ''; ?>">
    <img src="<?php echo esc_url( wp_get_attachment_url(get_post_thumbnail_id($slide->ID)) ); ?>" class="d-block w-100" alt="<?php echo esc_attr( $slide->post_title ); ?>">
    <div class="carousel-caption d-none d-md-block">
        <h3 class="text-right"><?php echo esc_html( $slide->post_title ); ?></h3>
        <div class="text-right"><?php echo esc_html( $slide->post_excerpt ); ?></div>
        <div><a href="<?php echo esc_url( get_permalink( $slide->ID ) ); ?>" class="btn btn-custom float-left" >ادامه مطلب</a></div>
    </div>
</div>
<?php $count++; ?>
<?php endforeach; ?>

附加注意事项

  • 确认你的主题已开启特色图支持,需要在主题functions.php中加入add_theme_support( 'post-thumbnails' );才能正常获取轮播图
  • 如果需要自动截取文章内容作为摘要,可以把摘要输出部分替换为echo wp_trim_words( get_post_field( 'post_content', $slide->ID ), 15 );,其中数字15是截取的字数,可自行调整
  • 发布轮播文章时请手动设置特色图、填写标题和摘要,确保内容正常展示

内容的提问来源于stack exchange,提问作者mohamad mehdi hajati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 17:06:01