On my menu, i want to be a a link called"Random" that when clicked it will open a random article, just like on stumbleupon. I made the template thinkink i may go from there and achieve this. But i couldn't. I m asking you how to open the only link that exists on the page and wich its generated ramdomly by the template, on itself(with the help of that php code). Kind of a redirect i want (but fast).I thought this might work in a 2 stept action: 1st, the user click on Random from my menu, then the /randompage opens,with a random link(one of my article link) on it. 2nd i want that link to open direcly(auto), no other loadind of the page needed. Can it be done this way ? or is there something else to it ? The page is completely empty. (My website is now offline due to dns change. It will be up and running in a few hours). Hope i made my self understood, i dont know how else to explain this. I m really sory if i cannot explain this better.
-
Show the code you are using in your template – brasofilo Jan 26 '13 at 14:02
-
1, 'orderby' => 'rand' ); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?>
- – Florin Dorinel Buricea Jan 26 '13 at 16:02
-
Sorry, add new information to the Question itself ([edit]). Don't forget to read the [faq] ;) – brasofilo Jan 26 '13 at 16:05
2 Answers
Maybe, this approach would be better:
I've prepared a Custom Walker for Nav menu, which generates random link to a post for menu item with class "random" assigned from administration menu.
So, to get this work, you have to copy and paste this code to your functions.php
class Random_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
if ( !empty( $classes ) && in_array( 'random', $classes ) ){
$args = array( 'numberposts' => 1, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $p ){
$item->url = get_permalink( $p->ID );
}
}
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
Than, you have to find a code of your wp_nav_menu in your template and assign a parameter 'walker' with 'new Random_Walker_Nav_Menu()'. Like this one:
wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new Random_Walker_Nav_Menu() ) );
Than you'll enter your administration Appearance -> Menu and will add new custom link to a desired menu. URL does not matter really. So, choose your blog ULR. Call it "Random Post" or "Random", it is up to you. Than assign it a class "random" (without qoutes). If class field was not showing to you, enable it from "Screen Options" in upper right corner. Save.
Voila!

- 1,058
- 9
- 15
-
i have done everithing you said, and so far so good, nothing ruinned, but there is still a problem.. i cant figuer it out.. how to do what you said about the replacement of that line. I've found it. its this : wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'menu_id' => 'primary-menu' ) ); I found it in header.php. How do i edit it to work as you said ? couse any combination i tried, nothing happened – Florin Dorinel Buricea Jan 27 '13 at 00:39
-
wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'menu_id' => 'primary-menu', 'walker' => new Random_Walker_Nav_Menu() ) ); – david.binda Jan 27 '13 at 08:34
All right sparky. I've got also another solution, maybe easier for implementation.
Insert this code to your functions.php and modify $page_id value to an ID of your page with custom template:
function binda_redirect_to_rand( $query ) {
$page_id = 234; //ID of your page with custom template
if ( $query->is_page() && $query->query_vars['page_id'] == $page_id && $query->is_main_query() ) {
$args = array( 'numberposts' => 1, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $p ){
$query->set( 'p', $p->ID );
$query->set( 'page_id', '' );
}
}
return $query;
}
add_action( 'pre_get_posts', 'binda_redirect_to_rand' );
This piece of code redirects a query leading to a page with specified ID to a random post before any post data are fetched from DB - so it is equally fast as my first solution. I prefer previous one, because you do not have to create a redundant page, but this one is really easy to implement.

- 1,058
- 9
- 15