WordPress.org

Support

Support » How-To and Troubleshooting » Help – Title list of posts of a specific category.

Help – Title list of posts of a specific category.

  • EM4NU3L3
    Member

    @em4nu3l3

    Hello, I need help to understand why this code doesnt work.

    Actually it works, but if I change the category ID number doesnt work… the code shows all the titles of all the categories. Hope you understand me… here the code:

    <?php
    /*
    Template Name: List of titles - posts of a specific category
    */
    
    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php get_template_part( 'content', 'page' ); ?>
    
    					<?php comments_template( '', true ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
                            <?php $myposts = get_posts('numberposts=500category=1');
    foreach($myposts as $post) : ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php setup_postdata($post); ?>
    <?php endforeach; ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar( $name ); ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Christian1012
    Member

    @christian1012

    EM4NU3L3
    Member

    @em4nu3l3

    Thank you but Im a newby of the php world…

    is this correct? doesnt work… so
    Could you please explain me whats wrong on this code and on my first code?

    thx

    <?php
    /*
    Template Name: List Exhibitions
    */
    
    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    <?php
    
    $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 2 );
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    	<li>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	</li>
    <?php endforeach;
    wp_reset_postdata();?>
    
    					<?php get_template_part( 'content', 'page' ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar( $name ); ?>
    EM4NU3L3
    Member

    @em4nu3l3

    Why this page shows me all the post of all categories?

    I need the post just of the categorie slug: 2.

    <?php
    /**
    *
    Template Name: Exhibitions Posts
    */
    
    get_header(); ?>
    
    <?php query_posts('category=2&numberposts=5'. get_query_var('paged')); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    			<?php if ( have_posts() ) : ?>
    
    				<?php toolbox_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 toolbox_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', 'toolbox' ); ?></h1>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'toolbox' ); ?></p>
    						<?php get_search_form(); ?>
    
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			 <?php endif; wp_reset_query(); ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar( $name ); ?>

    Please help me Im getting crazy!
    Im studying alone and its difficoult.

    esmi
    Forum Moderator

    @esmi

    From what I can see, the above template should work.

    EM4NU3L3
    Member

    @em4nu3l3

    Thats way Im still trying to understand. Becouse actually doesnt work!
    Is there some options on the wordpress setting that I have to check?

    What about the first code I posted?
    whats wrong?

    thx

    keesiemeijer
    moderator

    @keesiemeijer

    Try changing this:

    query_posts('category=2&numberposts=5'. get_query_var('paged'));

    to this:

    query_posts('category=2&posts_per_page=5&paged='. get_query_var('paged'));

    EM4NU3L3
    Member

    @em4nu3l3

    Quite Solved
    This one works:

    <?php
    /**
    *
    Template Name: Exhibitions Posts
    */
    
    get_header(); ?>
    
    <?php query_posts('cat=11&showposts=5'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php endwhile; endif; ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    			<?php if ( have_posts() ) : ?>
    
    				<?php toolbox_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 toolbox_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', 'toolbox' ); ?></h1>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'toolbox' ); ?></p>
    						<?php get_search_form(); ?>
    
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			 <?php endif; wp_reset_query(); ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar( $name ); ?>

    And its ok for one of the page that I need.
    Now I need, for another cotegory, just the list of the titles of a specific category…

    Any tips?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help – Title list of posts of a specific category.’ is closed to new replies.