Wordpress阵列显示某些类别的帖子并显示帖子摘录和功能

时间:2012-08-26 20:47:44

标签: php arrays wordpress post wordpress-theming

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

有人可以帮忙吗?

加里

2 个答案:

答案 0 :(得分: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;
?>

答案 1 :(得分:1)

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

以下是Post Thumbnails

的一些文档

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

<?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>​
相关问题