gpt4 book ai didi

wordpress - 带有便签的一致随机帖子集

转载 作者:行者123 更新时间:2023-12-02 22:57:15 25 4
gpt4 key购买 nike

使用 WordPress,在我的主页上,我希望能够查询整个分页中一致的随机帖子,同时仍首先显示便签。我已经尽力创建一致的流程,但我错过了像其他帖子一样随机显示的便签。

function custom_query($query) {
global $custom_query;
if ( $custom_query && strpos($query, 'ORDER BY RAND()') !== false ) {
$query = str_replace( 'ORDER BY RAND()', $custom_query, $query );
}
return $query;
}

add_filter( 'query', 'custom_query' );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$seed = $_SESSION['seed'];

if ( empty($seed) ) {
$seed = rand();
$_SESSION['seed'] = $seed;
}

global $custom_query;
$custom_query = " ORDER BY rand($seed) ";

$args = array(
'caller_get_posts' => 1,
'orderby' => 'rand',
'paged' => $paged,
);

query_posts($args);
$custom_query = '';

编辑:根据您的建议,我设法使用以下代码解决了该问题:

$sticky_post_ids = get_option('sticky_posts');
function mam_posts_query($query) {
global $mam_posts_query;
if ($mam_posts_query && strpos( $query, 'ORDER BY RAND()') !== false ) {
$query = str_replace( 'ORDER BY RAND()', $mam_posts_query, $query );
}
return $query;
}
add_filter( 'query','mam_posts_query' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$seed = date('Ymdh'); // Sets an hourly random cache
global $mam_posts_query;
$mam_posts_query = " ORDER BY rand($seed) ";

$args = array(
'orderby' => 'rand',
'paged' => $paged,
'post__not_in' => get_option( 'sticky_posts' )
);
$projects = query_posts($args);
$mam_posts_query = '';

if ( $paged === 1 ) {
$stickies = get_posts( array('include' => $sticky_post_ids) );
$projects = array_merge( $stickies, $projects );
}

感谢您的建议!

最佳答案

我知道codex wp_query pag中的描述e 有点令人困惑,但我相信您需要做的就是设置

'ignore_sticky_posts' => 0

这在我的实验中是有效的,但是当然,在处理查询时,我不知道您的查询在另一个位置的何处或何时可能会发生变化..

无论如何,如果这对您不起作用,您也可以使用以下方法获得便签

$sticky = get_option( 'sticky_posts' );

然后像这样设置查询:

'post__in' => get_option('sticky_posts')

甚至如此:(注意not_in)

 $query->set( 'post__not_in', get_option( 'sticky_posts' ) );

请注意,默认情况下,即时贴仅显示在主页上。

您也可以使用双循环方法:

$stickyQuery = new WP_Query( array(
'cat' => $your_category,// example
'ignore_sticky_posts' => 0,
'post__in' => get_option( 'sticky_posts' ),
'posts_per_page' => -1 //Get ALL and ONLY the stickies, or how many you want
);
while( $stickyQuery->have_posts() ) : $stickyQuery->the_post();

//... ( Sticky Posts should show )

endwhile;
wp_reset_query();

//... ( Continue main query or start a new one excluding the last.... )

关于wordpress - 带有便签的一致随机帖子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16023857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com