wordpress调用指定post type文章怎么操作

  我们有时会用wordpress创建好几种post type文章,比如默认的post文章和product文章,如果我们要在每个页面的底部调用post type类型为post最新文章要如何操作呢?那我们就需要进行改造一下了,下面就随ytkah一起来看看如何操作吧。

  我们把调用放在footer.php文件里吧,通过li列表形式调取最新发布的post文章,链接加标题,点击直接跳转到文章页

          <ul>
                    <li class="title">News</li>
                    <?php
                        $args = array( 'post_type' => 'post', 'posts_per_page' => 5 );
                        $loop = new WP_Query( $args );
                        while ( $loop->have_posts() ) : $loop->the_post();
                        echo '<li><a href="';
                        the_permalink();
                        echo '">';
                        the_title();
                        echo '</a></li>';
                        endwhile;
                    ?>
                </ul>

  展示效果如下

  文章内容页就用如下代码进行调取

<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;
?>

  有相同需求的朋友可以试试这种方法吧

  参考资料https://blog.wpjam.com/article/wordpress-post-type/

猜你喜欢

转载自www.cnblogs.com/ytkah/p/11024395.html
今日推荐