WordPress.org

Ready to get started?Download WordPress

Forums

Help - Title list of posts of a specific category. (8 posts)

  1. EM4NU3L3
    Member
    Posted 10 months ago #

    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 ); ?>
  2. Christian1012
    Member
    Posted 10 months ago #

  3. EM4NU3L3
    Member
    Posted 10 months ago #

    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 ); ?>
  4. EM4NU3L3
    Member
    Posted 10 months ago #

    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.

  5. esmi
    Forum Moderator
    Posted 10 months ago #

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

  6. EM4NU3L3
    Member
    Posted 10 months ago #

    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

  7. keesiemeijer
    moderator
    Posted 10 months ago #

    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'));
  8. EM4NU3L3
    Member
    Posted 10 months ago #

    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?

Reply

You must log in to post.

About this Topic