Skip to:
Content
Pages
Categories
Search
Top
Bottom

Random Topic Link


  • joejozwowski
    Participant

    @joejozwowski

    I can’t seem to find this anywhere, so I’m hoping someone can answer this for me. I have a blog that has a RANDOM POST button that was built in with the theme. But I’m looking to have a RANDOM FORUM TOPIC button instead.

    Does anyone know if a plugin like this exists?

    Once again… I’m looking for a link or a function I could put in my menu that would link to a random forum topic.

Viewing 20 replies - 1 through 20 (of 20 total)

  • Robin W
    Moderator

    @robin-w

    It’s doable, but would be very surprised if anyone has built this !


    Robkk
    Moderator

    @robkk

    @robin-w, @joejozwowski

    i found the code to create a random post link that you can use as a reference , when creating a random topic link

    got it from http://cazue.com/articles/wordpress-create-a-random-post-button-2013

    add_action('init','random_post');
    function random_post() {
           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;
           }
    }

    link to show

    <a href="/?random=1">Random Post</a>

    creating a menu link , idk how to bring that link up in the menu


    Robin W
    Moderator

    @robin-w

    @robkk thanks for that, so to make this a topic I think you you would just change

    $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
    

    to

    $posts = get_posts('post_type=bbp_get_topic_post_type()&orderby=rand&numberposts=1');

    although could be

    $posts = get_posts('post_type="bbp_get_topic_post_type()"&orderby=rand&numberposts=1');
    
    

    never sure of some of these without playing

    idk how to bring that link up in the menu

    For a ‘button’ (actually it would just be a link) you just paste it in text mode onto whtever page. in php, you just echo it

    echo '<a href="/?random=1">Random Post</a>' ;
    

    on a menu item, you just at it as a link dashboard>appearance>menus and look for links


    joejozwowski
    Participant

    @joejozwowski

    Sweet… do I could just put the: “/?random=1” in the URL field of a menu item and BLAM
    Fantastic.

    Say I wanted to pull from two specific forum topics, how would I incorporate that?


    Robkk
    Moderator

    @robkk

    Sweet… do I could just put the: “/?random=1″ in the URL field of a menu item and BLAM
    Fantastic.

    try it??

    if not try the other ways robin said.


    Robin W
    Moderator

    @robin-w

    Say I wanted to pull from two specific forum topics, how would I incorporate that?

    In your menu? Come back and I’ll help.


    joejozwowski
    Participant

    @joejozwowski

    ok… so I pasted the following into my functions.php page:

    add_action('init','random_post');
    function random_post() {
           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;
           }
    }

    And then in my menu I created a LINK element and pasted this into the URL field:

    /?random=1

    And all I get is a blank page


    joejozwowski
    Participant

    @joejozwowski

    BTW.. I tried both versions of the $posts = get_posts() calls.


    joejozwowski
    Participant

    @joejozwowski

    HEY!

    I just realized that you are calling $wp.

    If the prefix for my database was changed, would this have anything to do with why it is not working?


    Robin W
    Moderator

    @robin-w

    ok, need to work out if menu issue or function issue

    So eliminate function by creating a test page and putting

    echo '<a href="/?random=1">Random Post</a>' ;
    

    onto the test page and confirm it works.


    joejozwowski
    Participant

    @joejozwowski

    could I put that in a page or post, rather than the code?


    Robin W
    Moderator

    @robin-w

    Absolutely you can, once in the edit screen of the page, switch to ‘text’ from ‘visual’ (top right of the content box), and paste

    echo '<a href="/?random=1">Random Post</a>' ;
    

    in there


    joejozwowski
    Participant

    @joejozwowski

    Do I need to remove the functions in the function.php page? Because all I am getting is a blank white page.


    Robkk
    Moderator

    @robkk

    what is the theme you are using??

    are you using wordpress seo or a cache plugin like w3 total cache??


    Robkk
    Moderator

    @robkk

    @joejozwowski @robin-w i think stephen already cracked this

    i was browsing on how to make custom views and yeah stumbled upon this.

    there is a random topic view in his code.

    https://gist.github.com/ntwb/8167699


    Stephen Edgar
    Keymaster

    @netweb

    Ha! Yes, I was messing about with that yesterday thinking it would solve the issue here but it does not 😉 If you add the bbPress views widget to a sidebar you will see all those views in the gist you linked above, you can also add the shortcode [bbp-single-view id="single-random-topic"] to a page it it will display the ‘topic’ list template with a single random topic.

    So it can do all that ^^^, but it will not work as a ‘button’ so you can’t have a button with ‘Click this for a random topic’ 🙁

    Something like this should work though:

    https://gist.github.com/ntwb/ca87b9b75a42215b2889

    
    function ntwb_bbpress_random_single_topic() {
        if ( bbp_has_topics( array( 'orderby' => 'rand', 'posts_per_page' => 1 ) ) ) {
     
            while ( bbp_topics() ) : bbp_the_topic();
     
                ?>
                <a class="bbp-topic-permalink" href="<?php bbp_topic_permalink(); ?>"><?php bbp_topic_title(); ?></a>
                <?php
     
            endwhile;
        }
    }
    // Hook into action
    add_action('bbp_template_before_lead_topic','ntwb_bbpress_random_single_topic');
    // Add it as a shortcode [ntwb-bbpress-random-single-topic]
    add_shortcode('ntwb-bbpress-random-single-topic', 'ntwb_bbpress_random_single_topic');
    

    I’m hooking it into just before the ‘lead topic’ (bbp_template_before_lead_topic) in the example above but you could hook that anywhere you would like.

    I also add it as a shortcode [ntwb-bbpress-random-single-topic] for use that way.

    Make any changes you want to the HTML, I am just doing it as a HTML a anchor link with the permalink bbp_topic_permalink() and the link title as the topic title bbp_topic_title(). Change all that HTML code to a button, remove <?php bbp_topic_title(); ?> and replace with Random Topic or whatever takes your fancy.


    joejozwowski
    Participant

    @joejozwowski

    Stephen, Now, Obviously, I would plunk that function in my functions.php page, but the two main things that I would like to specify is:

    • I need to put this link in a menu
    • I would like to specify particular forums the random would link to.
    • Is this possible with what you have supplied?


    Stephen Edgar
    Keymaster

    @netweb

    Let me think about the menu solution for a bit….

    e.g. To include forum ID’s 2, 5, 12 and 14 add the post_parent__in

    
    if ( bbp_has_topics( array( 'orderby' => 'rand', 'posts_per_page' => 1, 'post_parent__in' => array( 3, 5, 12, 14 ) ) ) ) {

    e.g. To exclude forum ID’s 7, 11, 19 and 21add the post_parent__not_in

    
    if ( bbp_has_topics( array( 'orderby' => 'rand', 'posts_per_page' => 1, 'post_parent__not_in' => array( 7, 9, 11, 19 ) ) ) ) {

    johnskennedy
    Participant

    @johnskennedy

    I have used Stephen’s code above and it works perfectly.

    However, I have 8 forums on my site so if I wanted to add a random button for each separate forum that only selects topics from that forum would I have to create 8 different function ntwb_bbpress_random_single_topic() in my .php and include only a single forum ID every time or is there a better way to implement this? Also, how and where would I add the shortcodes so that they only show on the relevant forum?

    @ johnskennedy You’re spot on, creating 8 functions is one way to do that, you’d need to make sure that each function name is unique.

    That said, the above is one way, another way is to use some conditional logic with some if/else statements which is a little trickier, possibly more fun though to update the function in this way even 😉

    See these codex docs for sme very initial hints:
    https://codex.wordpress.org/Conditional_Tags
    https://codex.bbpress.org/bbpress-conditional-tags/

Viewing 20 replies - 1 through 20 (of 20 total)
  • You must be logged in to reply to this topic.
Skip to toolbar