Support » Fixing WordPress » Show posts from only one category

  • Is there a way to modify the code below to get only posts from one category?

    <? if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<? the_ID(); ?>">
    <h3 class="storytitle"><a href="<? the_permalink() ?>" rel="bookmark"><? the_title(); ?></a></h3>
    <div class="info">Posted by <? the_author() ?> on <? the_time('F') ?> <? the_time('j') ?> under <? the_category(',') ?> <? edit_post_link(__('Edit This')); ?></div>
    <div class="storycontent">
            <? the_content(__('Read the rest of this entry &raquo;')); ?>
    <div class="info">
    		<? wp_link_pages(); ?>
    		<? comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
    <? comments_template(); // Get wp-comments.php template ?>
    <? endwhile; ?>
    <div class="alignleft"><p><? next_posts_link('&laquo; Previous Entries') ?></p></div>
    <div class="alignright"><p><? previous_posts_link('Next Entries &raquo;') ?></p></div>
    <? else : ?>
    <p><? _e('Sorry, no posts matched your criteria.'); ?></p>
    <? endif; ?>

    I know there are several forum posts about this, but they’re confusing. Some talk about “excluding categories” as a solution, which is cumbersome if you have many categories and often add new ones.

    There are several plugins, but they don’t seem very stable judging from the comments.

    I use the code above on an index.php page outside WordPress. I still have trouble wrapping my head around The Loop.

    The code above somehow gets the posts from the database, right? So it should be possible to just get the posts from a certain category, right?

Viewing 5 replies - 1 through 5 (of 5 total)
  • That’s not the piece of code to modify. You need to modify the code immediately before what you pasted in. Take a look at this. Whether it makes sense depends on your level of experience, but it’s a start:

    This is what I did for my “viki.eyeball” theme and that seems to work:
    I pasted following code into my about page:

    <ul class="dates">
    		// I love WordPress so
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li><a>"><span class="date"><?php the_time( $hemingway->date_format() ) ?></span> <?php the_title() ?>
    	<?php endwhile; endif; ?>

    Instead of asking “code questions” – describe WHAT do you want to achieve. We will tell you the HOW.



    Instead of asking “code questions” – describe WHAT do you want to achieve. We will tell you the HOW.
    I habe the same question taht seems asked from time to time whuth no clear simple answer

    In fact in some cas we need to have on the home page (index.php) only posts form a specific category

    In an ideal world the solution would be to use

    while (have_posts($category_id)) : the_post();

    Has anyone found a solution to this at all please?

    I’m also looking to find out how I can only show related posts when a post that was posted in a certain category.

    ie, if I had a post about “home loans” and it was placed in a category of “loans”, then I was hoping to find some code that would allow me to only show those posts when that “home loans” post was clicked on.

    One solution a person kindly gave me was to mention the category name, but then I am limited to like if a person were to click on another post on my blog about “cheap insurance” that was posted in a category of “Insurance” then it wont show any related posts that were posted in the category of “insurance”

    So is there any way to say like, in coding terms of logic:

    if posts-posted-in-category = category-name, show posts, show total of related posts

    Does the
    while (have_posts($category_id)) : the_post();

    actually do this, and how would the entire loop look like?

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show posts from only one category’ is closed to new replies.