如何在一个中添加这两个短代码,因为我想在几个类别中使用这个代码。也请告诉我如何在这个简短的代码中添加类别。
现在我的简短代码是[featured-post],我想要这个[featured-post='category name']
准确地说,我需要一个短代码5的帖子将显示从一个类别,但第一个帖子有长春花,然后标题,然后摘录,然后其余的4个列表,cetegory的帖子只显示标题和permalink。
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');发布于 2016-02-14 18:54:21
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"]
https://stackoverflow.com/questions/35393974
复制相似问题