• kennethschultz

    (@kennethschultz)


    I’m trying to make a page-template that only shows one specific category of posts. I’ve seached the web, but can’t find the correct way to implement it.

    I’ve found examples like: https://wpmu.org/wordpress-show-posts-from-one-category/

    but I can’t get my head around the correct way to implement the <?php query_posts('cat=4'); ?>

    with the rest of the php-code for the page-template.

    Does anybody have a solution for this, i presume, classical request and problem.

    Sincerely Kenneth

Viewing 11 replies - 1 through 11 (of 11 total)
  • artwired

    (@artwired)

    Hi Kenneth,

    Is this how you have your code set up to accept your category?

    <?php query_posts('cat=4'); ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    Brad

    esmi

    (@esmi)

    Are you using a child theme?

    Thread Starter kennethschultz

    (@kennethschultz)

    Hello

    Thanks for the response. But a matter a fact I (sort of) solved the problem myself before seeing these responses.
    I dublicated the category.php file and made it into a page-template. Adding
    <?php query_posts('cat=4'); ?>
    just before
    <?php if ( have_posts() ) : ?>

    But – now I just want it to show the LATEST post from the specific category! And that solution I’ve not been able solve. Any ideas?

    Sincerely Kenneth

    Michael

    (@alchymyth)

    see all parameters for query_posts()https://codex.www.ads-software.com/Class_Reference/WP_Query#Parameters

    example for the one latest post of a category:

    <?php query_posts('cat=4&posts_per_page=1&orderby=date&order=DESC'); ?>

    this might also catch posts which are in the child categories of cat 4; to avoid this, use:

    <?php query_posts( array( 'category__in' => array(4), 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); ?>
    Thread Starter kennethschultz

    (@kennethschultz)

    Thanks alchymyth

    But this raises another problem I’ve tried to solve for a long time.
    How can I make sure that it ONLY shows the LATEST post form EACH category. There are 6 categories in total and it’s not certain that all of the different categories will be update simultaniously.
    That means the code can’t look like this:
    <?php query_posts('cat=4,5,6,7,8,16&posts_per_page=6&orderby=date&order=DESC'); ?>

    What do I have to change?

    Thanks a lot for the help.

    sincerely Kenneth

    Michael

    (@alchymyth)

    it ONLY shows the LATEST post form EACH category.

    you will need to loop through all categories, and get one latest post for each category – unfortunately, this cannot be done with one single query.

    example (without html tags):

    <?php
    $all_cats = array( 4,5,6,7,8,16 ); //manual category list
    foreach( $all_cats as $cat ) {
    echo get_category($cat)->name; //optional category title output
    $latest_post = new WP_Query( array( 'posts_per_page' => 1, 'category__in' => array( $cat ) ) );
    if( $latest_post->have_posts() ) : while( $latest_post->have_posts() ) : $latest_post->the_post();
    //YOUR OUTPUT, for example
    the_title();
    //end of YOUR OUTPUT
    endwhile;
    else : echo 'no post';
    endif; wp_reset_postdata();
    }
    ?>
    Thread Starter kennethschultz

    (@kennethschultz)

    Hi alchymyth

    thanks – but unfortunately this is the kind of situation where my basic understanding of WordPress and the structure of the code is revealed.
    I’m not quite sure how to combine your example with my code; what to leave out and what to keep.

    Right now my code for the page template looks like this

    <?php
    /**
     * Template Name: Category TEST NEW
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    
    get_header(); ?>
    
    		<section id="primary">
    			<div id="content" role="main">
    
    				<?php query_posts('cat=4,5,6,7,8,16&posts_per_page=6&orderby=date&order=DESC'); ?>
    
    			<?php if ( have_posts() ) : ?>
    
    				<header class="page-header">
    					<h1 class="page-title"><?php
    						printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    					?></h1>
    
    					<?php
    						$category_description = category_description();
    						if ( ! empty( $category_description ) )
    							echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );
    					?>
    				</header>
    
    				<?php twentyeleven_content_nav( 'nav-above' ); ?>
    
    				<?php /* Start the Loop */ ?>
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php
    						/* Include the Post-Format-specific template for the content.
    						 * If you want to overload this in a child theme then include a file
    						 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    						 */
    						get_template_part( 'content', get_post_format() );
    					?>
    
    				<?php endwhile; ?>
    
    				<?php twentyeleven_content_nav( 'nav-below' ); ?>
    
    			<?php else : ?>
    
    				<article id="post-0" class="post no-results not-found">
    					<header class="entry-header">
    						<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
    						<?php get_search_form(); ?>
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			<?php endif; ?>
    
    			</div><!-- #content -->
    		</section><!-- #primary -->
    
    <?php get_footer(); ?>

    The question is now where to add your code. I’m sorry for this dilettantish behaviour.

    Sincerely Kenneth

    Michael

    (@alchymyth)

    possible integration of both codes:

    https://pastebin.com/CnZ81s7w

    details depend on your idea of the formatting and layout.

    Thread Starter kennethschultz

    (@kennethschultz)

    Hi alchymyth

    It worked perfectly! Thanks a lot.

    Right now it shows the 6 different categories in alphabetical order.
    If I also wanted to control that aspect. Either by the latest post first or in an order controlled by me, manually … Would that be possible?

    For now, thanks an awful lot for the solution I’ve searched for a loooong time ??

    Sincerely Kenneth

    Michael

    (@alchymyth)

    it shows the 6 different categories in alphabetical order … in an order controlled by me, manually

    this is controlled by the order of category ID you add to the array;

    by the latest post first

    much more complex; you would need to change the dode to using get_posts() and gather the latest post of each category first, possibly in an array, including the category; then sort the array by post date, and output the sorted elements …

    Thread Starter kennethschultz

    (@kennethschultz)

    Hi alchymyth

    The manually order I figured out – thanks.

    Regarding my second question I think I’ll have to dive into the WordPress Codex and start reading, because i’m still trying to grasp the structure of the code you so kindly supplied. I’ve searched and read so many posts regarding this question without really understanding the different suggestions. So I’ll better take it from the very beginning.

    But thanks a lot for your help.

    sincerely Kenneth

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to make a page-template showing only one category of post’ is closed to new replies.