在谷歌搜索和查看他们的示例后,似乎没有一个单一的来源可以获取 Pod 的内容或自定义字段。上面的答案对我有所帮助,但没有得到很好的解释。
在您的wp-content/plugins 文件夹中,创建一个名为podutils 的新文件夹并创建一个名为PodUtils.php 的php 文件。
将该类复制/粘贴到该文件中:
<?php
//Class to get WordPress Pod data:
class PodUtils {
//get WordPress Pod Object:
public static function GetPodObject($podType, $podSlug) {
//for use with Pods WP Plugin (https://pods.io/):
if(function_exists("pods")) {
$pod = pods($podType, $podSlug)->row();
return $pod;
} else {
return false;
}
}
//get the content from a WordPress Pod:
public static function GetPodContent($podType, $podSlug) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return $pod["post_content"];
} else {
return false;
}
}
//get a custom field from a WordPress Pod:
public static function GetPodMeta($podType, $podSlug, $metaName, $isSingle = true) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return get_post_meta($pod["ID"], $metaName, $isSingle);
} else {
return false;
}
}
}
要使用具有静态方法的类,请执行以下操作:
在你想使用的php文件中包含PodUtils.php文件:
require_once ABSPATH . 'wp-content/plugins/podutils/PodUtils.php';
获取内容:
$strPodContent = PodUtils::GetPodContent('pod-type', 'pod-item-slug');
获取元(您添加到 pod 类型的自定义字段):
$strPodMeta = PodUtils::GetPodMeta('pod-type', 'pod-item-slug', 'custom-meta-name');
这可以很容易地更改为您使用公共函数实例化的对象,或者它自己的插件。