Wordpress PODS显示帖子中的内容

时间:2014-02-23 22:50:32

标签: wordpress

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

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

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

  • 标题(常规帖子只有the_title();
  • 内容(常规帖子只有the_content();
  • 特色图片

有人可以帮我弄清楚如何从POD中提取标题,内容和精选图片吗?

2 个答案:

答案 0 :(得分:1)

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

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

$row = $pod->row();
get_the_post_thumbnail($row['ID']);

答案 1 :(得分:0)

经过一个令人沮丧的谷歌搜索并查看了他们的示例之后,似乎没有一个单一的来源可以获取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');

获取元数据(您添加到窗格类型的自定义字段):

$strPodMeta = PodUtils::GetPodMeta('pod-type', 'pod-item-slug', 'custom-meta-name');

可以很容易地将其更改为使用公共功能实例化的对象,或者它自己的插件。