WordPress.org

Ready to get started?Download WordPress

Forums

Grabbing the latest post from a specific category (1 post)

  1. NialGiac
    Member
    Posted 6 years ago #

    I'm using WordPress to run a webcomic. In one category 'comic', I post the latest comic image. In another 'news', I want to post messages. Within index.php in my theme folder, I have the following code:

    <?php if( is_home() ) query_posts('cat=-22&numberposts=1'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    
    	<div class="storycontent">
                    <?php if($comic=mycomic_is_comic($id)) mycomic_navigator($comic,$id); ?>
    		<?php the_content(__('(more...)')); ?>
                    <?php if($comic=mycomic_is_comic($id)) mycomic_navigator($comic,$id, false); ?>
    	</div>
    
    	<div class="feedback">
    		<?php wp_link_pages(); ?>
    			<?php edit_post_link(__('Edit This - ')); ?><?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
    	</div>
    </div>
    
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    The category with ID 22 is 'news'. This works absolutely fine. The comic image is posted and any posts to news are ignored. Now, in sidebar.php, I want to display the latest post from category 'news'. But no matter what I try, it'll either break the display of the comic in index.php or show the same post from 'news'.

    Here was my latest attempt for displaying the news in the sidebar:

    <?php query_posts('cat=22&numberposts=1'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    
    	<div class="storycontent">
                    <?php if($comic=mycomic_is_comic($id)) mycomic_navigator($comic,$id); ?>
    		<?php the_content(__('(more...)')); ?>
                    <?php if($comic=mycomic_is_comic($id)) mycomic_navigator($comic,$id, false); ?>
    	</div>
    
    	<div class="feedback">
    		<?php wp_link_pages(); ?>
    			<?php edit_post_link(__('Edit This - ')); ?><?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
    	</div>
    </div>
    
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; rewind_posts(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags