WordPress.org

Ready to get started?Download WordPress

Forums

Show posts from only one category (6 posts)

  1. modifiedcontent
    Member
    Posted 6 years ago #

    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>
    
    <div class="info">
    		<? wp_link_pages(); ?>
    		<? comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
        </div>
    
    </div>
    
    <? 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?

  2. Adam Brown
    Member
    Posted 6 years ago #

    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:

    http://codex.wordpress.org/Template_Tags/query_posts

  3. noafilm
    Member
    Posted 6 years ago #

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

    <h2>Important</h2>
    <ul class="dates">
    	<?php
    		// I love WordPress so
    		query_posts('showposts=3');
    		query_posts("cat=36");
    	?>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <li><a>"><span class="date"><?php the_time( $hemingway->date_format() ) ?></span> <?php the_title() ?>
    </a></li>
    	<?php endwhile; endif; ?>
  4. moshu
    Member
    Posted 6 years ago #

    Instead of asking "code questions" - describe WHAT do you want to achieve. We will tell you the HOW.

  5. lstelie
    Member
    Posted 6 years ago #

    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();

  6. lozy
    Member
    Posted 6 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic