Kategorien
WordPress

WordPress related Artikel aus denselben Kategorien

Um bei WordPress die letzten 5 Artikel aller Kategorie eines Artikels (es kann mehrere geben) auszugeben, braucht man den folgenden Code:

function getMyRelatedPosts()
{
    $categories = get_the_category();
    $categoriesArray = array();
    if($categories){
        foreach($categories as $category) {
            $categoriesArray[] = $category->term_id;
        }
    }
    $queryRelatedPosts = new WP_Query(
        array (
            'category__and' => $categoriesArray,
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 5,
            'post_status' => array( 'publish')
        )
    );
    return $queryRelatedPosts;
}
                        $relatedPosts = getMyRelatedPosts();
                            while ( $relatedPosts->have_posts() ) :
                                $relatedPosts->the_post();
                                echo " <a class='relatedPosts' href='".get_permalink()."' title='".the_title_attribute('echo=0')."'>".get_the_title()."</a><br>";
                            endwhile;
                            wp_reset_postdata();