Support » Plugins » How to get 1 random line out of all my posts?

  • How can I call one random line out of all my posts? I’ve looked at the Random Quote plugins and wasn’t sure if any would do this.

    I want to do like what the default “Hello Dolly Plugin” does, except, I want it to randomly choose 1 line to quote out of all my posts (which are songs anyway), instead of choosing 1 line out of the finite lines of 1 song defined in the plugin txt. Any ideas or code to replace in Hello Dolly?

Viewing 7 replies - 1 through 7 (of 7 total)
  • As long as these random lines are, well, actual lines (ending with a line break), this should do it for you:

    Random Line from Posts:
    download plugin | view source

    Download the file and upload it to your plugins directory, then activate Random Line from Posts under Plugins. Then use this *template tag* in your template(s) where you want to display the line:

    <?php szub_random_line(); ?>

    Optional parameters you can pass to the szub_random_line() function:

    ‘link’ (boolean) = Display line as a link to its post (1) or as plain text (0). Default is 0.

    ‘apply_filters’ (boolean) = Apply WordPress (and plugin) text formatting ‘filters’ to the line (1) or apply no formatting (0). Default is 1.

    ‘strip_tags’ (boolean) = Remove HTML from the line (1) or leave it as is (0). Default is 1 and is recommended unless you really know what you’re doing. Turning this off can also be problematic with ‘link’ set to 1.

    ‘echo’ (boolean) = Display the line (1) or just return it for use in ones code (0). Default is 1.

    Parameters are passed in query-string format:

    <?php szub_random_line('link=1&apply_filter=0&strip_tags=1&echo=1'); ?>

    Personal note: I kinda like this little plugin… :)

    Kaf, did you just write that for me? Once again, thanks a million for your solutions. I am going to try this tonight. Will post back here if it works.

    My “lines” are actual lines with a line-break, assuming that WordPress is putting an actual line-break in my posts when I hit the Enter button.

    Well, not just you Dgold. I do this for me too!

    By the way, a little ‘oops’ slipped in that I didn’t notice until I posted above. So I’d suggest grabbing 0.2 of the plugin (changed in links above).

    And yes, WordPress preserves line breaks in the post content. But one can never be too sure how someone is putting WordPress to use!

    This plugin is delightful.

    Anyone can quote the great Jazz Quotes (*cough*Matt), but who can quote their own posts now!! The Quoteable You awaits.

    Actually in the situation I’m using it in, my Posts are all Songs anyway, so the plugin generates random song quotes, on that blog. It’s great. Thanks again Kaf. Just what I was looking for.

    By the way, I applied the “IF” trick, which I’ve learned is good practice:
    <?php if (function_exists('szub_random_line')) { szub_random_line('link=1&apply_filter=0&strip_tags=1&echo=1'); } ?>

    Now, my questions.
    1. I don’t know what the Filtering option does, and Filtering doesn’t turn up much in the Codex. Is this removing apostrophes and ampersands and such?

    2. The output acts like it is enclosed in a paragraph tag, I want to display it inline. Example,

    <ul><li><strong>QUOTE:</strong><?php if (function_exists('szub_random_line')) { szub_random_line('link=1&apply_filter=0&strip_tags=1&echo=1'); } ?></li></li></ul>

    I want result:
    QUOTE: "Here is a one-liner quote from my posts"

    But I am getting:
    "Here is a one-liner quote from my posts"

    Any ideas?

    3. Finally, Kaf, just to prod you to do your best work, can you make it show the title=”Post Title” in the tooltip when the quote is a link? (Apply the title attribute of the hyperlink)

    1. ‘apply_filters’ applies to the line all text formatting functions WordPress and your various plugins may perform on post content, such as placing separate lines into paragraph elements, changing straight quotes to curly, converting smileys to images, and so on.

    2. For version 0.3 I’ll alter ‘apply_filters’ to provide more of an enumerative argument where you can select any or all filters *not* to apply (such as wpautop, which is the filter sticking the paragraph tags in).

    3. Easy peasy to add in 0.3.

    I’ll post another reply when the update is online.

    kaf if you’re updating anyway
    Could you put the ReadMe inside the Plugin commented-out at the top? Else, zip file with ReadMe.txt (for the arguments and how to call it). Not complaining, just suggesting.

    About those “Filters” — I wonder if the AutoLinks plugin is using a filter. Just curious cuz it generates Links at the time of display (that aren’t actually HTML links in the post), and I could image a conflict of link-within-a-link.

    Also, maybe it’s just me, but apply_filter=1 or apply_filter=0 doesn’t seem to change anything in the output I’m getting.

    This plugin is working great, by the way, my site readers will dig it.

    No, didn’t forget 0.3. Had lots on my plate this week (including that other cool plugin! I hate it when they come in a rush). But the delay is really due to figuring out how to handle text filtering and if I should be overly concerned on potential conflicts with other plugins. I chose to keep things as simple as possible. I mean, it displays one line!

    Now trying to decide if its R1 ready and should go up on my site.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get 1 random line out of all my posts?’ is closed to new replies.