How to display posts on template

admin2025-01-07  4

I've added a page template that is my new home page and I would like to be able simply list the last 5 blog entries. My home page currently looks like...

<?php
/**
    * This file controls the layout of your homepage
    *
    * @package WordPress
    * @subpackage Pytheas WordPress Theme
    * Template Name: Custom Home
*/

get_header(); ?>

<?php get_template_part('content','slider'); ?>


    <div id="primary" class="content-area span_16 col clr clr-margin">
        <div id="content" class="site-content" role="main">

<?php
        $wp_query = new WP_Query(
            array(
                'post_type' => 'post',
                'showposts' => of_get_option('home_blog_count','4'),
                'no_found_rows' => true,
                'ignore_sticky_posts' => true
            )
        );

        if( $wp_query->posts ) { ?>
            <div id="home-blog" class="row clr">       
                <?php
                // Display heading
                if( of_get_option('home_blog_title') ) { ?>
                    <h2 class="heading">
                        <span><?php echo of_get_option('home_blog_title'); ?></span>
                    </h2>
                <?php } ?>       
                <div class="row clr">    
                    <?php
                    // Begin Loop
                    $wpex_count=0;
                    foreach( $wp_query->posts as $post ) : setup_postdata( $post );
                        $wpex_count++;                  
                        $wpex_clr_margin = ( $wpex_count == 1 ) ? 'clr-margin' : NULL; ?>           
                    <div <?php post_class('home-blog-entry span_6 col '. $wpex_clr_margin); ?>>
                        <?php
                        //featured image
                        if( has_post_thumbnail() ) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="home-blog-entry-img-link">
                                <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ), wpex_img('blog_related_entry_width'),  wpex_img('blog_related_entry_height'),  wpex_img('blog_related_entry_crop') ); ?>" alt="<?php echo the_title(); ?>" class="blog-entry-img" />
                            </a>
                        <?php } ?>
                        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                        <div class="home-blog-entry-excerpt">
                            <?php 
                            // Display excerpt
                            echo ( !empty( $post->post_excerpt ) ) ?
                                apply_filters('the_content', get_the_excerpt() ) :
                                    wp_trim_words( strip_shortcodes( get_the_excerpt() ), of_get_option('portfolio_entry_excerpt_length','200') ); ?>
                        </div><!-- /home-blog-entry-excerpt -->
                    </div><!-- /home-blog-entry -->
                    <?php
                    if( $wpex_count==4) {
                        echo '<div class="clr"></div>';
                        $wpex_count=0;
                    }
                    endforeach; ?>
                 </div><!-- /row -->         
            </div><!-- /home-blog -->       
        <?php
        }
        wp_reset_postdata();?>

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

<?php get_sidebar(); ?>

<?php get_footer();?>

I'm unclear why I'm not able to do something like...

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <div class="entry-conten clr">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links clr">', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                </div><!-- .entry-content -->

                <footer class="entry-footer">
                    <?php edit_post_link( __( 'Edit Page', 'wpex' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-footer -->
            </article><!-- #post -->

            <?php comments_template(); ?>
        <?php endwhile; ?>

which is what I see on many other template files. Can someone help me understand how I can query the most recent X posts and display them using the_content(); (which I think will display the post with the author, post date, etc.)

I've added a page template that is my new home page and I would like to be able simply list the last 5 blog entries. My home page currently looks like...

<?php
/**
    * This file controls the layout of your homepage
    *
    * @package WordPress
    * @subpackage Pytheas WordPress Theme
    * Template Name: Custom Home
*/

get_header(); ?>

<?php get_template_part('content','slider'); ?>


    <div id="primary" class="content-area span_16 col clr clr-margin">
        <div id="content" class="site-content" role="main">

<?php
        $wp_query = new WP_Query(
            array(
                'post_type' => 'post',
                'showposts' => of_get_option('home_blog_count','4'),
                'no_found_rows' => true,
                'ignore_sticky_posts' => true
            )
        );

        if( $wp_query->posts ) { ?>
            <div id="home-blog" class="row clr">       
                <?php
                // Display heading
                if( of_get_option('home_blog_title') ) { ?>
                    <h2 class="heading">
                        <span><?php echo of_get_option('home_blog_title'); ?></span>
                    </h2>
                <?php } ?>       
                <div class="row clr">    
                    <?php
                    // Begin Loop
                    $wpex_count=0;
                    foreach( $wp_query->posts as $post ) : setup_postdata( $post );
                        $wpex_count++;                  
                        $wpex_clr_margin = ( $wpex_count == 1 ) ? 'clr-margin' : NULL; ?>           
                    <div <?php post_class('home-blog-entry span_6 col '. $wpex_clr_margin); ?>>
                        <?php
                        //featured image
                        if( has_post_thumbnail() ) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="home-blog-entry-img-link">
                                <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ), wpex_img('blog_related_entry_width'),  wpex_img('blog_related_entry_height'),  wpex_img('blog_related_entry_crop') ); ?>" alt="<?php echo the_title(); ?>" class="blog-entry-img" />
                            </a>
                        <?php } ?>
                        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                        <div class="home-blog-entry-excerpt">
                            <?php 
                            // Display excerpt
                            echo ( !empty( $post->post_excerpt ) ) ?
                                apply_filters('the_content', get_the_excerpt() ) :
                                    wp_trim_words( strip_shortcodes( get_the_excerpt() ), of_get_option('portfolio_entry_excerpt_length','200') ); ?>
                        </div><!-- /home-blog-entry-excerpt -->
                    </div><!-- /home-blog-entry -->
                    <?php
                    if( $wpex_count==4) {
                        echo '<div class="clr"></div>';
                        $wpex_count=0;
                    }
                    endforeach; ?>
                 </div><!-- /row -->         
            </div><!-- /home-blog -->       
        <?php
        }
        wp_reset_postdata();?>

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

<?php get_sidebar(); ?>

<?php get_footer();?>

I'm unclear why I'm not able to do something like...

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <div class="entry-conten clr">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links clr">', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                </div><!-- .entry-content -->

                <footer class="entry-footer">
                    <?php edit_post_link( __( 'Edit Page', 'wpex' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-footer -->
            </article><!-- #post -->

            <?php comments_template(); ?>
        <?php endwhile; ?>

which is what I see on many other template files. Can someone help me understand how I can query the most recent X posts and display them using the_content(); (which I think will display the post with the author, post date, etc.)

Share Improve this question asked Jul 13, 2013 at 13:54 BenBen 1131 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0
$args = array(
            'posts_per_page'=>-1,
            'number_posts'=>-1,
            'category'=>9,
            'orderby'=>'post_date',
            'order' => 'DESC',
            'post_type'=>'post',
            'post_status'=>'publish'                
        );
$posts = get_posts($args);  
foreach($posts as $post):
    $id= $post->ID;
    $permalink = get_permalink( $id );
    $title = $post->post_title;
    $content = $post->post_content;
    $image = wp_get_attachment_url(get_post_thumbnail_id($id));     
endforeach; 

To alter the main query that pulls posts, I needed to do query_posts( 'posts_per_page=5' ); before has_posts()

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257141a415.html

最新回复(0)