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

如何用短码循环显示CPT leistungencpt特定分类的文章?

问题分析与修正方案

原代码的核心问题

  • 分类参数错误:误用了WooCommerce专属的product_cat参数,自定义文章类型的分类查询需使用tax_query
  • 默认值无效:posts_per_page的默认值#不是合法数字,会导致查询逻辑出错
  • 未注册短码:仅开启了小工具的短码支持,但未注册自定义短码,前端无法识别执行

修正后的完整代码

<?php
function my_custom_sliderlesoftware( $atts, $content = null ) {
    // 开启输出缓冲
    ob_start(); 
    
    // 设置短码属性默认值,-1表示显示所有文章,也可改为具体数字如6
    $atts = shortcode_atts(array(
        'anzahl' => -1,
    ), $atts); 
    
    // 正确的自定义文章类型+分类查询参数
    $args = array( 
        'post_type' => 'leistungencpt',
        'posts_per_page' => intval($atts['anzahl']), // 转为整数确保参数合法
        'post_status' => 'publish', // 仅查询已发布文章
        'facetwp' => true,
        // 使用tax_query查询自定义分类
        'tax_query' => array(
            array(
                'taxonomy' => '你的自定义分类法slug', // 替换为你为leistungencpt配置的分类法标识
                'field'    => 'slug',
                'terms'    => 'le-software-entwicklung', // 目标分类的slug
            ),
        ),
    );

    $query = new WP_Query( $args );
?>

<div class="post_box"> 
    <?php if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?> 
            <div class="vc_col-sm-4 post_boxinner"> 
                <div class="post_img"> 
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                        <?php echo get_the_post_thumbnail( get_the_ID(), 'startgrid' ); ?>
                    </a>
                </div> 
                <h3 class="post_headline"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>  
                <p class="post_excerpt">
                    <?php echo wp_trim_words( get_the_excerpt(), 22, ' […]' ); ?>
                </p> 
                <div class="vc_btn3-container vc_btn3-left vc_do_btn">
                    <a class="vc_general vc_btn3 vc_btn3-size- vc_btn3-shape- vc_btn3-style-btn btn-rahmen vc_btn3-color-green" href="<?php the_permalink() ?>" title="Weiterlesen">Weiterlesen</a>
                </div>
            </div> 
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else : ?>
        <!-- 无匹配文章时的提示 -->
        <p>暂无相关文章</p>
    <?php endif; ?>
</div>

<?php
    // 获取缓冲内容
    $sc = ob_get_contents();
    // 清空缓冲
    ob_end_clean();
    // 返回短码渲染内容
    return $sc;
}
// 注册短码,前端可使用[my_custom_sliderlesoftware]或[my_custom_sliderlesoftware anzahl="6"]调用
add_shortcode('my_custom_sliderlesoftware', 'my_custom_sliderlesoftware');
// 允许在小工具中使用短码
add_filter('widget_text','do_shortcode');

关键修改说明

  • 替换分类查询逻辑:用tax_query替代product_cat,需将taxonomy的值替换为你为leistungencpt配置的自定义分类法slug(即注册分类时设置的标识)
  • 修复参数合法性:将anzahl默认值改为合法的-1,并通过intval()确保参数为整数
  • 注册短码:添加add_shortcode()函数,让WordPress识别并执行该自定义短码
  • 增加空状态提示:无匹配文章时显示友好提示,避免页面空白
  • 优化代码结构:调整循环缩进,提升代码可读性

内容的提问来源于stack exchange,提问作者janw

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 04:36:04