2011-11-25 342 views
0

我对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有一个网页叫做“博客”为好,。然后,我将其模板分配到上面的“博客”模板。但问题是,代码无法正常工作。它不显示帖子的标题,评论等,而是显示一些其他信息。另一方面,如果我只是复制此:

<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> 

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

回答

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;  
} 
+0

感谢您的答复。当我运行你的代码,虽然,我得到这个错误:致命错误:调用未定义的函数get_field() – jason

+0

看看现在是否是正确的代码,对不起,我忘了我有一个特殊的特色图像代码。 – gabearnold

+0

再次感谢您的回复。问题似乎与这部分:$ category = get_field('category_blog');虽然。我得到了一个缺少get_field函数的致命错误。我添加了get_images函数到我的funciton.php页面,只是相同,但错误仍在继续 – jason

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

相关问题