WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to short the length text in this code? (7 posts)

  1. Cristescu Bogdan
    Member
    Posted 4 years ago #

    Hi, i have another issue.
    I try to show a random post, not only the link but with some text from article. I made the code, i recive the title, the post text, but it is all the article, and now i whant to use just some words of it...
    The code is:

    function single_random_post_text() {
    		    global $wpdb;
    		    $query = "SELECT id, post_content, post_title, post_name FROM $wpdb->posts WHERE ((post_status='publish') AND (post_type = 'post') AND ($wpdb->posts.post_password = '')) ORDER BY RAND() LIMIT 1";
    		    $randompost = $wpdb->get_results($query);
    		    $post = $randompost[0];
    		    $post_content = "<p>$post->post_content</p>\n";
    		    $post_title = htmlspecialchars(stripslashes($post->post_title));
    		    $title_post .= "<h2><a href=\"" . get_permalink($post->id) . "\" title=\"". $post_title ."\">" . $post_title ."</a></h2>\n";
    		    echo $title_post;
    		    echo $post_content;
    
    }

    Pls help me with that!

  2. nouveller
    Member
    Posted 4 years ago #

    You could use a combination of a regular expression and a for each loop to shorten the $post_content.

  3. Pinoy.ca
    Member
    Posted 4 years ago #

    Use post_excerpt and not post_title.

    Or, if you don't have excerpts, use SUBSTRING http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring .

    I hope this helps!

  4. Cristescu Bogdan
    Member
    Posted 4 years ago #

    I`m not a developer, i just wanna resolve that ... i check all the random post plugins and seams that no one to show the text, only the link of post. When you have a old blog it verry cool for the visitator to stick for they a random post in the top of blog or the sidebar. Some old post did not have visits ...
    Sory for my bad english but this is a good idea...
    Pinoy.ca , i try to change post_title with post_excerpt ... did not work!
    With the substing ... i dont know what is that :)

  5. Pinoy.ca
    Member
    Posted 4 years ago #

    $query = "SELECT id, SUBSTRING(post_content,1,40), post_title, post_name FROM $wpdb->posts WHERE ((post_status='publish') AND (post_type = 'post') AND ($wpdb->posts.post_password = '')) ORDER BY RAND() LIMIT 1"; where 40 is the desired length.

    You will also want to strip_tags(), e.g., $post_content = "<p>".strip_tags($post->post_content)."</p>\n";

    Or, better, you can do all the cutting in PHP, $post_content = snippet(strip_tags($post->post_content), 10); where 10 is number of words. You can copy snippet() from here http://www.php.net/manual/en/function.substr.php#73233 . It's good practice to rename the snippet() function to avoid function name collisions.

    I hope this helps!

  6. Cristescu Bogdan
    Member
    Posted 4 years ago #

    Your point its verry good, but i made my own code, and i put all in a plugin, a new random post plugin.
    The plugin page is http://blog.121416.co.cc/en/wordpress/wordpress-plugin-single-random-post-with-text/

    Thx "Pinoy.ca" for your code.

  7. nouveller
    Member
    Posted 4 years ago #

    Your not a developer but you made your own plugin...

Topic Closed

This topic has been closed to new replies.

About this Topic