【问题标题】:Get a specific page's excerpt within the loop Wordpress在循环 Wordpress 中获取特定页面的摘录
【发布时间】:2012-07-21 03:16:19
【问题描述】:

我正在尝试创建一个循环,从任何帖子加载随机图像,同时检索特定页面的摘录。我已经完成了随机发布部分,但无法获取它来检索页面摘录......我想我可能需要在它们自己的循环中查询页面,但我不知道该怎么做。我已经安装了获取页面摘录支持等功能,但我认为我在循环中做错了,任何帮助将不胜感激。

<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('blog-post-image'); ?></a>
  <div class="borderimage"></div>
  <div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
  </div>
</div>
<?php endwhile; else : endif; ?>

【问题讨论】:

  • 试试the_excerpt()(不带参数)。另外,你应该把query_posts语句放在if (have_posts())之前
  • 我需要参数,因为我试图从特定页面获取摘录,同时还从随机帖子中绘制图像。
  • 啊。知道了。请参阅下面的答案。

标签: wordpress loops


【解决方案1】:

query_posts 替换了全局 $wp_query,您不想这样做,因为您想为您的页面保留该查询。试试这个...

if (have_posts()){
    while(have_posts()){
        the_post(); //global $post now has the page in it
        $args = array("posts_per_page"=>1,"orderby"=>"rand");
        $random_posts = get_posts($args); //returns an array
        $random_post = $random_posts[0];
        //do your stuff... 
        //$post contains the original page
        //$random_post contains the random post
     }
}

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多