特色-随机顺序的内容/特色帖子

时间:2015-08-07 作者:vega

我在dailysuit工作。de,在顶部有四个特色帖子,由某个标签定义。订单从最新到最旧。

How do I get this order randomised (and not newest to oldest)?

我试图在inc/featured内容中更改如下内容。php:

// Query for featured posts.
$featured = get_posts( array(
        \'numberposts\' => $settings[\'quantity\'],
        \'tax_query\'   => array(
            array(
                \'field\'    => \'term_id\',
                \'taxonomy\' => \'post_tag\',
                \'terms\'    => $tag,
            ),
        ),
    ) );

// Query for featured posts.
$featured = get_posts( array(
        \'numberposts\' => $settings[\'quantity\'],
        \'tax_query\'   => array(
            array(
                \'field\'    => \'term_id\',
                \'taxonomy\' => \'post_tag\',
                \'terms\'    => $tag,
            ),
        ),
        \'orderby\'          => \'rand\',
    ) );
但有两个缺陷:

1. It did not work/randomise.

2. Changing this file is a bad idea. If the theme gets an update, the randomising is gone.

在儿童主题中是否有可行的解决方案?提前非常感谢。

1 个回复
最合适的回答,由SO网友:birgire 整理而成

第三方主题是离题的,但我最终还是检查了一下:

问题是什么;在您发布的代码很重要之后:

Before:

// Return array of cached results if they exist.
$featured_ids = get_transient( \'featured_content_ids\' );
if ( ! empty( $featured_ids ) ) {
    return array_map( \'absint\', (array) $featured_ids );
}

After:

// Ensure correct format before save/return.
$featured_ids = wp_list_pluck( (array) $featured, \'ID\' );
$featured_ids = array_map( \'absint\', $featured_ids );
set_transient( \'featured_content_ids\', $featured_ids );
这意味着您的主题正在通过Transients API缓存特色帖子ID。

所以随机化不会像您预期的那样工作。

默认TTL为0,这意味着它永远不会过期,直到手动删除为止。

解决方法有四种不同的方法:

Method 1)

相反,请在中修改此Featured_Content::get_featured_posts():

$featured_posts = get_posts( array(
        \'include\'        => $post_ids,
        \'posts_per_page\' => count( $post_ids ),
) );
收件人:

$featured_posts = get_posts( array(
        \'include\'        => $post_ids,
        \'posts_per_page\' => count( $post_ids ),
        \'orderby\'        => \'rand\'
) );
在您的孩子主题中。

Method 2)

我们还可以用longform_get_featured_posts 过滤器:

add_filter( \'longform_get_featured_posts\', function( $posts )
{
    shuffle( $posts );
    return $posts;
}, 11 );
Method 3)

我们可以修改orderbypre_get_posts 挂钩:

add_action( \'pre_get_posts\', function( $q )
{
    if( 
           did_action( \'longform_featured_posts_before\' ) 
        && ! did_action( \'longform_featured_posts_after\' ) 
    )
        $q->set( \'orderby\', \'rand\' );            
}, 99 );
longform_featured_posts_beforelongform_featured_posts_after 挂钩。

Method 4)

我们可以试着增加主题的quantity 选项(从后端)设置为6,但仅显示4(仅作为示例)。然后,我们可以使用方法#2的修改:

add_filter( \'longform_get_featured_posts\', function( $posts )
{
    shuffle( $posts );

    if( count( $posts ) > 4  )
        $posts = array_slice( $posts, 0, 4 );

    return $posts;
}, 11 );
增加上述数字6, 然后max_posts 需要从此代码部分进行修改:

// Add support for featured content.
add_theme_support( \'featured-content\', array(
    \'featured_content_filter\' => \'longform_get_featured_posts\',
    \'max_posts\' => 6,
) );
可能会有许多其他的变化,但我就到此为止;-)

请注意,我以前从未安装或使用过此主题。这些只是基于略读主题的一些想法source code.

结束