Support » Themes and Templates » News page with categories as filters

News page with categories as filters

  • Im setting up a site using wordpress.

    Im using pages to display certain content, so i have a news section which will drag posts from the “news” category. Posts are also categorised into child categories as follows:

    – Print
    – Web
    – General

    Id like to be able to filter news by these categories but cant get it to work.

    This is the code im using to display news posts

    Template Name: News
    <?php get_header(); ?>
    <div id="header" class="news col last span-all">
    	<?php query_posts('pagename=news'); ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<h3><?php $values = get_post_custom_values("intro-header"); echo $values[0]; ?></h3>
    		<?php if ( get_post_meta($post->ID, 'header-image', true) ) { ?>
    			<img src="<?php $values = get_post_custom_values("header-image"); echo $values[0]; ?>" />
    		<?php } ?>
    	<?php endwhile; else: ?>
    		<?php endif; ?>
    <hr />
    	<div id="content" class="col right span-8 last">
    		$style_classes = array('col','col', 'col', 'last');
    		$styles_count = count($style_classes);
    		$style_index = 0;
    		<?php query_posts('category_name=news'); ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    				<div class="post-small span-2 left <?php echo $style_classes[$style_index++ % $styles_count]; ?>">
     					<!-- display the thumbnail for the first image -->
    					<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    					$attachments = get_children( array(
    						'post_parent'    => get_the_ID(),
    						'post_type'      => 'attachment',
    						'numberposts'    => 1, // show all -1
    						'post_status'    => 'inherit',
    						'post_mime_type' => 'image',
    						'order'          => 'ASC',
    						'orderby'        => 'menu_order ASC'
    					) );
    					foreach ( $attachments as $attachment_id => $attachment ) {
    					echo wp_get_attachment_image( $attachment_id );
    					} ?>
    						<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    						<?php the_excerpt(); ?>
    						<a href="<?php the_permalink(); ?>" class="read-more" title="Read More"> Read More</a>
    			<?php endwhile; ?>
    				<div class="navigation">
    					<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    					<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		<?php else : ?>
    		<h3>Not Found</h3>
    		<p>Sorry, but you are looking for something that we havent done yet!</p>
    <?php endif; ?>
    	<hr />
    	<div id="sidebar" class="col span-0">
    	<div class="section">
    				<h4 class="ver small">Filter by Category</h4>
    				<ul class="nav">
    				<?php wp_list_categories('title_li=&orderby=id&show_count=1&use_desc_for_title=0&child_of=4'); ?>
    	<hr />
    	<?php get_footer(); ?>


Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi


    Forum Moderator

    If you mean that you want to display all of the Print posts first followed by the News posts etc, then you might be better off using multiple Loops.

    Thanks for the reply.
    Basically id like the category links to work, at the moment they dont.

    You go to “news” which displays everything. News is then further split into print/web/general categories. So id like to be able to select print and only posts tagged with news and print be displayed.

    That make sense?



    Forum Moderator

    Is that in addition to the current display or instead of?

    in addition to.
    i need to make the page function as a normal blog would.



    Forum Moderator

    I think that:

    <?php query_posts('category_name=Print&tag=news+print'); ?>

    is the query you’re looking for. Not sure that you want to do with the results after that.

    So your suggesting rather than have multiple categories, have one category and then also tag the posts. Hadn’t thought of that.

    If I changed query_post part of my code to your version, when I selected the print tag would it display all news posts tagged print?



    Forum Moderator

    Tagged with news and print. To select all posts in the Print category tagged with news, use:

    <?php query_posts('category_name=Print&tag=news'); ?>

    You mentioned post being tagged up above, so I assumed that this is what you’d been doing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘News page with categories as filters’ is closed to new replies.
Skip to toolbar