2020-11-01 / 6072阅
今天带大家做一个文章列表的短代码,可以在任何地方输出一个文章列表。输出的内容结构如下:

短代码调用方法:
[youran_list title="推荐文章" num="3" ]这里是推荐的文章[/youran_list]
案例代码如下:
function youran_post_list($atts = [], $content = null, $tag = '')
{
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$youran_atts = shortcode_atts([
'title' => '悠然自学网',
'cat' => '',
'num' => 3,
], $atts, $tag);
$html = '<div class="youran_box">';
$html .= '<h2>' . esc_html__($youran_atts['title'], 'youran') . '</h2>';
if (!is_null($content)) {
$html .= '<p>' . apply_filters('the_content', $content) . '</p>';
}
if($youran_atts['cat']) {
$args = array(
'cat' => $youran_atts['cat'],
'showposts' => (int)$youran_atts['num']
);
}else{
$args = array(
'post_type'=>'post',
'showposts' => (int)$youran_atts['num']
);
}
//var_dump($args);
query_posts($args);
if (have_posts()) {
$html .= '<ul>';
while (have_posts()):the_post();
$html .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
endwhile;
wp_reset_query();
$html .= '</ul>';
}
$html .= '</div>';
return $html;
}
function youran_post_list_init()
{
add_shortcode('youran_list', 'youran_post_list');
}
阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228