dqxz96998 2014-08-28 04:28
浏览 15

如何通过短代码显示wordpress类别名称?

    function Service_shortcode($atts , $content = null){
        extract(shortcode_atts(array(
        'title'=>'',
        ),$atts
        ));
        $q = new WP_Query(
            array('posts_per_page'=>3, 'post_type'=>'services')
            );

            $list = '
                <h1>'.$title.'</h1>
                <ul>
            ';
            while ($q->have_posts()) : $q->the_post();          
                $list.='
                    <li>
                        <h3>'.get_the_title ().'</h3>
                        <h6>'.the_category(',').'</h6>
                        <p>'.get_the_content().'</p>
                    </li>
                ';
            endwhile;
            $list.='</ul>';
            wp_reset_query();

        return $list;
            }  
        add_shortcode('service','Service_shortcode');

when use it the_category(',') in theme it's show the category name but when use it in shortcode not working. which code use to show category name by shortcode.

  • 写回答

1条回答 默认 最新

  • douxia5179 2014-08-28 05:18
    关注

    try this code :

    function categories_list_func( $atts ){
         $categories = get_the_category();
    
             if($categories) {
                foreach($categories as $category) {
                    $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
                }
                $second_output = trim($output);
              }
              $return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
    
         return $return_string;
    
        } // END Categories
        add_shortcode( 'categories-list', 'categories_list_func' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大