【问题标题】:Get current page id and page title inside the theme functions.php在主题functions.php中获取当前页面ID和页面标题
【发布时间】:2022-11-02 19:23:28
【问题描述】:
是否可以在当前主题 functions.php 中获取当前页面 ID 和页面标题?
注意:我不是指帖子ID。
我尝试过在themes->twentytwentytwo->functions.php 文件中使用以下函数,但没有成功。
prin_r(the_title());
print_r(get_queried_object_id());
print_r(get_the_title());
【问题讨论】:
标签:
wordpress
wordpress-theming
【解决方案1】:
据我了解,functions.php 包含得太早,无法使用 $post 或 get_queried_object_id()。
我之前所做的是在functions.php 中创建一个函数,您可以在模板中调用该函数并将其传递给它的id 或标题。
例如在你的functions.php中:
function myPage($pageId, $pageTitle) {
//Do whatever you want and return the result
}
然后在您的模板中调用该函数:
<?php
$myPage(get_the_ID(), get_the_title());
?>
希望能帮助到你