echo first 20 words of any page


/ Published in: PHP
Save to your folder(s)

This gets the content from the page with the ID 22 (change as appropriate on line 1) and echos the first 20 words (change word count on line 8) - can be used on any page inside your Wordpress theme.


Copy this code and paste it in your HTML
  1. <?php
  2. $my_id = 22; // change to relevant page ID
  3. $post_id_5369 = get_post($my_id);
  4. $content = $post_id_5369->post_content;
  5. $content = apply_filters('the_content', $content);
  6. $content = str_replace(']]>', ']]>', $content);
  7. $words = explode(' ', $content);
  8. $i = 0;
  9. while ($i <= 20) {
  10. $short .= $words[$i] . ' ';
  11. $i++;
  12. }
  13. echo $short;
  14. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.