首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义post列表项短代码

自定义post列表项短代码
EN

Stack Overflow用户
提问于 2016-02-14 16:15:32
回答 1查看 84关注 0票数 0

如何在一个中添加这两个短代码,因为我想在几个类别中使用这个代码。也请告诉我如何在这个简短的代码中添加类别。

现在我的简短代码是[featured-post],我想要这个[featured-post='category name']

准确地说,我需要一个短代码5的帖子将显示从一个类别,但第一个帖子有长春花,然后标题,然后摘录,然后其余的4个列表,cetegory的帖子只显示标题和permalink。

代码语言:javascript
复制
    function featured_post_shortcode($atts){
    extract( shortcode_atts( array(), 
    $atts, 'featured-post' ) );

    $q = new WP_Query(
        array( 'category' => $category, 'posts_per_page' => '1', 'post_type' => 'post')
        );
$list = '';

while($q->have_posts()) : $q->the_post();
    //get the ID of your post in the loop
    $id = get_the_ID();


    $post_thumbnail= get_the_post_thumbnail( $post->ID, 'lead-thumbnail' ); 
    $list .= '

        <div class="featured">
                            '.$post_thumbnail.'
            <a href="'.get_permalink().'">'.get_the_title().'</a>
            <p>' . get_the_excerpt() . '</p>
            </div>  

    ';        
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('featured-post', 'featured_post_shortcode');  



function more_item_shortcode($atts){
    extract( shortcode_atts( array(), 
    $atts, 'featured-post' ) );

    $q = new WP_Query(
        array( 'category' => $category, 'posts_per_page' => '4', 'post_type' => 'post')
        );
$list = '<ul>';

while($q->have_posts()) : $q->the_post();
    //get the ID of your post in the loop
    $id = get_the_ID();



    $list .= 

        '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';


endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('more-item', 'more_item_shortcode');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-14 18:54:21

代码语言:javascript
复制
function featured_post_shortcode($atts){
    extract(shortcode_atts( array('cat' => ''), $atts));

    $q = new WP_Query(
            array( 'cat' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
            //Use category slug: array( 'category_name' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
        );
    $count = 0;
    if( $q->have_posts() ): 
        $output = '<ul>';
        while($q->have_posts()) : $q->the_post(); $count++; global $post;

            if($count == 1){
                $output .= '
                    <div class="featured">'.
                        get_the_post_thumbnail($post->ID, 'lead-thumbnail').
                        '<a href="'.get_permalink().'">'.get_the_title().'</a>
                        <p>' . get_the_excerpt() . '</p>
                    </div>
                ';
            }else{
                $output .= '
                    <li>
                        <a href="'.get_permalink().'">'.get_the_title().'</a>
                    </li>
                ';  
            }
        endwhile; 
        $output .= '</ul>';
    endif;
wp_reset_query();
return $output;
}
add_shortcode('featured-post', 'featured_post_shortcode'); 

如果您想使用类别段格,那么使用category_name,否则使用cat,它接受类别ID

在您的回调:[featured-post cat="1"]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35393974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档