• Hi

    I have the following code

    <?php
    		$categories = get_categories('hide_empty=1');
    		foreach ($categories as $category) :
    		query_posts('showposts=1&amp;cat='.$category->cat_ID);
    		if (have_posts()) : the_post();
    		?>

    wich displays lates post from all categories on home page

    i want do display all of posts from only one category on home page

    What do i need to change here?

    category id is 9 and category name is Novosti

    Thank you alot ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • You need a query.

    query_posts('showposts=10&amp;cat=9');
    if(have_posts()) : ...

    that’ll show the last 10 pots from category 9.

    problem is, your pagination will be broken if you’re using “pretty meralinks”. I actually found a plugin that works fairly well to resolve the issue – you might give it a shot. For some reason, it works.

    Thread Starter marijan_e

    (@marijan_e)

    So if i undestod you well

    code should look like this

    $categories = get_categories('hide_empty=1');
    		foreach ($categories as $category) :
    		query_posts('showposts=10&amp;cat=9');
    if(have_posts()) : the_post();
    		?>

    Right?

    anyway i done it like this and, what i get is that last post is shown repeatedly, because main index is coded to display latest post from all categories,

    No…

    the query is for the Loop. If you’re just trying to display category 9 on your index page, then you just need to place the query before the loop. forget all that $category and foreach stuff, you don’t need it.

    Thread Starter marijan_e

    (@marijan_e)

    I really do not know PHP

    Here is whole code for index page

    <?php get_header(); ?>
    
    	<!-- BEGIN content -->
    	<div id="content">
    
    		<?php
    		$categories = get_categories('hide_empty=1');
    		foreach ($categories as $category) :
    		query_posts('showposts=1&amp;cat='.$category->cat_ID);
    		if (have_posts()) : the_post();
    		?>
    
    		<!-- begin post -->
    		<div class="post">
    
    		<div class="thumb"><a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a></div>
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		<p><?php echo dp_clean($post->post_content, 300); ?></p>
    		<a href="<?php the_permalink(); ?>" class="readmore">Read More</a>
    		</div>
    		<!-- end post -->
    
    		<?php endif; endforeach; ?>
    
    	</div>
    	<!-- END content -->
    
    <?php get_sidebar(); get_footer(); ?>

    Can you please update the code for me ?

    I already did – I’ll repeat myself again:

    <?php query_posts('showposts=1&amp;cat=9');
    if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    your formatting
    
    <?pgp endwhile; endif; ?>

    I can’t really get any clearer than that.

    Thread Starter marijan_e

    (@marijan_e)

    Well i edited now like this

    <?php get_header(); ?>
    
    	<!-- BEGIN content -->
    	<div id="content">
    
    		<?php query_posts('showposts=1&amp;cat=9');
    if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    		<!-- begin post -->
    		<div class="post">
    
    		<div class="thumb"><a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a></div>
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		<p><?php echo dp_clean($post->post_content, 300); ?></p>
    		<a href="<?php the_permalink(); ?>" class="readmore">Read More</a>
    		</div>
    		<!-- end post -->
    
    		<?pgp endwhile; endif; ?>
    
    	</div>
    	<!-- END content -->
    
    <?php get_sidebar(); get_footer(); ?>

    and i get

    Parse error: syntax error, unexpected T_ENDWHILE in /home/www/web4437/html/wp-content/themes/meganews/index.php on line 19

    it’s the &amp; – the forums must have converted my & to the “safe” character. It’s just supposed to be &, not “&amp;

    Thread Starter marijan_e

    (@marijan_e)

    Ok we can now say it works but, it only diplay the latest post from choosen category

    Again, if you want to change how many posts are shown, change the “showposts” part.

    This is helpful… but i’m still having some issues.

    <?php query_posts('showposts=1&;cat=679');
    	if (have_posts()) : the_post();	?>
    
    	<!-- begin post -->
    	<div class="post">
    		<h2><a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?></a></h2>
    		<p class="date"><?php the_author_posts_link(); ?> on <?php the_time('m j, Y') ?> | <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>
    		<div class="thumb"><a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'medium', 'alt="' . $post->post_title . '"'); ?></a></div>
    		<p><?php echo dp_clean($post->post_content, 200); ?></p>
    		<a class="readmore" href="<?php the_permalink(); ?>">Read More</a>
    	</div>
    	<!-- end post -->
    
     <?pgp endwhile; endif; ?>

    Any thoughts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display posts from only one category not from all categoris’ is closed to new replies.