2013-11-21 40 views
0

我想隨機發佈一個隨機的按鈕,點擊。我想要下面的東西。我想在wordpress中硬編碼一個'隨機發布'按鈕

<div class="randomize"> 
    <a href="http://www.example.com/?p=<?php get_post('orderby=rand'); ?>" > 
     <img src="http://www.example.com/randomize-button.png" /> 
    </a> 
</div> 

任何幫助,將不勝感激。

回答

-2

這裏是一個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。

+0

非常感謝奏效。 – Paul

+0

沒問題,很高興我能幫到你。 –

+0

提供基準代碼以供每個評論請求參考。 –

3

你可以試試這個

<?php 
    $args = array('orderby' => 'rand', 'type'=>'post', 'posts_per_page'=>1); 
    $post = get_posts($args); 
?> 
<a href="<?php echo get_permalink($post[0]->ID); ?>" > 
    <img src="http://www.example.com/randomize-button.png" /> 
</a> 
相關問題