Run a loop outside WordPress installation


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

Put the following outside WP


Copy this code and paste it in your HTML
  1. <?php
  2. // Include WordPress
  3. define('WP_USE_THEMES', false);
  4. require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
  5. query_posts('showposts=1');
  6. ?>
  7.  
  8. <?php while (have_posts()): the_post(); ?>
  9. <h2><?php the_title(); ?></h2>
  10. <?php the_excerpt(); ?>
  11. <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
  12. <?php endwhile; ?>
  13.  
  14.  
  15.  
  16.  
  17. To truncate excerpt, put this inside functions.php, inside WP theme:
  18.  
  19. <?php
  20.  
  21. function limit_words($string, $word_limit) {
  22.  
  23. // creates an array of words from $string (this will be our excerpt)
  24. // explode divides the excerpt up by using a space character
  25.  
  26. $words = explode(' ', $string);
  27.  
  28. // this next bit chops the $words array and sticks it back together
  29. // starting at the first word '0' and ending at the $word_limit
  30. // the $word_limit which is passed in the function will be the number
  31. // of words we want to use
  32. // implode glues the chopped up array back together using a space character
  33.  
  34. return implode(' ', array_slice($words, 0, $word_limit));
  35.  
  36. }
  37.  
  38. ?>
  39.  
  40. Then, replace <?php the_excerpt(); ?> with:
  41.  
  42. <?php echo limit_words(get_the_excerpt(), '10'); ?>
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. Alternatively: http://www.webdesigncreare.co.uk/blog/videos/recent-posts-outside-wordpress.html
  51.  
  52. <ul>
  53. <?php require($_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php'); query_posts('showposts=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>
  54. <li>
  55. <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
  56. <span>Posted on <?php the_time('l jS F, Y') ?></span><br />
  57. <?php the_excerpt(); ?>
  58. </li>
  59. <?php endwhile; else: echo "no posts"; endif; ?>
  60. <?php wp_reset_query(); ?>
  61. </ul>

URL: http://css-tricks.com/snippets/wordpress/run-a-loop-outside-of-wordpress/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.