CPT UIの投稿を「最近の投稿」に表示させたい…!

2024年1月10日

背景

カスタム投稿はどうも普通の投稿じゃないので新着には出ないので、手間かける必要がありました。

方法

  • widgetにphpが書けるプラグイン「PHP Code Widget」をインストール
  • [外観] > [ウィジェット]で、自分が利用しているブロックのところに「PHPコード」ウィジェットをドラグアンドドロップ
  • コードに下記を入力する
<?php 
$args = array('post_type' => array('カスタム投稿のslug名1', '複数あればカスタム投稿のslug名2'), 'posts_per_page' => 5);
$news = new WP_Query( $args );
if ( $news -> have_posts()):
 ?>
   <dl>
   <?php
while ($news -> have_posts()): $news -> the_post();
?>
     <dt><a href="<?php the_permalink(); ?>"><?php echo get_the_date(); ?></a></dt>
     <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
   <?php endwhile; ?>
   </dl>

          <?php endif;
wp_reset_postdata();
 ?>

参考

https://gokansoichiro.com/blog/wordpress-custom-post-type-ui/

感謝です

Posted by ocarina