WordPress.org

Ready to get started?Download WordPress

Forums

News page with categories as filters (8 posts)

  1. Cultivate
    Member
    Posted 4 years ago #

    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:

    News
    - 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

    <?php
    /*
    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; ?>
    </div>
    
    <hr />
    
    	<div id="content" class="col right span-8 last">
    		<?php
    		$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(); ?>">
    					<?php
    					$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 );
    					} ?>
    					</a>
    
    						<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>
    				</div>
    
    			<?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>
    				</div>
    			-->
    
    		<?php else : ?>
    
    		<h3>Not Found</h3>
    		<p>Sorry, but you are looking for something that we havent done yet!</p>
    
    <?php endif; ?>
    	</div>	
    
    	<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'); ?>
    				</ul>
    			</div>
    	</div>
    
    	<hr />
    
    	<?php get_footer(); ?>

    Thanks

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  3. Cultivate
    Member
    Posted 4 years ago #

    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?

  4. esmi
    Forum Moderator
    Posted 4 years ago #

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

  5. Cultivate
    Member
    Posted 4 years ago #

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

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  7. Cultivate
    Member
    Posted 4 years ago #

    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?

  8. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic