WordPress.org

Ready to get started?Download WordPress

Forums

How to show list of posts in a category (4 posts)

  1. Jmac1337
    Member
    Posted 1 year ago #

    Hey guys,

    Im just wondering if someone could help me. Im trying to display all of the posts that are in a category on one page. I have done it before on another one of my sites using the following code:
    `<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=21&paged=$paged"); ?>`

    It works completely fine on my other site and so i thought i would transfer that piece of code to my other site and put it in the same place as the last one (although they are two different templates).

    here is where I have placed it (line 11):

    <?php
    /**
     * The default template for displaying content
     */
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="postformat">
        <div class="format-icon"></div>
      </div>
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=21&paged=$paged"); ?>
      <?php if ( is_single() ): ?>
      <header class="entry-header">
        <h1 class="entry-title">
          <?php the_title(); ?>
        </h1>
      </header>
      <span class="date-i fleft">
      <?php the_time('F jS, Y') ?>
      </span>
      <?php edit_post_link( __( 'Edit', 'wplook' ), '<span class="edit-i">', '</span>' ); ?>
      <?php else : ?>
      <header class="entry-header">
        <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'wplook' ), the_title_attribute( 'echo=0' ) ); ?>" 
    
    rel="bookmark">
          <?php the_title(); ?>
          </a></h1>
      </header>
      <span class="date-i fleft"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'wplook' ), the_title_attribute( 'echo=0' ) ); ?>" 
    
    rel="nofollow">
      <?php the_time('F jS, Y') ?>
      </a></span>
      <?php edit_post_link( __( 'Edit', 'wplook' ), '<span class="edit-i">', '</span>' ); ?>
      <?php endif; ?>
      <?php if ( is_search() ) : // Only display Excerpts for Search ?>
      <div class="entry-content">
        <?php the_excerpt(); ?>
      </div>
      <!-- .entry-summary -->
      <?php elseif ( is_single() ): ?>
      <div class="entry-content">
        <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'wplook' ) ); ?>
        <?php wp_link_pages( array( 'before' => '<div class="clear"></div><div class="page-link"><span>' . __( 'Pages:', 'wplook' ) . '</span>', 'after' => '</div>' ) ); 
    
    ?>
        <!-- .entry-content -->
        <div class="clear"></div>
      </div>
      <footer class="entry-utility">
        <div class="alignright">
          <?php wplook_prev_next(); ?>
        </div>
        <div class="alignleft">
          <?php wplook_get_author(); ?>
          <?php wplook_get_category(); ?>
          <?php wplook_get_tag_list(); ?>
        </div>
        <div class="clear"></div>
      </footer>
      <?php else : ?>
      <div class="entry-content">
        <?php the_content( __( '', 'wplook' )); ?>
        <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'wplook' ) . '</span>', 'after' => '</div>' ) ); ?>
    
      <?php endif; ?>
    </article>
    <?php comments_template( '', true ); ?>

    Im not a php genius of any sorts and i thought applying the same method would work but sadly i was mistaken. Could someone please help me out!

    Thanks in advanced!

    J.Mac

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

  3. Jmac1337
    Member
    Posted 1 year ago #

    Im using dailypost theme, it seems to be quite oddly made- not the usual coding design i am used to

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    Im trying to display all of the posts that are in a category on one page.

    this is already something what a category archive page would do - without much coding, consider to use the plugin 'page-links-to' and link a page from the menu to a category archive page.

    or you could start with a page template;
    for example copy page.php, and save it as category21-page-template.php;
    replace the commented stuff with the necesssary code at the top;
    replace this line:
    get_template_part('content', 'page' ) ;
    with this code:

    <div id="content">
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=21&paged=$paged");
    if ( have_posts() ) : ?>
    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content', get_post_format() ); ?>
    <?php endwhile; ?>
    <?php  wplook_content_nav( 'nav-below' ); ?>
    <?php endif; ?>
    </div><!-- #content -->
    <div class="clear"></div>

    example of the full category21-page-template.php: http://pastebin.com/QTZ0VxgF

    if you want to show all posts on one page, change the query to:

    query_posts("cat=21&posts_per_page=-1");

Topic Closed

This topic has been closed to new replies.

About this Topic