2013-07-22 32 views
0

我創建了一個自定義的頁面模板來顯示最新的12帖子與他們各自的標題和摘錄,但我認爲,如果我可以用短代碼來調用它,會更容易。WordPress的短代碼來調用最新的帖子

這是「post-grid.php」中的循環,它調用了這3件事情。

<section class="post-grid"> 
    <?php 
     $grid = array('post_per_page' => 12); 
     $query = new WP_Query($grid); 
     while ($query->have_posts()) : $query->the_post(); 
    ?> 
<div class="grid-posts"> 
    <h2><?php the_title(); ?></h2><br> 
    <?php the_post_thumbnail('featured'); ?><br> 
    <?php the_excerpt() ?><br> 
</div> 
<?php endwhile; // end of the loop. ?> 
</section> 

如何創建一個執行該循環的短代碼?

我知道如何使用

add_shortcode('postGrid', 'postGrid'); 
function postGrid() 
{ 
//Code here 
} 

添加簡碼但林不知道如何實現上述的功能。我感謝您的幫助!

回答

0
<?php 

    $args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12, 
    'paged' => $page, 
    ); 

query_posts($args);?> 
hope this will help you :) 

Point related to add Post Thumbnail: 

// check if the post has a Post Thumbnail assigned to it. 
<?php if (has_post_thumbnail()) { 
the_post_thumbnail(); 
} the_content(); ?> 

希望這有助於你:)

+0

嘿,這個作品!但我如何添加縮略圖?當我把這個帖子換成div時,它會給我500個服務器錯誤! – enriqg9

+0

您的歡迎和即將更新點添加縮略圖。 //檢查帖子是否分配了帖子縮略圖。 <?php if(has_post_thumbnail()){ \t the_post_thumbnail(); } the_content(); ?> – Aaron

0

由於您沒有編輯任何代碼 - 您正在創建自己的代碼 - 只需將所有代碼放在回調函數中就可以了,它應該可以工作。

add_shortcode('postGrid', 'postGrid'); 
function postGrid() 
{ 
    <section class="post-grid"> 
     <?php 
      $grid = array('post_per_page' => 12); 
      $query = new WP_Query($grid); 
      while ($query->have_posts()) : $query->the_post(); 
     ?> 
    <div class="grid-posts"> 
     <h2><?php the_title(); ?></h2><br> 
     <?php the_post_thumbnail('featured'); ?><br> 
     <?php the_excerpt() ?><br> 
    </div> 
    <?php endwhile; // end of the loop. ?> 
    </section> 
} 
+0

我做到了,但我得到500服務器錯誤,我跑我的網站在本地主機! – enriqg9