I'm a newbie. I need the shortcode to display articles of the category in which we are (but without the current article). Now the code displays, by default, the names of articles from all categories or the category, the number of which the user enters in the parameters.

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;
}

0 Answers