繁体   English   中英

WordPress简码以调用最新帖子

[英]Wordpress Shortcode to call latest posts

我创建了一个自定义页面模板,以显示最新的12篇文章及其各自的标题和摘录,但是我坚信,如果我可以用简码来称呼它会更容易。

这是“ post-grid.php”中的循环,它调用了这三件事。

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

但是我不确定如何将以上功能实现。 我感谢您的帮助!

由于您没有编辑任何代码-您正在创建自己的代码-只需将所有代码原样放在回调函数中即可,它应该可以工作。

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>
}
  <?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(); ?> 

希望这对您有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM