Tuesday, 10 September 2013

Retrieve posts by category name issue in WP Query

Retrieve posts by category name issue in WP Query

So I've created a custom page template for my "Topics" Page.
What I want to do is add in some PHP to the custom page template my Topics
page uses, to retrieve permalinks for the 3 most recent posts from a
chosen category.
E.g.
(From post category 1)
--> Permalink for post 1
--> Permalink for post 2
--> Permalink for post 3
My code so far is as follows:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<ul>
<?php
$category_posts = new WP_Query('cat=consumer-trust&showposts=3');
while ($category_posts ->have_posts()) : $category_posts->the_post();?>
<li>
<a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent
Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
</a></li>
<?php endwhile; ?>
The problem, however, is that changing the cat in the WP_Query doesn't
seem to make any difference. I've tried numbers and category names, and
neither works.
Can anybody advise? This code will appear three times on the intended
page, for three different categories.

No comments:

Post a Comment