自定义帖子列表项短信

时间:2016-02-14 16:15:32

标签: wordpress categories custom-post-type

如何在一个中添加这2个短代码,因为我想在几个类别中使用它。另请告诉我如何在此简短代码中添加类别。

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

确切地说,我需要一个短信5帖子将从一个类别显示,但第一个帖子有trumbanail然后标题然后摘录然后其余4个列表cetegory帖子只显示标题和永久链接

    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');

1 个答案:

答案 0 :(得分:0)

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'); 

如果您想使用类别slug,请使用category_name,否则使用cat,其中包含类别ID

回调中的

[featured-post cat="1"]