如何在模板上显示帖子

时间:2013-07-13 作者:Webnet

我已经添加了一个页面模板,这是我的新主页,我希望能够简单地列出最后5个博客条目。我的主页当前看起来像。。。

<?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();?>
我不清楚为什么我不能做像。。。

            <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; ?>
这是我在许多其他模板文件上看到的。有人能帮助我了解如何查询最新的X帖子并使用the_content(); (我认为这将显示文章的作者、发布日期等)

2 个回复
SO网友:Balas
$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; 
SO网友:Webnet

要更改获取帖子的主查询,我需要执行以下操作query_posts( \'posts_per_page=5\' ); 之前has_posts()

结束

相关推荐