如何使用Timber for WordPress自定义帖子类型

时间:2018-02-19 17:51:48

标签: wordpress twig timber

我正在尝试使用我创建的Timber自定义帖子类型。我使用CPT UI插件创建了自定义帖子类型。我已使用ACF Pro为此自定义帖子类型指定了我想要的字段。我遇到的麻烦是使用Timber将此自定义帖子类型渲染到页面上。

functions.php档案:

function add_to_context( $context ) {
        $context['foo'] = 'bar';
        $context['stuff'] = 'I am a value set in your functions.php file';
        $context['notes'] = 'These values are available everytime you call Timber::get_context();';
        $context['menu'] = new TimberMenu();
        $context['site'] = $this;
        // Social Media Links CPT (Main)
        $context['social_media_links__main'] = Timber::get_posts('post_type=social_media_link');
        return $context;
    }

如果我这样做在我的页面上:

{{dump(social_media_links__main)}}

我会得到这个:

class Timber\Post#1092 (58) {
    public $ImageClass =>
    string(12) "Timber\Image"
    public $PostClass =>
    string(11) "Timber\Post"
    public $TermClass =>
    string(11) "Timber\Term"
    public $object_type =>
    string(4) "post"
    public $custom =>
    array(20) {
      '_edit_last' =>
      string(1) "1"
      'social_media_links_0_social_media_url' =>
      string(7) "youtube"
      '_social_media_links_0_social_media_url' =>
      string(19) "field_5a8ad0ba8f0db"
      'social_media_links_0_social_media_icon' =>
      string(14) "fab fa-youtube"
      '_social_media_links_0_social_media_icon' =>
      string(19) "field_5a8ad0d68f0dc"
      'social_media_links' =>
      string(1) "4"
      '_social_media_links' =>
      string(19) "field_5a8ad08b8f0da"
      '_edit_lock' =>
      string(12) "1519061667:1"
      'social_media_links_1_social_media_url' =>
      string(7) "twitter"
      '_social_media_links_1_social_media_url' =>
      string(19) "field_5a8ad0ba8f0db"
      'social_media_links_1_social_media_icon' =>
      string(14) "fab fa-twitter"
      '_social_media_links_1_social_media_icon' =>
      string(19) "field_5a8ad0d68f0dc"
      'social_media_links_2_social_media_url' =>
      string(8) "linkedin"
      '_social_media_links_2_social_media_url' =>
      string(19) "field_5a8ad0ba8f0db"
      'social_media_links_2_social_media_icon' =>
      string(18) "fab fa-linkedin-in"
      '_social_media_links_2_social_media_icon' =>
      string(19) "field_5a8ad0d68f0dc"
      'social_media_links_3_social_media_url' =>
      string(8) "facebook"
      '_social_media_links_3_social_media_url' =>
      string(19) "field_5a8ad0ba8f0db"
      'social_media_links_3_social_media_icon' =>
      string(17) "fab fa-facebook-f"
      '_social_media_links_3_social_media_icon' =>
      string(19) "field_5a8ad0d68f0dc"
    }

我可以看到我需要的内容,但是我无法找出正确的语法来循环并检索我需要的数据。我在ACF中使用转发器字段,我需要的字段称为social_media_url& social_media_icon

提前感谢您的帮助,我非常感谢。

干杯, 罗伯特

1 个答案:

答案 0 :(得分:2)

您是否已查看官方文档中的ACF Guide

如果您通过$context['social_media_links__main'] = Timber::get_posts('post_type=social_media_link');收到帖子,那么您将需要循环播放一系列帖子。对于每个帖子,您将遍历转发器字段。我想这可能适合你:

<强>枝条

{% for post in social_media_links__main %}
    {% for link in post.meta('social_media_links') %}
        <a href="{{ link.social_media_url }}">
            <span class="{{ link.social_media_icon }}"></span>
        </a>
    {% endfor %}
{% endfor %}

但是,我是否理解您的自定义帖子类型只有一个帖子,并且在该帖子中您为社交媒体配置文件的链接定义了转发器字段?如果它只是您正在使用的一个帖子,ACF Options Page可能更适合您想要实现的目标。