开发者

Wordpress: show category posts

开发者 https://www.devze.com 2022-12-20 16:31 出处:网络
I have 3 boxes that i have at the bottom of my home page, i want to display in each of them a wordpress post.

I have 3 boxes that i have at the bottom of my home page, i want to display in each of them a wordpress post.

Each of these posts will be under a category special1, special2, special3, how do i do this?

I have tried

<div class="special_box">
        <?php query_posts('tag=special3');?>
    </div>

But this does not work

Any ideas? I tried this, but it replaced all of my other content with just the post, which is not what i want

This is outside the wordpress loop:

<?php 

$special1 = query_posts('category_name=special1'); ?>

This is inside:

<div class="special_box">
        <?php echo $special1 ;?开发者_如何学编程>
    </div>


This new query will show the latest post from a category and can be used more than once on a page/post (with php excecution enabled) and within the standard WP loop:

<?php $my_query = new WP_Query('category_name=special1&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a><br /><?php the_content(); ?><?php endwhile; ?>

Function Reference/WP Query « WordPress Codex


An alternative would be using the category number (doesn't matter). But This code is easier:

<?php wp_reset_query(); ?>

<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; endif; ?>

http://snippetcentral.com/query-posts-category/

0

精彩评论

暂无评论...
验证码 换一张
取 消