【问题标题】:How to catch the specific page template in WordPress如何在 WordPress 中捕获特定的页面模板
【发布时间】:2022-08-21 15:43:35
【问题描述】:

这是一个屏幕截图:

我想捕捉一个特定的页面模板并根据特定的页面模板显示元框。我试过用它:

$pageTemplate = get_post_meta($post->ID, \'_wp_page_template\', true);

但不工作。

    标签: wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    将以下代码添加到functions.php

    add_filter('template_include', 'var_template_include', 1000);
    function var_template_include($t) {
        $GLOBALS['current_theme_template'] = basename($t);
        return $t;
    }
        
    function get_current_template() {
        if (isset($GLOBALS['current_theme_template'])) {
            return $GLOBALS['current_theme_template'];
        } else {
            return false;
        }
    }
    

    然后,您将能够执行以下操作:

    $pageTemplate = get_current_template();
    

    【讨论】:

    • 是的,它有助于捕获当前页面模板。我什么时候会留在这个页面。但是我想捕获我的创建页面模板和模板文件名,并且我想根据帖子中的特定模板显示元框,请查看屏幕截图prnt.sc/kvnDOZmLcZ-6,当用户选择特定页面然后显示这些页面中的特定元框时邮政
    • 在这种情况下,我建议您尝试插件“高级自定义字段”(ACF),它将允许您根据正在编辑的内容类型创建编辑器 UI 元素。
    • 是的,我知道,但是 ACT 现在要付款了,而且价格对我来说非常高。所以我正在寻找 ACF 的其他选项
    猜你喜欢
    • 2017-08-31
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多