How to create a page in wordpress theme to list post with thumbnails from any category ?

If want to create a seprate page to display some categories differently in your wordpress themes, you can do it easily. following code will display post titles only on your pages. You need to create a custom field when create a new page. Steps: 1.create a new page in wordpress? 2. write something on your page. 3. create a new custom fields, name it category and in value type category name from which you want to display posts. 4. select page from drop down in side bar and hit publish . 5. By using this code you can display post from any category on your page. for example you can create a category for training course and display all posy from that category with this page. create a new page in your theme and add following code in it. name your page something like page-course.php or anything you want.



<?php
/*
* This
Template Name: Course Page with Thumbnails
*/

get_header(); ?>

    <div id="primary" class="content-area span12">
      <div id="content" class="site-content course-page" role="main">

        <?php if (have_posts()) : while (have_posts()) : the_post();?>
           <div class="post">
             <h1 class="large-page-title" id="post-<?php the_ID(); ?>"><?php the_title();?></h1>
           <div class="entrytext">
            <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
           </div>
           </div>
           <?php endwhile; endif; ?>
           <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
          </div>

          <?php
          if (is_page() ) {
          $category = get_post_meta($posts[0]->ID, 'category', true);
          }
          if ($category) {
           $cat = get_cat_ID($category);
           $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
           $post_per_page = -1; // -1 shows all posts
           $do_not_show_stickies = 1; // 0 to show stickies
           $args=array(
            'category__in' => array($cat),
            'orderby' => 'date',
            'order' => 'ASC',
            'paged' => $paged,
            'posts_per_page' => $post_per_page,
            'ignore_sticky_posts' => $do_not_show_stickies
           );
           $temp = $wp_query; // assign orginal query to temp variable for later use  
           $wp_query = null;
           $wp_query = new WP_Query($args); ?>
          <ul class="thumbnails course-thumbnails clearfix">
                      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                      <li class="span4">
                          <a href="<?php the_permalink(); ?>" class="thumbnail">
                                            <?php
                                            // check if the post has a Post Thumbnail assigned to it.
                                            if ( has_post_thumbnail() ) {
                                                the_post_thumbnail();
                                            } 
                                         ?>
                                        </a>
                                        <h1 class="course-post-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'watan' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(''); ?></a></h1>
                      </li>
                      <?php endwhile; ?>
                    </ul>


            <div class="navigation">
             <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
             <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
            </div>
           <?php else : ?>
              <h2 class="center">Not Found</h2>
              <p class="center">Sorry, but you are looking for something that isn't here.</p>
              <?php get_search_form(); ?>

            <?php endif; 
            
            $wp_query = $temp; //reset back to original query
            
          } // if ($category)
          ?>

      </div><!-- #content .site-content -->
    </div><!-- #primary .content-area -->


<?php get_footer(); ?>


Reactions:

0 comments: