Sunday, March 29, 2015

Wordpress display post by category on custom page template.


1) Create a page on theme folder name "custom.php"
2) Paste following code into custom.php file



<?php
/*
Template Name: Custom category page.
*/
?>

<?php get_header(); ?>

<h2>Post of category "Uncategorized"</h2><br />

<?php

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   *
   * It's good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /**
   * We construct the pagination arguments to enter into our paginate_links
   * function.
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

}


  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $custom_args = array(
      'post_type' => 'post',
      'posts_per_page' => 2,
      'paged' => $paged
    );

  $custom_query = new WP_Query( $custom_args ); ?>

  <?php if ( $custom_query->have_posts() ) : ?>
 
    <!-- the loop -->
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <article class="loop">
        <h3><?php the_title(); ?></h3>
        <div class="content">
          Excerpt : <?php the_excerpt(); ?><br />
 Content : <?php the_content(); ?><br />
 Link Page : <?php wp_link_pages(); ?><br />
 ID : <?php the_ID();?><br />
 Category : <?php the_category( $separator, $parents, $post_id ); ?> <br />
 Category Description : <?php category_description();?><br />
 Tags : <?php the_tags();?><br />
 Meta : <?php the_meta();?><br />
 Author : <?php the_author_posts_link(); ?> <?php //echo get_the_modified_author(); ?> : <?php //the_author(); ?>: <?php //echo get_the_author_posts(); ?><br />
 Last modified: <?php the_modified_date('F j, Y'); ?><br />
 Created on : <?php echo get_the_date( 'Y-m-d' ); ?><br />
 Published time : <?php the_time( 'H:i:s' ); ?><br />
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->
    <?php
      if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<?php
get_footer();
?>


3) Create one page on backend side and select template "Custom category page".

Thats it.

No comments:

Post a Comment

Please mention your comments.......