缩略图

WordPress如何调用指定ID文章

2026年09月27日 网站文章 会被自动插入 会被自动插入
本文最后更新于2026-09-27已经过去了7天请注意内容时效性
热度4 点赞 收藏0 评论0

最近又在折腾自己的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的文章:

<?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=\"<?php the_permalink(); ?>\" rel=\"external nofollow\" ><?php the_title();?></a></li>
<?php endforeach; endif; ?>

更多技术教程,请关注90资源网www.cnhacker90.com

淚
奈
硫
正文结束 阅读本文相关话题
相关阅读
评论框
正在回复
评论列表

暂时还没有任何评论,快去发表第一条评论吧~

空白列表