duane2364 2013-07-22 02:53
浏览 41
已采纳

Wordpress Shortcode来呼叫最新帖子

I created a custom page template to display the latest 12 posts with their respective title and excerpt, but I tought that It would be easier if I could call this with a shortcode.

This is the loop in "post-grid.php" which calls to those 3 things.

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

How can I create a shortcode that executes that loop?

I know how to add a shortcode using

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

But im not sure how to implement the above as a function. I appreciate your help!

  • 写回答

2条回答 默认 最新

  • dsj83686 2013-07-22 11:00
    关注
      <?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(); ?> 
    

    Hope this help you :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟