自定义 post 列表项简码
custom post list item shortcode
如何将这 2 个短代码添加到一个中,因为我想在多个类别中使用它。还请告诉我如何在此短代码中添加类别。
现在我的短代码是 [featured-post]
我想要这个[featured-post='category name']
确切地说,我需要一个简码 5 post 将从一个类别中显示,但首先 post 有 trumbanail 然后是标题然后是摘录然后其余 4 个列表 cetegory post 只显示标题和固定链接
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');
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"]
如何将这 2 个短代码添加到一个中,因为我想在多个类别中使用它。还请告诉我如何在此短代码中添加类别。
现在我的短代码是 [featured-post]
我想要这个[featured-post='category name']
确切地说,我需要一个简码 5 post 将从一个类别中显示,但首先 post 有 trumbanail 然后是标题然后是摘录然后其余 4 个列表 cetegory post 只显示标题和固定链接
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');
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"]