1

i am strugling with huge problem :( Whole day i am trying loop to work. I use default wordpress theme. if i put loop on index.php or home.php posts are working fine. And if i put loop in some pate templte for example blog-template.php i get blank screen ?? How loop work on index.php or archive.php or categories.php but not on any page templates???

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

        <?php /* The loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>

        <?php twentythirteen_paging_nav(); ?>

    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>

As i said with this code on index.php i get graet results along with prev/next page buttons. But if i use on template file i got blank screen? Please help

1
  • Your problem is not related to your code. You get the blank page because of a syntax error. Enable debug by setting debug to true in wp-config.php. Commented Apr 14, 2014 at 7:07

3 Answers 3

4

Place this preceding your loop. I suffered through the same issue.

<?php query_posts('post_type=post') ?>

EDIT:

That's a quick fix to the issue, I suppose. I just looked at the query_post function and the wordpress codex seems to violently denounce it; emphatically deriding it as inefficient and overly simplistic in usage. It does however recommend get_posts.

You aren't the only individual suffering this problem though. Wordpress, along with everyone else, seems to readily document using the loop but not really...implementing it. I think most sites just sort've take it for granted you know what you're doing.

sigh.

Sign up to request clarification or add additional context in comments.

Comments

2

in page template you need to pass query_posts before have_posts

$args = array(
'post_type'=> 'post',

);
query_posts( $args );

6 Comments

so in basic have code like this <?php $args = array( 'post_type'=> 'post',); query_posts( $args ); ?> <?php if ( have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php twentythirteen_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?>
i tried but same result :( brezilla.info/drwerner/blog this is result i got
replace <?php get_template_part( 'content', get_post_format() ); ?> with <?php the_title(); ?> and let me know
my file was broklen so your firs tsolution works but not quite well. check please brezilla.info/drwerner/blog it show me posts and older/new posts BUT as it seems it show same posts on both paegs? i limited from admin 2posts pare page, but when i click old it shows same posts ?
|
0

Please follow the steps to create custom page template:

  1. Create new file i.e. custom-page-template.php. Copy and paste the following commenting lines at the top of the file

    <?php
    /*
    
    Template Name: Custom Template 1
    
    */
    
    ?>
    
  2. Copy and paste the following codes in custom-page-template.php:

    <?php 
    get_header(); 
    ?>
    
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
    
        <?php /* The loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
    
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                    <div class="entry-thumbnail">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <?php endif; ?>
    
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header><!-- .entry-header -->
    
                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                </div><!-- .entry-content -->
    
                <footer class="entry-meta">
                    <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-meta -->
            </article><!-- #post -->
    
            <?php comments_template(); ?>
        <?php endwhile; ?>
    
    </div><!-- #content -->
    

  3. In the admin panel create new page and select the page template "Custom Template 1" from the page attribute panel (at right side). See the following image to select page template:

    enter image description here

  4. Now save and see the view. It must work.

8 Comments

Hello i treid your approach also :) i made new template , paste the code , created new page choose proper tempalte of course and -> brezilla.info/drwerner/new-page . i must mention i use fresh install of wordperss and fresh theme i did not alter functions.php or anything
did you put some content in the page? Because I saw title is shown so, content should be shown.
nope no content its empty page with title. i want to get those posts on blog page i did not belived that its so complicated :) Solution of Bhumi Shah is working except older/newer posts buttons
brezilla.info/drwerner/new-page his solution show me the posts and links i wanted back/fwd but in address bar i see page1 page2 but same post loaded for every page
I thought, you wanted to create custom page template but actually you want to show some posts of category in a custom page template. I explained how to create custom page template. Now you can add your code to show posts. There are many ways: Bhumi shows one. You may read this codex.wordpress.org/Template_Tags/get_posts , codex.wordpress.org/Function_Reference/query_posts and codex.wordpress.org/Class_Reference/WP_Query
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.