wordpress create new template page code example

Example 1: create custom page template wordpress

/*           make this file in template-parts folder 
                 give file name as category_post.php        */

<?php
/**
 * Template Name: Category Custom Page
 */
 
get_header(); ?>
 
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
 
    <?php
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'wordpress',
        'posts_per_page' => 5,
    );
    $arr_posts = new WP_Query( $args );
 
    if ( $arr_posts->have_posts() ) :
 
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail();
                endif;
                ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header>
                <div class="entry-content">
                    <?php the_excerpt(); ?>
                    <a href="<?php the_permalink(); ?>">Read More</a>
                </div>
            </article>
            <?php
        endwhile;
        wp_pagenavi(
            array(
                'query' => $arr_posts,
            )
        );
    endif;
    ?>
 
    </main><!-- .site-main -->
</div><!-- .content-area -->
 
<?php get_footer(); ?>

Example 2: wordpress define template name

/*on top of Php file add the below line to make as custom template*/

<?php /* Template Name: Custom template name */ ?>

Tags:

Php Example