Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WordPress中获得当前类别内的5篇随机帖子_Wordpress - Fatal编程技术网

在WordPress中获得当前类别内的5篇随机帖子

在WordPress中获得当前类别内的5篇随机帖子,wordpress,Wordpress,我只想得到5个职位在当前类别内随机使用以下代码 // Get all posts within current category, but exclude current post $category_posts = new WP_Query( array( 'cat' => $categories[0]->term_id, 'post__not_in' => array( get_the_ID(

我只想得到5个职位在当前类别内随机使用以下代码

// Get all posts within current category, but exclude current post
        $category_posts = new WP_Query( array(
            'cat'          => $categories[0]->term_id,
            'post__not_in' => array( get_the_ID() ),
        ) );
           // Get all posts within current category, but exclude current post
        $category_posts = new WP_Query( array(
        'orderby'        => 'rand',
        'cat'          => $categories[0]->term_id,
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 3,
        ) );

如何将“5篇文章”限制和“随机排序”应用于上述代码?

至于我,我会使用get_posts(),但这些参数也适用于您的情况:

<?php $args = array(
'numberposts'      => 5,
'category'         => $categories[0]->term_id,
'orderby'          => 'rand',
'exclude'          => array( get_the_ID() ),
'post_type'        => 'post',
'post_status'      => 'publish',
'suppress_filters' => true 
);

$posts_array = get_posts( $args ); ?>


关于这方面的更多信息:

我使用了以下代码

// Get all posts within current category, but exclude current post
        $category_posts = new WP_Query( array(
            'cat'          => $categories[0]->term_id,
            'post__not_in' => array( get_the_ID() ),
        ) );
           // Get all posts within current category, but exclude current post
        $category_posts = new WP_Query( array(
        'orderby'        => 'rand',
        'cat'          => $categories[0]->term_id,
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 3,
        ) );
,