WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Random post according to localization (3 posts)

  1. dragoeco
    Member
    Posted 6 months ago #

    Hi all. I'm searching for a way to get a ling to redirect to a radom post according the language selected by the visitor. I've created my own button but I just need a link who can do that. Maybe someting like:
    mysite.com/?display-post&orderby=rand
    or maybe a modification to function.php file like here
    I have 2 languages and about 10 categories on each languages.
    If anyone have an idea, he'll makes my day :) Thanks

  2. dragoeco
    Member
    Posted 6 months ago #

    Ok, I discovered that I have a randompost.php file with my template. Here is the code:

    <?php
    /* Template Name: Random Post */
    $randomPost = get_posts(array(
    'numberposts' => 1,
    'orderby' => 'rand'
    ));
    foreach ($randomPost as $post) {
    wp_redirect(get_permalink($post->ID));
    exit();
    }

    So, can I add something like 'lang => current' in there?

  3. dragoeco
    Member
    Posted 6 months ago #

    Ok, Ive found the solution:
    I just added this line:
    'suppress_filters' => false,
    So, here is the full code:

    $randomPost = get_posts(array(
    'numberposts' => 1,
    'suppress_filters' => false,
    'orderby' => 'rand'
    ));

    Solution comes from here: http://codex.wordpress.org/Template_Tags/get_posts

Reply

You must log in to post.

About this Topic