在WordPress安装外部运行循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在WordPress安装外部运行循环相关的知识,希望对你有一定的参考价值。

Put the following outside WP
  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>

以上是关于在WordPress安装外部运行循环的主要内容,如果未能解决你的问题,请参考以下文章

在wordpress之外运行循环

PHP WordPress从循环外部检索自定义字段

WordPress从循环外部检索自定义字段

markdown 在WordPress中使用jQuery代码片段

WordPress - 代码片段插件

Wordpress - 将代码片段包含到布局的选定部分的插件