這裏是一個walkthru供你參考:
Random Post Button
INSERT INTO:functions.php的
add_action('init','explore_button');
function explore_button() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
添加到您的HTML:
<div class="explore">
<div><a href="http://example.com/index.php?random=1 ">EXPLORE</a></div>
添加自定義CSS。
非常感謝奏效。 – Paul
沒問題,很高興我能幫到你。 –
提供基準代碼以供每個評論請求參考。 –