【问题标题】:Wordpress array to show post from certain category and show post excerpt and feature ingWordpress 数组显示特定类别的帖子并显示帖子摘录和特色
【发布时间】:2012-08-21 10:58:26
【问题描述】:

大家好,正在寻找 Wordpress 帮助。我需要放置一个简单的查询/数组来显示来自某只猫的帖子,例如“新闻”,其中将包含帖子特色图片。

有人可以帮忙吗?

加里

【问题讨论】:

  • 只是一个简单的...
  • 但我想包含帖子功能 img 作为拇指不知道如何措辞 PHP..
  • 请不要将不相关的新问题添加到现有问题中。谢谢。

标签: php arrays wordpress post wordpress-theming


【解决方案1】:

试试这个

<?php
    $query = new WP_Query('category_name=News&posts_per_page=4');
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
    if (has_post_thumbnail()) {
        ?>
            <a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
        <?php
    }
    ?>
    <h2><a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php
    the_excerpt(); // or the_content(); for full post content
    endwhile;endif;
?>

【讨论】:

  • 效果很好。但是我将如何保留它,以便每个帖子都向滑块添加分页,因为目前没有滑动动作,只是一个简单的帖子列表。
  • 不确定滑块是什么意思。
  • 对不起。请参阅garyrevell.co.uk/student-i,如果您查看“最新消息”的位置,我输入了一个简单的 html 内容滑块。一旦网站进入 WP,我希望滚动/滑动浏览 5 个最新的新闻帖子。这有意义吗?
  • 为此您需要使用 javascript(可能是 jQuery 插件)。
  • 我知道,我有一个。不知道如何将其集成到 wordpress 中。例如,上面的代码显示了 cat "news" 中的所有帖子,但我不确定如何配置 php,以便滑块标记保持不变。
【解决方案2】:

不要使用 query_posts()。其目的是修改默认的 Wordpress 循环,不应用于一般查询。请改用WP QueryGet Posts

这里有一些关于Post Thumbnails的文档

这是一个基于您向我展示的可能有效的小示例。请注意,“showposts”已更改为“posts_per_page”,因为 2.1 版已弃用“showposts”:

<?php
$q = new WP_Query(array('cat'=>8, 'posts_per_page'=>4));
if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
    the_excerpt();
    if(has_post_thumbnail())
        the_post_thumbnail('thumbnail');
endwhile;endif;
?>

更新:

根据您给我的示例,这应该可以帮助您入门:

<div id="slider2">
<div class="viewport">
    <?php
    $q = new WP_Query(array('cat'=>8, 'posts_per_page'=>4));
    if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
    ?>
    <div class="newsPost">
        <div class="news-date"><?php the_date(); ?></div>
        <div class="newstitle"><?php the_title(); ?></div>
        <div class="news-des"><?php the_excerpt(); ?></div>
        <?php if(has_post_thumbnail()){ ?>
        <div class="newsimg"><?php the_post_thumbnail(); ?></div>
        <?php } ?>
        <p><a href="<?php the_permalink(); ?>">Read More...</a></p>
    </div>
    <?php endwhile;endif; ?>   
</div>
</div>​

【讨论】:

  • 非常感谢...在我的网站上,所有新闻帖子都将显示在主页上的迷你滑块中。我可以拆分上面的代码吗?例如 '
    8, 'posts_per_page'=>4)); if($q->have_posts()) : 而($q->have_posts()) : $q->the_post(); the_excerpt(); (即将关闭)
    ' - 对于帖子摘录,然后对于拇指/功能 img
    if(has_post_thumbnail()) the_post_thumbnail('thumbnail'); endwhile;endif; ?>
  • 当然,这样的事情是可以做到的。但我个人会在循环中保留每个相应帖子的开始和结束标签。您还可以在循环之前打开一个“包装器”div,并在之后立即关闭,以包含所有帖子 div。
  • 好吧。这是带有虚拟内容的滑块的 HTML。
  • 用你的例子更新你的问题。我可以更新我的答案来告诉你我的意思。
  • 这是我的 html 中的 sn-p,显示了我的迷你滑块的第一张幻灯片,您能建议将适当的 PHP 放在哪里吗? jsfiddle.net/Gya2u我的主页设计-> garyrevell.co.uk/student-i/student-i-home-960px.jpg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
相关资源
最近更新 更多