如何在Timber中设置自定义帖子类型和ACF字段

时间:2019-09-06 19:13:34

标签: wordpress twig advanced-custom-fields timber

我是Timber / Twig / PHP的新手,并且有相当明确的要求。 我有一个page-home.php / page-home.twig,其中显示了几个ACF字段,作为自定义帖子类型的推荐和最新的3个博客帖子。

我对Twig前端部分的感觉很好,但是在设置php模板文件时遇到了困难。

我阅读了Timber文档,并得到了ACF嵌入的印象,因此可以直接在Twig中调用该字段。还检查了Github和SO的类似问题,但没有找到答案。 youtube上有一段视频介绍了自定义帖子类型,但该视频仅包含一个帖子。

这是我的page-home.php

echo 'page-home.php';

$context = Timber::get_context();
$context['page'] = new Timber\Post();

$templates = array('page.twig');
if (is_front_page()){

    $context['posts'] = Timber::get_posts('post_type=post&posts_per_page=3');
    $context['testimonials'] = Timber::get_posts('post_type=testimonials');

    // add your twig view to the front of the templates array
    array_unshift($templates, 'page-home.twig');
}

Timber::render( $templates, $context );


$image = get_field('hero_image');

echo '<pre>';
    var_dump( $image );
echo '</pre>';

page-home.php按预期显示了3条博客文章,但返回了ACF字段var_dump的NULL值。

如果我对此行发表评论 // $context['posts'] = new Timber\PostQuery($args); 我得到了图像数组的var_dump,尽管它没有在Twig文件中呈现。

这是page-home.twig中的图像 <div class="image image-overlay" style="background-image:url({{ Image(post.meta('hero_image')).src }})"></div>返回空白。

博客帖子被称为:

{% for post in posts %}
  {% include "blogpost-card.twig" %}
{% endfor %}

由于文档未提供该示例,因此我也不确定如何在同一页面上调用单独的自定义帖子类型(推荐)。

我正在使用通过Composer安装的启动程序主题。 如果主题或文档提供了一个包含帖子,自定义帖子和ACF字段的页面示例,那就太好了,因为我认为这是一个非常常见的情况。至少对于像我这样需要在PHP方面提供一些额外帮助的人。

2 个答案:

答案 0 :(得分:0)

对于ACF变量,您只需将其添加到functions.php中就可以了,如果我错了,有人可以纠正我。

  class StarterSite extends Timber\Site {
        /** Add timber support. */
        public function __construct() {
            //maybe you already have this part in your functions file
            add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
            add_filter( 'timber_context', array( $this, 'add_to_context' ) );
            add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
            add_action( 'init', array( $this, 'register_post_types' ) );
            add_action( 'init', array( $this, 'register_taxonomies' ) );
            parent::__construct();
        }

        public function add_to_context( $context ) {
            //left side: define how the variable is named to grab them in the TWIG files
            //right side: grabing the ACF variable-name
            $context['site'] = $this;
            $context['menu'] = new Timber\Menu("Main Menu");
            $context['something'] = new Timber\Menu("something_acf_name");
            $context['foo'] = new Timber\Menu("bar");
            $context['my_custom_button_name'] = new Timber\Menu("acf_custom_button_name");
            // Posts with the category name "featured" for the slideshow

            //
            return $context;
        }
    }

因此,我们假设我们位于index.twig文件(或任何其他文件,只要您在functions.php文件中定义了文件),就可以像这样获取变量:

<div>
{{ foo }}
</div>

我希望这是您想要的。例如,您也可以只在index.php文件中分配变量,那么它们将不会全局可用(在每个页面上)。这可以使您的变量混乱得到控制。真的取决于您要构建的东西。

答案 1 :(得分:0)

如果Twig文件中有{{ Image(post.meta('hero_image')).src }},则Twig将寻找post变量。在您的情况下,您可以在遍历帖子以将其显示为卡片时定义一个post变量。

{% for post in posts %}
    {% include "blogpost-card.twig" %}
{% endfor %}

但是,您可能没有为帖子定义一个名为hero_image的自定义字段,而是在显示它们的页面上。因此,当您定义上下文并在上下文中的page下添加当前显示的帖子时,您以后也需要通过page而不是通过post来访问它。

$context = Timber::get_context();
$context['page'] = new Timber\Post();
<div
     class="image image-overlay"
     style="background-image:url({{ Image(page.meta('hero_image')).src }})"
></div>