WordPress全站编辑主题:如何获取归档页各文章ID
归档页面获取每篇文章ID(WordPress FSE主题开发)
问题描述
我正在开发一款适配WordPress全站编辑(FSE)的主题,需要在归档页面中获取每篇展示文章的ID。尝试过get_the_ID()和get_queried_object_id()函数,但均未成功,希望找到无需JavaScript的简便解决方案。
注:前端中每篇文章被包裹在带有post-{id}类的<li>标签中,可通过JS获取ID,但优先用PHP实现。
现有代码
archive.html
<!-- wp:template-part {"slug":"topbar","tagName":"topbar","className":"site-topbar"} /--> <!-- wp:template-part {"slug":"header","tagName":"header","className":"site-header"} /--> <!-- wp:group {"align":"full","className":"site-inner"} --> <div class="wp-block-group alignfull site-inner"> <!-- wp:group {"className":"content-sidebar-wrap"} --> <div class="wp-block-group content-sidebar-wrap"> <!-- wp:group {"tagName":"main","align":"wide","className":"site-content"} --> <main class="wp-block-group alignwide site-content"> <!-- wp:group {"align":"wide","layout":{"inherit":true}} --> <div class="wp-block-group alignwide"> <!-- wp:group {"align":"wide"} --> <div class="wp-block-group alignwide"> <!-- wp:pattern {"slug":"theamy/archive-title"} /--> <!-- wp:term-description /--> </div> <!-- /wp:group --> </div> <!-- /wp:group --> <!-- wp:pattern {"slug":"theamy/query-archive-grid"} /--> </main> <!-- /wp:group --> </div> <!-- /wp:group --> </div> <!-- /wp:group --> <!-- wp:template-part {"slug":"footer","tagName":"footer","className":"site-footer"} /-->
归档查询Pattern(theamy/query-archive-grid)
<?php /** * Title: Grid of posts in one column. * Slug: theamy/query-archive-grid * Categories: theamy-query */ ?> <!-- wp:group {"tagName":"section","align":"full","layout":{"inherit":true}} --> <section class="is-layout-constrained wp-block-group alignfull theamy-query-archive-grid"> <?php do_action('query-archive-grid-start'); ?> <!-- wp:query {"queryId":0,"query":{"perPage":12,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true,"taxQuery":null},"displayLayout":{"type":"grid"},"align":"wide","layout":{"inherit":false},"lock":{"move":false,"remove":false}} --> <div class="wp-block-query alignwide"> <!-- wp:post-template {"align":"wide"} --> <!-- wp:group {"tagName":"article","layout":{"inherit":false}} --> <article class="wp-block-group"> <!-- wp:post-featured-image {"isLink":true} /--> <!-- wp:group {"tagName":"header","className":"entry-header","layout":{"inherit":false}} --> <header class="wp-block-group entry-header"> <!-- wp:group {"className":"entry-meta","layout":{"type":"block"}} --> <div class="wp-block-group entry-meta"> <!-- wp:post-date {"format":"j F Y"} /--> </div> <!-- /wp:group --> <!-- wp:post-title {"isLink":true} /--> </header> <!-- /wp:group --> <!-- wp:group {"className":"entry-content","layout":{"inherit":false}} --> <div class="wp-block-group entry-content"> <!-- wp:post-excerpt {"moreText":"Lees verder","showMoreOnNewLine":true} /--> </div> <!-- /wp:group --> <!-- wp:group {"tagName":"footer","className":"entry-footer","layout":{"inherit":false}} --> <footer class="wp-block-group entry-footer"></footer> <!-- /wp:group --> </article> <!-- /wp:group --> <!-- /wp:post-template --> <!-- wp:query-pagination {"align":"wide","layout":{"type":"flex","justifyContent":"center"}} --> <!-- wp:query-pagination-previous {"label":"<"} /--> <!-- wp:query-pagination-numbers /--> <!-- wp:query-pagination-next {"label":">"} /--> <!-- /wp:query-pagination --> <!-- wp:query-no-results --> <!-- wp:paragraph --> <p><?php echo __('Geen resultaten gevonden.', 'theamy') ?></p> <!-- /wp:paragraph --> <!-- /wp:query-no-results --> </div> <!-- /wp:query --> </section> <!-- /wp:group -->
解决方案
方法1:在Post Template循环内直接调用get_the_ID()
get_the_ID()仅能在文章循环内部生效,你之前失败的原因大概率是在循环外部调用了该函数。修改你的归档查询Pattern,在wp:post-template对应的文章容器或内容区域添加PHP代码即可获取ID:
比如在<article>标签上添加ID属性:
<!-- wp:group {"tagName":"article","layout":{"inherit":false}} --> <article class="wp-block-group" id="post-<?php echo get_the_ID(); ?>">
或者在文章页脚输出ID(用于调试或后续逻辑):
<!-- wp:group {"tagName":"footer","className":"entry-footer","layout":{"inherit":false}} --> <footer class="wp-block-group entry-footer"> <?php // 获取并使用文章ID $post_id = get_the_ID(); echo '当前文章ID:' . $post_id; // 这里可以添加基于ID的自定义逻辑,比如获取自定义字段等 ?> </footer> <!-- /wp:group -->
方法2:利用钩子获取循环内的文章ID
如果需要在Pattern外部处理文章ID,可以通过the_post钩子在循环触发时获取:
在主题的functions.php中添加:
add_action('the_post', 'get_archive_post_id'); function get_archive_post_id($post) { // $post对象包含当前文章的所有信息,$post->ID即为文章ID $current_post_id = $post->ID; // 这里可以添加自定义逻辑,比如存储ID到全局变量、添加自定义类等 }
注意事项
- 确保代码在
wp:post-template对应的循环范围内执行,只有此时get_the_ID()才能返回当前文章的ID; get_queried_object_id()返回的是归档页面本身的ID(比如分类ID、标签ID),而非单篇文章的ID,这也是你之前用它失败的原因。
内容的提问来源于stack exchange,提问作者Garb o' Donk
相关产品推荐
相关产品推荐

