在我的主頁上使用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);
}
謝謝你的建議!
我會避免查詢中的ORDER BY RAND(),這是一個相當慢的查詢參數。你可以隨意使用php來代替...你想讓所有的貼紙都在每一頁上面嗎?或者只是在那個恰好位於該特定頁面的隨機custom_query中的粘貼文章? – hexalys
謝謝你指出。是的,所有膠粘物都在輸出中。 –