我是新手。我需要简码来显示我们所在类别的文章(但没有当前文章)。现在,默认情况下,代码显示所有类别或类别中文章的名称,以及用户在参数中输入的数目。
add_shortcode( 'art_related_posts', 'related_posts_function' );
function related_posts_function ($atts) {
$atts = shortcode_atts( array(
'count' => 3,
'category' => $cat,
), $atts );
$cat = get_the_category( $post->ID );
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $atts['count'],
'category__in' => $atts['category'],
);
$out_posts = get_posts( $args );
$out = '<style>
.art-rp{
background: #ddd;
padding: 20px 20px;
}
</style>';
$out .= '<ul class="art-rp">';
foreach ($out_posts as $post) {
setup_postdata( $post );
$out .= '<li><a href="'. get_the_permalink($post->ID) .'">'. get_the_title( $post->ID ) . '</a></li>';
}
$out .= '</ul>';
wp_reset_postdata();
return $out;
}