在静态页面上显示博客帖子

时间:2011-11-25 23:06:55

标签: wordpress-theming wordpress

我对Wordpress很新,并且有一个问题。我创建了自己的主题,似乎一切正常。但是,我有一个问题。我想在我的主页以外的页面上创建我的博客页面(包含所有帖子)。所以,在我的主题文件夹中,我创建了一个名为blog.php的页面模板:

 <?php
  /*
  Template Name: blog  
  */
  ?>
  <?php get_header(); ?>

    <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 
   <?php get_footer(); ?>   

然后,我在wordpress中创建了一个名为“blog”的页面,位于仪表板的“pages”部分。然后我将其模板分配给上面的“博客”模板。但问题是代码不能正常工作。它没有显示帖子的标题,评论等,而是显示其他一些信息。另一方面,如果我只是复制这个:

   <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 

到我的索引页面,它工作正常。那么,如何在主页以外的页面上显示我的所有帖子信息?

2 个答案:

答案 0 :(得分:1)

我想为您提供一个更简单的循环作为第二个选项。如果你使用它并将阅读设置设置到特定的博客页面,这很有效:

<?

/*

Template Name: Blog Template Example

*/

?>

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_content(); ?>
</div>

<?php endwhile; ?>

<div class="navigation">
    <div class="next-posts"><?php next_posts_link(); ?></div>
    <div class="prev-posts"><?php previous_posts_link(); ?></div>
</div>

<?php else : ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1>Not Found</h1>
</div>

<?php endif; ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

Reading Settings

答案 1 :(得分:0)

你做这样的事情:

博客-的template.php:

<?php/*
Template Name: Blog Page
*/
?>

<?php get_header(); ?>
<?php get_template_part( 'layout-page', 'blog' );?>
<?php get_footer(); ?>

布局页面blog.php的:

<?php 
the_post();
$title = get_the_title();
$baselink = get_permalink();
$category = get_field('category_blog'); 

if( !empty($category) ){
    $post_per_page = get_option('posts_per_page'); 
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;

    $categoryID = get_category_id($category);

    $total = get_post_count(array($categoryID));

    $the_query = new WP_Query("posts_per_page={$post_per_page}&cat=    {$categoryID}&paged={$paged}");
?>

<div id="wrapper">
<div id="content">
    <h1 class="title"><?php echo $title; ?></h1>
    <div class="content-middle">
        <div class="node">              
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
            <h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="content">
                <?php echo content(150); ?>
            </div>
            <div class="read-more"><a href="<?php echo get_permalink(); ?>">Read more</a></div>                     
            <?php endwhile; ?>
            <br/><br/>
            <div class="wp-paginate">
            <?php 
                wp_reset_query();

                echo paginate_links( array(
                    'base' => $baselink.'%_%',
                    'total' => ceil($total/$post_per_page),
                    'current' => $paged,
                ));

                ?>
            </div>
        </div>
    </div>

</div> <!-- end content -->

<div style="clear:both"></div>
</div>

<?php
}
?>

这可能都在一个文件中,或者你可以像我写的那样将它分成两部分使用。

编辑:

对不起,我已将它设置为也从帖子中抓取图像。我认为这是您需要的功能代码:

function get_images_by_cat($id){
    $limit = 1000;

    $the_query = new WP_Query("posts_per_page={$limit}&cat={$id}");
    $arr = array();
    while ( $the_query->have_posts() ) { 
        $the_query->the_post();

        $title = get_the_title();
        $image_src = get_field('banner_image');
        $image_link = get_field('banner_link');

        $arr[] = array(
            "title" => $title,
            "link" => $image_link,
            "image" => $image_src,
        );
    }

    wp_reset_query();

    return $arr;    
}
相关问题