【问题标题】:Using the wordpress loop in a random file在随机文件中使用 wordpress 循环
【发布时间】:2010-12-23 05:07:01
【问题描述】:

对于我的首页,我使用的是 FrontPageSlideshow,它会生成一个 html/css/javascript 元素来显示文章和其他内容。目前,它从文件中提取静态条目。我能够为我工作的 Drupal 网站使其动态化,但现在我被困在 Wordpress 网站上。

在文件中,我尝试在我的 Wordpress 主题中包含一个 php 文件。

    <?php

$show = new WP_Query();

$show = query_posts(array(
     'category_name'      => 'News', 
     'posts_per_page'     => 4, 
     'orderby'       => 'date', 
     'order'         => 'DESC',
    ));

if (have_posts()) :
 while (have_posts()) : the_post() :
  $recentStories['article'.$articleNumber] = array(
   id      => $post->ID,
   title   => wp_title(),
   summary_name  => $post->post_name,
   dateMade=> the_date(),
   image   => get_post_meta($post->ID, 'fp_image', true),
   summary => get_excerpt()
   );
  $articleNumber++;
 endwhile;
endif;

?>

但问题是它似乎无法返回我的任何 Wordpress 帖子。该文件包含在另一个文件中,类似于

$articleNumber = 1;
$recentStories = array();

include("wp-content/themes/Website/frontpage-post.php");

$slides = array();

// --- Start slide list ---
for ( $counter = 1; $counter <= 4; $counter++) {
 // slide elements
 array_push($slides, array(
  'slidelink'  => 'http://IPHERE/website/?p=' . $recentStories['article'.$counter]['id'],
  'title'      => $recentStories['article'.$counter]['title'],
  'category'   => 'News',
  'tagline'    => $recentStories['article'.$counter]['summary'],
  'text'       => $recentStories['article'.$counter]['summary_name'],
  'slideimage' => $recentStories['article'.$counter]['image'],
  ) 
 );
};

但是,我的循环不断返回一个空数组。我想知道是否可以在 index.php 页面之外使用 Wordpress 循环。

【问题讨论】:

  • 假设这两个文件都在活动主题的文件夹中,您是否尝试过将包含切换到..include("frontpage-post.php");
  • 在这里第二次 t31os - 那条路绝对行不通。如果你在它前面加上一个正斜杠,它看起来会相对于站点的根目录,但如果那是你使用的确切路径,它将不起作用。您是否尝试过启用 wp_debug 来检查 PHP 错误?另外,在您的 $recentStories 变量上尝试 print_r()

标签: php wordpress


【解决方案1】:

对于在 wordpress 系统之外访问的所有 wordpress,您必须包含 wp-load.php

include("wp-load.php");
include("wp-content/themes/Website/frontpage-post.php");

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2014-02-14
    • 2018-03-18
    相关资源
    最近更新 更多