Oletem
1/11/2018 - 12:41 PM

WP Queries/How to create custom page

First, we create page-custom.php. Second, in the head we comment the name of template, that will be displayed and available to select in page attributes template => custom template selector. Then, in query, created in custom-page.php we can specify, what content will be displayed on this page More refernce: https://codex.wordpress.org/Class_Reference/WP_Query

<?php
/**
 * Template Name: Custom Template - how WP knows what the page is 
 */
get_header(); ?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <article <?php post_class(); ?>>
                <header class="entry-header">
                    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
                </header><!-- .entry-header -->
                <div class="entry-content">
                    <?php
                        // The Query
                        $args = array(
                            'post_type' => 'post', - what type of content should the custom page widget display it can be any of the mentioned in WP Codex
                            'order' => 'ASC',
                            'orderby' => 'title',
                            'posts_per_page'=>3 - how many posts to display
                        );
                        $the_query = new WP_Query( $args );
                        // The Loop
                        if ( $the_query->have_posts() ) {
                            echo '<ul>';
                            while ( $the_query->have_posts() ) {                                
                                $the_query->the_post();
                                echo '<li>' . get_the_title() . '</li>';
                            }
                            echo '</ul>';
                        } else {
                            // no posts found
                        }
                        /* Restore original Post Data */
                        wp_reset_postdata();
                    ?>
                </div>
            </article>
        </main><!-- .site-main -->
    </div><!-- .content-area -->
<?php get_footer(); ?>