Wordpress array to show post from certain category

2019-03-07 03:39发布

Hi all looking for a Wordpress help. I need to place a simple query/array to display posts from a certain cat e.g "News' that will include the posts featured image.

Can anyone please help?

Gary

2条回答
ゆ 、 Hurt°
2楼-- · 2019-03-07 04:23

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>​
查看更多
The star\"
3楼-- · 2019-03-07 04:33

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;
?>
查看更多
登录 后发表回答