我以为我在阅读了很多 SO 文章后就明白了这一点,但我不断收到错误。基本上,在我的 content.php 页面模板中,我想根据您是在存档页面还是单个帖子页面上显示不同的文章标签。所以我一直在做这样的事情:
<?php if ( is_archive() ) {
echo '<article id="post-' . the_ID() . '">';
} else {
echo '<article id="post-' . the_ID() . ' . "post_class() . '">';
}
?>
但是这里发生的事情并不是在页面上输出 ID,所以生成的 HTML 看起来像这样:
1234<article id="post-">Content Goes Here</article>
什么时候应该......
<article id="post-1234">Content Goes Here</article>
那么为什么这显示不正确呢?
最佳答案
the_ID()
(以及许多其他 WP 函数)有一个变体 get_the_ID()
您需要在此处使用。 the_ID()
在内部执行自己的 echo 操作; get_the_ID()
返回它。
关于php - 尝试在 HTML echo 语句中获取 WordPress PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53965702/