0

I'm new to wordpress and am trying to develop my first own theme. The thing is: the front page must be static, with no display of posts. But I need a blog section and am trying to do it by creating a custom page template that calls the last 10 posts.

How would I do that using the loop? Please, help me! Thank you all in advance!

  • If developing own theme, always good to start with a wp default theme and then add/delete, change stylesheet. This functionality is already built in, no need to do anything special for your own theme. WP should handle it if you have used one of the default themes as a starting point, it should work. so user can just Go under Settings > Reading, define home page as static and say which page is your home page, and then create a page (empty) to be your 'posts' page. In the next field under, say '10' for the number of posts per page. Don't hardcode the 10, just use what wp already offers. – anmari Jun 25 '18 at 4:56
  • 1
    But why? Why don’t you set a static page for the front page and then use index.php template to display blog index? – Krzysiek Dróżdż Jun 25 '18 at 6:21
  • you can create sort-code for this and you can display latest 10 post for any page template just call your sort-code – Jignesh Patel Jun 25 '18 at 7:34
0

If you create a page template with your own query, you are throwing away all the work WordPress does for you.

In your settings set a static front page. Make a second page and in the settings choose it as your “page for posts”

Then if your theme has a valid loop in the templates everything should just work.

0

By default WordPress displays your content in a blog format on the homepage.

Using Default Static and Blog Page Settings in WordPress. WordPress comes with built-in support for creating a custom home page (static front page), and a separate page for blog posts. To use this method, you need to create two new WordPress pages.

The first page is going to be your custom home page.

Next you need to create another page for your blog posts. You can title this page as Blog. A lot of WordPress themes come with different templates, and it is possible that your theme may have a template to be used for blog page. However, if there is no template available in your theme, then you can simply choose default. Don’t forget to disable the comments and trackbacks option on this page as well.

Now we need to get WordPress to use these pages accordingly. To do that go to Setttings » Reading and under the Front page displays option choose A static page. Below that choose the page to be used as the front page and the page for your blog posts. Save your changes, and load your site to review changes.


And If you want to create a custom Template that calls the latest 10 posts, You can use the following step (Note - You can use your own HTML for the required layout) -

Step 1: Page template

Create a blank page template named “page-blog.php” and include the following code:

    <?php
    /*
    Template Name: Blog
    */
    ?>
        <?php get_header(); ?>

        <div id="content">

        <?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>

        <?php if( have_posts() ): ?>

        <?php while( have_posts() ): the_post(); ?>

        <div id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>

        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(200,220) ); ?></a>

        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

        <span class="meta"><?php author_profile_avatar_link(48); ?> <strong><?php the_time('F jS, Y'); ?></strong> / <strong><?php the_author_link(); ?></strong> / <span class="comments"><?php comments_popup_link(__('0 comments','example'),__('1 comment','example'),__('% comments','example')); ?></span></span>

        <?php the_excerpt(__('Continue reading »','example')); ?>

        </div><!-- /#post-<?php get_the_ID(); ?> -->

        <?php endwhile; ?>

        <div class="navigation">
        <span class="newer"><?php previous_posts_link(__('« Newer','example')) ?></span> <span class="older"><?php next_posts_link(__('Older »','example')) ?></span>
        </div><!-- /.navigation -->

        <?php else: ?>

        <div id="post-404" class="noposts">

        <p><?php _e('None found.','example'); ?></p>

        </div><!-- /#post-404 -->

        <?php endif; wp_reset_query(); ?>

        </div><!-- /#content -->

        <?php get_footer(); ?>

Instead of showing 5 posts, you can set posts_per_page=10 or whatever works best.

Note also that the HTML used in this example is rudimentary to keep things simple. You'll probably need to make a few changes to the markup to synchronize with your theme design.

Step 2: Add New Page

Once page-blog.php is complete and uploaded to the server, log in to the WP Admin and visit the Add New Page screen. There, create a new page named "Blog" (or whatever you want), and set its Template as "Blog" from the "Page Attributes" panel.

Done! Now visit the Blog page after publishing and you should see the custom WP_Query loop working: your latest blog posts will be displayed on the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.