【问题标题】:Wordpress PODS display content from a postWordpress PODS 显示帖子中的内容
【发布时间】:2019-07-11 21:32:27
【问题描述】:

我最近开始使用 Wordpress PODS 插件,但在显示一些基本内容时遇到了一些问题。我可以使用以下内容很好地显示自定义字段内容:

<?php
$field_name = get_post_meta( get_the_ID(), ‘my_field_name’, true );
echo $field_name; ?>

但是我无法获得以下基本内容:

  • 标题(在普通帖子中只是the_title();
  • 内容(在常规帖子中只是the_content();
  • 特色图片

谁能帮我弄清楚如何从 POD 中提取标题、内容和特色图片?

【问题讨论】:

  • 5 年后在 Pods 网站上仍然没有一个简单的示例,这让我很疯狂。

标签: wordpress


【解决方案1】:

pod-type-slug 应替换为您的 pod 类型的 slug。 pod-slug 是您创建的特定 pod 内容的 slug:

//Pods->display has been deprecated since Pods version 2.0 with no alternative available according to a warning when trying to use this function:
$pod = pods('pod-type-slug', "pod-slug");

$pod->display('title')
$pod->display('post_content')

等等

您也可以使用默认的 WordPress 功能:

$pod = pods('pod-type-slug', "pod-slug");

$row = $pod->row();

//print the pod content:
echo $row->post_content;

get_the_post_thumbnail($row['ID']);

【讨论】:

  • 您没有在此示例中定义 $pod 变量。
  • Pods->display 自 Pods 2.0 版以来已弃用,没有其他可用的替代方案。
【解决方案2】:

在谷歌搜索和查看他们的示例后,似乎没有一个单一的来源可以获取 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');

这可以很容易地更改为您使用公共函数实例化的对象,或者它自己的插件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 2017-12-04
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多