Support » Fixing WordPress » Placing posts on page templates for homepage

  • I have read “blog-posts-on-static-front-page” and it’s development the next year (now two years ago) “static-front-page-with-the-latest-post”
    but I’m afraid that it is still not clear to me exactly where to place the code provided. Someone asked the very same thing but the topic was close with this unanswered.
    Here’s the background:

    Here’s my situation:
    I want posts on the front page with a short intro text above. Because I need different templates for the two languages of the site, I have to use a static page for the front page. I have looked at some ‘posts into page’. Maybe I’ve misunderstood their use but I don’t see a clear instruction for getting all the posts (they all seem to be filtering for certain categories or types) or they place posts in a ul li form with the formatting and styles that go along with that.

    Here’s my current thoughts:
    I have two templates : sidebar-left-page ; and sidebar-right-page . I got this from an article and it said that the names had to be exactly these.
    I’m thinking that I need two more templates : home-sidebar-left-page ; and home-sidebar-right-page. That would solve the problem, but I need to
    1) registering the new templates so that I can select themin the page editor
    2) putting the right code in the right place of the loop of the new templates

    The contet part of these template pages looks like this.

    <div id=”content” role=”main”>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( ‘content’, ‘page’ ); ?>
    <?php comments_template( ”, true ); ?>
    <?php endwhile; // end of the loop. ?
    </div><!– #content –>

    The above forum topics, and the axcellent article on “the Loop” have the loop beginning with if
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    Is that a change in recent WordPress versions?

    I’d be grateful for an answer understandable to a novice coder, as I’m sure many readers in the future will be.


Viewing 3 replies - 1 through 3 (of 3 total)
  • Drew Jaynes


    WordPress Core Developer

    Hi Tim,

    You can create your own Page Templates which hold Loops to display posts.

    The filename of page templates follows this pattern: template-whatever.php, which you place in your theme’s root directory. In new file’s header, you set the name, like this:

    Template Name: Whatever

    Then, in your page-editing screen, your new template should show in the Template dropdown in the Page Attributes box:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    Is that a change in recent WordPress versions?

    In that instance, they’re just wrapping the Loop code in an if statement, e.g. If we have have_posts, (yes/no), while we have_posts (if yes then), the_post (loop through our found posts).

    If you’re looking to output posts based on a specific subset of requirements like a specific category, tag, the number of posts etc, you may be better off using the WP_Query class. The following is a heavily commented example of a page template using the WP_Query loop:

    Hope that helps a bit.

    Thanks for looking Drew.
    I couldn’t get the code at pastebin to wotk – or I’m not clever enough to tidy it up.
    I tried something similar

    <?php $temp_query = $wp_query; ?>
    <?php query_posts(‘showposts=5’); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h3>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>

    but it only solves half of the problem.

    The problem begins with the fact that in the standard WordPress, when I go to Settings => Reading, and select Front Page displays Static; it wont allow me to select Home as both the front page and the blog posts page (otherwise I could make a sticky post of the short Welcome message).
    I need to select static pages in order to select my templates with alternate sidebar positions per language (one is RTL lang) because if I have selected the radio button for Front Page as Your Latest Posts, then they do not appear amongst ‘pages’ to select a template for them.

    I either need
    1) a way to register two more templates with right and left sidebar positions that I can put the posts loop into

    2) an If statement such as
    if (ICL_LANGUAGE_CODE=’en’) and if (page_order=0)get template (left_sidebar_2)
    if (ICL_LANGUAGE_CODE=’ar’) and if (page_order=0)get template (right_sidebar_2)

    I know that there’s another option – a decent posts in page plugin. I’ve been looking at them all day. Advanced Post List was looking very promising but I failed to get it working for me.

    These are my current thoughts – but I’m not really experienced enough with code to write a statement that meets all the protocol

    My other option is to tell the client that he can’t have the blog posts on the front page.

    Any help would be appreciated

    Oh , I found out that AND IF is not valid php – it’s &&

    if (ICL_LANGUAGE_CODE=’en’) && (page_order=0) get template (left_sidebar_2)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Placing posts on page templates for homepage’ is closed to new replies.