Need help with a mini-loop and categories….
-
I am trying to list recent posts in a single category on a couple of static php pages on my site.
Linked below is the 1st page I am working on. The list appears under “Recent Georgia Bulldogs Basketball Articles.”
http://www.sicemdawgs.com/basketball/bk.php
I want to list only posts in the category Basketball. Can someone help me modify the code to display only this category? Code is pasted below (I got it from WordPress.org):
<?php $how_many=5; //How many posts do you want to show require_once('../blog/wp-config.php'); // Change this for your path to wp-config.php file ?> <ul class="blinks"> <? $news=$wpdb->get_results("SELECT <code>ID</code>,<code>post_title</code> FROM $wpdb->posts WHERE <code>post_type</code>=\"post\" AND <code>post_status</code>=\"publish\" ORDER BY post_date DESC LIMIT $how_many"); foreach($news as $np){ printf ("<li><a href=\"%s\">%s</a></li>", get_permalink($np->ID),$np->post_title); } ?> </ul>[moderated–bump removed. Please refrain from bumping as per Forum Rules]
-
What about instead of the get_results loop:
<?php $posts=get_posts('cat=20&showosts=5') foreach(posts as post) { setup_postdata($post); ?> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <?php } ?>Can you check that code? I am getting the following error with it:
Parse error: syntax error, unexpected T_FOREACH in /home/sicem0/public_html/basketball/bk.php on line 67
Sorry, that’s what I get for just throwing it out there without testing…
<?php $posts=get_posts('cat=1&showposts=5'); if ($posts) { foreach($posts as $post) { setup_postdata($post); ?> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <?php } } ?>That code works. However, when I put basketball in for the category, I still get the last 5 posts overall, not basketball only posts. Any suggestions?
Thanks for your help on this.
Got it, I needed the category ID instead of the name.
Last question, I want to use an unordered list instead of the h2 code you gave me. How can I remove the spacing between the results? In the first example I posted, that did not happen. Thanks.
not testing this but I’d say:
replace
?> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>with
printf ("<li><a href=\"%s\">%s</a></li>", get_permalink($post->ID),$post->post_title); ?>That worked! Thanks a lot!
The topic ‘Need help with a mini-loop and categories….’ is closed to new replies.