高级自定义字段:调用未定义函数 get_field()

问题描述 投票:0回答:1

如果我尝试在自定义函数插件中使用 ACF,则会收到此错误:

Uncaught Error: Call to undefined function get_field() in

我创建了自定义函数插件以避免使用主题附带的默认functions.php,因为它会覆盖每次更新。

我的代码:

<?php

$author_id = get_the_author_meta('ID');
$author_badge = get_field('post_autor', 'user_'. $author_id );

?>
<img src="<?php echo $author_badge['url']; ?>" alt="<?php echo $author_badge['alt']; ?>" />

我正在使用 Gantry5 主题。

我已经遵循了文档并阅读了一些带有示例的帖子,但即便如此我也无法让它工作。该字段设置为

User Object

更新:

我在开发者的网站上找到了这段代码,我打算尝试一下。此代码应该允许 ACF(免费版本)加载到插件中。

// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );

// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );

// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
    return MY_ACF_URL;
}

// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
    return false;
}

https://www.advancedcustomfields.com/resources/include-acf-within-a-plugin-or-theme/

第二次更新: 还是有同样的问题。

php mysql advanced-custom-fields acfpro
1个回答
0
投票

嘿@PickleRiiiick您在子主题中使用functions.php,或者您可以覆盖page.php或您想要显示get_field()的任何其他页面 这也适用于您的子主题,因此它永远不会被主主题覆盖

© www.soinside.com 2019 - 2024. All rights reserved.