Wordpress array to show post from certain category and show post excerpt and feature ing

十年热恋 提交于 2019-12-02 13:36:13

Try this

<?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;
?>

Don't use query_posts(). Its intention is to modify the default Wordpress Loop, and should not be used for general queries. Use WP Query or Get Posts instead.

Here's some documentation on Post Thumbnails

Here's a small example based on what you showed me that might work. Notice that 'showposts' has been changed to 'posts_per_page', as 'showposts' was deprecated as of version 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;
?>

UPDATE:

Based on the example you gave me, this should get you started:

<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>​
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!