简体   繁体   English

如何在单个 PHP 文件中包含 WordPress Advance Custom Field 插件功能?

[英]How to include WordPress Advance Custom Field plugin functions in single PHP file?

I'm trying to get value from WordPress post custom field (with WordPress Advance Custom Field plugin) and echoing this the custom field value on a single (standalone) PHP file on WordPress root directory with this PHP code我正在尝试从 WordPress 帖子自定义字段(使用 WordPress Advance Custom Field 插件)获取价值,并使用此 PHP 代码在 WordPress 根目录上的单个(独立)PHP 文件中回显自定义字段值

<?php
    require_once("wp-load.php");

    $value = get_field( "content" );

    echo $value;

?>

and it doesn't work, what is the correct method to include the WordPress functions?它不起作用,包含 WordPress 功能的正确方法是什么?

Thanks for the help.感谢您的帮助。

Use the_field() function使用the_field()函数

<?php
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

$page_id=123;
the_field('content',$page_id);

 ?>

Remove require_once("wp-load.php");删除 require_once("wp-load.php"); from your code and add following code for Retrieving a field as a variable.从您的代码中添加以下代码以将字段作为变量检索。

<?php

$variable = get_field('field_name');

// do something with $variable

echo $variable;

?>

Hope this will help.希望这会有所帮助。 Please let me know if any issue.如果有任何问题,请告诉我。

Custom field is related with specific posts, pages, custom post type so when you want to retrieve filed value then you must pass ID , at root file by default it will not have any POST ID.自定义字段与特定的帖子、页面、自定义帖子类型相关,因此当您要检索归档值时,您必须传递ID ,默认情况下在根文件中它不会有任何 POST ID。

So your code will work in this way所以你的代码将以这种方式工作

<?php
      require_once("wp-load.php");
      $value = get_field( "content", $ID ); // $ID = specific post, page id
      echo $value;

 ?>

This code is worked for me, I hope its work for you as well.这段代码对我有用,我希望它也适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM