• I have a external php page that grabs my wordpress blog content. I would like to know if there is a way to truncate the blog content to 30 words?

    Here is my php code that works (but does not truncate content):
    <?php
    /* PHP to pull and publish wordpress 2 post data on an external page NO CACHE*/
    define(‘WP_USE_THEMES’, false);
    require(‘blog/wp-blog-header.php’);

    $how_many=1; //How many posts do you want to show
    require_once(“blog/wp-config.php”);
    // strip out html characters to allow for truncating
    $strippedDesc = strip_tags($desc);
    // truncate post content to 12 words
    $numwords = 12;
    preg_match(“/([\S]+\s*){0,$numwords}/”, $strippedDesc, $regs);
    $shortDesc = trim($regs[0]);
    // Change this for your path to wp-config.php file

    $news=$wpdb->get_results(“SELECT ID,post_title,
    post_content FROM $wpdb->posts
    WHERE post_status= \”publish\”
    ORDER BY ‘ID’ DESC LIMIT “.$how_many);
    foreach($news as $np){

    //NOTE: get other post data by selecting different
    //data in the posts table…

    $permalink=get_permalink($np->ID);

    $html= “<p><a href=\””. $permalink .
    “\”>$np->post_title

    $np->post_content\n</p>”;

    //NOTE: you can easily alter the HTML above to suit

    echo $html;

    }
    ?>

  • The topic ‘Truncate the blog “post_content” to 30 words?’ is closed to new replies.