2011-11-25 356 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

相關問題