WordPress如何调用指定ID文章

最近又在折腾自己的wordpress博客,突然想把之前博客调用置顶文章的区块改成调用指定ID文章,于是便寻思着如何实现。

在网上搜了搜,发现利用WordPress函数get_post(),能够调用指定ID的文章链接、标题及内容。

下面是调用文章ID为1的文章:

<?php
$post_id = 1; // 文章ID
echo get_post( $post_id )->post_content; // 输出文章的内容
echo get_post( $post_id )->post_title; // 输出文章的标题
?>

下面是调用文章ID为1,2,3的dede读取指定id文章文章:

<?php
$postsl = get_posts("numberposts=4&post_type=any&include=1,2,3");
if($postsl) : foreach( $postsl as $post ) : setup_postdata( $post );
?>
<li><a href="https://www.wptoo.com/learn/<?php the_permalink(); ?>" rel="external nofollow" ><?php the_title();?></a></li>
<?php endforeach; endif; ?>
© 版权声明
THE END
喜欢就支持一下吧
点赞12赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容