Show category posts in sidebar only
-
I have a problem that I can’t seem to solve on my own…
I want posts from one specific category to show up in the right sidebar (say title+excerpt for 3-5 posts). No problems, I just add a few lines to the sidebar.php file. However, I don’t want posts in this category to show up in the regular front page blogroll and this is exactly my problem.
How do I exlude posts from a specific category from the front page, but not from the front page sidebar?
-
suppose specific category id is 5. Write a code above the loop
<?php $tmp = $wp_query; $paged = get_query_var('paged') ? get_query_var('paged') : 1; $wp_query = new WP_Query('cat=-5&posts_per_page=10&paged=$paged'); //here your loop start. don't modify your loop. while(have_posts()): the_post(); //like this ...........some code here ..... // loop is end endwhile; //end of the loop write following line $wp_query = $tmp; ?>
Try this once
Thanks
CHmm, the page won’t even load when I put that code into my index.php file. Am I perhaps doing something wrong?
can you give your front page file?
Sure, this is the index.php file:
<?php get_header(); ?> <div id="content"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1> <p class="postmetadata">I <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php the_time('l, F jS, Y') ?> | <?php comments_popup_link('0 Kommentarer »', '1 Kommentar »', '% Kommentarer »'); ?> | <?php if(function_exists('wp_print')) { print_link(); } ?> | <a href="<?php trackback_url(true); ?> " rel="trackback">Trackback</a></p> <span style="display:none;"><?php todays_overall_count($post->ID, '', '', '', '0'); ?></p></span> <div class="entrytext"> <?php the_content('Forts?¤tt l?¤sa artikeln »'); ?> </div> </div> <?php endwhile; ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('??ldre inslag') ?></div> <div class="alignright"><?php previous_posts_link('Nyare inslag') ?></div> </div> <?php else : ?> <h2 class="center">Vi hittar inte det du s??ker.</h2> <p class="center">Vi hittar ingen matchning till den s??ktermen. Prova n?¥got annat.</p> <?php include (TEMPLATEPATH . "/searchform.php"); ?> <?php endif; ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
ok. Can you give your sidebar.php file?
Sorry for the delay, chinmoy29, I’ve been out of town for a while.
Here’s the sidebar.php file. I have the three most recent posts from category 2 displaying, but for some reason the latest post (no matter what category) is diplayed before the three category 2 posts. Can’t see why!?
But, the main issue is that I want category 2 posts to be displayed in the sidebar only on the main page.
<div id="sidebar1"> <ul> <li> <h2 style="font-color: #000000;"><img src="https://www.xyz.se/images/bulletlarge.jpg"/> Senaste programmen</h2> <?php $recent = new WP_Query(); ?> <?php $recent->query('cat=2&showposts=3'); ?> <?php while($recent->have_posts()) : $recent->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <div style="font-weight: bold"><?php the_title(); ?></div> <?php the_excerpt(); ?> </a> </li> </ul> <?php endwhile; ?> </ul> </li> </ul> <div style="font-weight: bold;"> > <a href="https://www.xyz.se/?cat=2">Till programarkivet</a></div> </div> <div id="sidebar1"> <ul> <h2><img src="https://www.xyz.se/images/bulletlarge.jpg"/> S?k</h2><ul> <div id="search"><?php include (TEMPLATEPATH . '/searchform.php'); ?> </ul> <ul><li> <h2><img src="/images/bulletlarge.jpg"/> Mest l?¤sta</h2> <ul> <?php todays_count_widget('bes?k', 'ul'); ?> </ul> </li> <ul> <li><img src="/images/bulletlarge.jpg"/> <?php src_simple_recent_comments(); ?> </ul> </li> <h2><img src="/images/bulletlarge.jpg"/> Prenumerera</h2> <p><a href="https://feeds.feedburner.com/xyz" rel="alternate" type="application/rss+xml"><img src="https://www.feedburner.com/fb/images/pub/feed-icon16x16.png" alt="" style="vertical-align:middle;border:0"/></a> <a href="https://feeds.feedburner.com/xyz" rel="alternate" type="application/rss+xml"><font color="#000000">PodCast</font></a></p> <p> <li><a href="<?php bloginfo('rss2_url'); ?>"><font color="#000000">RSS - Artiklar</font></a></li> <li><a href="<?php bloginfo('comments_rss2_url'); ?>"><font color="#000000">RSS - Kommentarer</font></a></li> <li> <a href="https://www.feedburner.com/fb/a/emailverifySubmit?feedId=312442&loc=en_US"> <font color="#000000">Radio UPF via Epost</font></a></li> <ul> </div>
Thanks alot for your time and efforts!
<div id="sidebar1"> <?php if(is_home() || is_fornt_page()): ?> <ul> <li> <h2 style="font-color: #000000;"><img src="https://www.xyz.se/images/bulletlarge.jpg"/> Senaste programmen</h2> <?php $recent = new WP_Query(); ?> <?php $recent->query('cat=2&showposts=3'); ?> <?php while($recent->have_posts()) : $recent->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <div style="font-weight: bold"><?php the_title(); ?></div> <?php the_excerpt(); ?> </a> </li> </ul> <?php endwhile; ?> </ul> </li> </ul> <?php endif; ?> <div style="font-weight: bold;"> > <a href="https://www.xyz.se/?cat=2">Till programarkivet</a></div> </div> <div id="sidebar1"> <ul> <h2><img src="https://www.xyz.se/images/bulletlarge.jpg"/> S?k</h2><ul> <div id="search"><?php include (TEMPLATEPATH . '/searchform.php'); ?> </ul> <ul><li> <h2><img src="/images/bulletlarge.jpg"/> Mest l?¤sta</h2> <ul> <?php todays_count_widget('bes?k', 'ul'); ?> </ul> </li> <ul> <li><img src="/images/bulletlarge.jpg"/> <?php src_simple_recent_comments(); ?> </ul> </li> <h2><img src="/images/bulletlarge.jpg"/> Prenumerera</h2> <p><a href="https://feeds.feedburner.com/xyz" rel="alternate" type="application/rss+xml"><img src="https://www.feedburner.com/fb/images/pub/feed-icon16x16.png" alt="" style="vertical-align:middle;border:0"/></a> <a href="https://feeds.feedburner.com/xyz" rel="alternate" type="application/rss+xml"><font color="#000000">PodCast</font></a></p> <p> <li><a href="<?php bloginfo('rss2_url'); ?>"><font color="#000000">RSS - Artiklar</font></a></li> <li><a href="<?php bloginfo('comments_rss2_url'); ?>"><font color="#000000">RSS - Kommentarer</font></a></li> <li> <a href="https://www.feedburner.com/fb/a/emailverifySubmit?feedId=312442&loc=en_US"> <font color="#000000">Radio UPF via Epost</font></a></li> <ul> </div>
Try it once.
Nothing happened with that extra if tag in the sidebar.php. I managed to solve it, though!
By adding the following code after the header call in the index.php file:
<?php if ( is_home() ) { query_posts($query_string . '&cat=-2'); } ?>
And adding this to the sidebar.php file:
<?php query_posts('cat=2&showposts=3'); ?> <?php while (have_posts()) : the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <div style="font-weight: bold"><?php the_title(); ?></div> <?php the_excerpt(); ?> </a> </li> </ul> <?php endwhile; ?>
Category 2 posts are excluded from the home page, but show up in the sidebar.
- The topic ‘Show category posts in sidebar only’ is closed to new replies.