• Resolved stiwdio

    (@stiwdio)


    can someone explain this to me, am using a grid theme, the home page is set to display all posts, but the code below is used in the homage, how would i get this to use in a page template as I’ve tried creating a template with it in but it doesn’t display anything;

    <?php if (have_posts()) : ?>
    <div id="post-area">
    <?php while (have_posts()) : the_post(); ?>	
    
       		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		 <?php if ( has_post_thumbnail() ) { ?>
    
           <div class="imagewrap">
             <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'summary-image' );  ?></a></div>
             <div class="gridly-category"><p><?php the_category(', ') ?></p></div> </div>
    		  <?php } ?>
           			<div class="gridly-copy"><h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt(); ?> 
    
                   <p class="gridly-link"><a href="<?php the_permalink() ?>">read more &rarr;</a></p>
             </div>
           </div>
    
    <?php endwhile; ?>
    </div>
    <?php else : ?>
    <?php endif; ?>
    
    <?php next_posts_link('<p class="view-older">View Older Entries</p>') ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello stiwdio,

    Before that code you would have to tell wordpress which posts you would like to display.

    WordPress has some default code that gets posts based on the which page it is displaying that is not in the template files. When you are on the homepage it gets the newest posts. When you are viewing a page, it gets the “Page” you requested by the URL.

    The first line in that piece of code is saying “If I have posts display them with this code. And the ELSE at the end is saying If I don’t have posts do nothing”

    I would recommend using the get_posts() function to get the posts you want to display in your template
    http://codex.wordpress.org/Template_Tags/get_posts

    You would have to change the have_posts() in the first line to reflect the variable that will contain your posts

    To get the 5 newest posts:

    <?php $args = array(
        'numberposts'     => 5,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'suppress_filters' => true ); ?>
    
    <?php $posts_array = get_posts( $args ); ?>
    <?php if ($posts_array) : ?>
    <div id="post-area">
    <?php foreach ($posts_array as $post) :  setup_postdata($post); ?>	
    
       		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		 <?php if ( has_post_thumbnail() ) { ?>
    
           <div class="imagewrap">
             <div class="gridly-image"><a>"><?php the_post_thumbnail( 'summary-image' );  ?></a></div>
             <div class="gridly-category"><p><?php the_category(', ') ?></p></div> </div>
    		  <?php } ?>
           			<div class="gridly-copy"><h3><a>"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt(); ?> 
    
                   <p class="gridly-link"><a>">read more →</a></p>
             </div>
           </div>
    
    <?php endforeach; ?>
    </div>
    <?php else : ?>
    <?php endif; ?>
    
    <?php next_posts_link('<p class="view-older">View Older Entries</p>') ?>

    I have changed the while loop to a foreach loop.

    <?php $posts_array = get_posts( $args ); ?> tells wordpress to get all posts that match the criteria inside the $args array. If posts are found they are returned and put into the $posts_array variable.

    <?php if ($posts_array) : ?> checks to see if anything was returned from the get_posts function

    <?php foreach ($posts_array as $post) : setup_postdata($post); ?>
    Loops through each post in the $posts_array and sets up the post data so you can use the WordPress functions to display the post

    Hope this helps!

    Thread Starter stiwdio

    (@stiwdio)

    Thank you simalamdeveloper, I would have never have though of that, it worked sort of, theres a rouge ‘”>’ somewhere that keeps showing up in every post. Could this be amended to display all posts of a particular category ?

    Thread Starter stiwdio

    (@stiwdio)

    Ive sorted the few rouge ‘”>’, but would still be interested it I could show all of a particular category such as ‘news’

    Thanks again!!!!

    Your certainly can. I didn’t include all the available args in the example. You cans see them in the codex
    http://codex.wordpress.org/Template_Tags/get_posts

    Category needs to be the ID, not the name so you could do something like this:

    <?php $args = array(
        'numberposts'     => 5,
        'offset'          => 0,
        'category'        => get_cat_ID( 'news' ),
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'suppress_filters' => true ); ?>
    Thread Starter stiwdio

    (@stiwdio)

    Awesome, thank you for all your help!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘not displaying posts’ is closed to new replies.