如何在 Timber for WordPress 中使用自定义 post 类型
How to use custom post types with Timber for WordPress
我正在尝试使用我创建的带有 Timber 的自定义 Post 类型。我使用 CPT UI 插件创建了自定义 post 类型。我已使用 ACF Pro 将我想要的字段分配给此自定义 post 类型。我遇到的问题是使用 Timber 将此自定义 post 类型渲染到页面上。
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
.
提前感谢您的帮助,非常感谢。
干杯,
罗伯特
您是否看过官方文档中的ACF Guide?
如果您通过 $context['social_media_links__main'] = Timber::get_posts('post_type=social_media_link');
获取 post,那么您将有一个需要循环的 post 数组。对于每个 post,您将循环遍历转发器字段。我想这可能对你有用:
树枝
{% 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 %}
但是,我的理解是否正确,您只有一个 post 用于您的自定义 post 类型,并且在该 post 中您已经为指向社交媒体的链接定义了转发器字段简介?如果你真的只有一个post,一个ACF Options Page可能更适合你想要达到的效果。
我正在尝试使用我创建的带有 Timber 的自定义 Post 类型。我使用 CPT UI 插件创建了自定义 post 类型。我已使用 ACF Pro 将我想要的字段分配给此自定义 post 类型。我遇到的问题是使用 Timber 将此自定义 post 类型渲染到页面上。
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
.
提前感谢您的帮助,非常感谢。
干杯, 罗伯特
您是否看过官方文档中的ACF Guide?
如果您通过 $context['social_media_links__main'] = Timber::get_posts('post_type=social_media_link');
获取 post,那么您将有一个需要循环的 post 数组。对于每个 post,您将循环遍历转发器字段。我想这可能对你有用:
树枝
{% 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 %}
但是,我的理解是否正确,您只有一个 post 用于您的自定义 post 类型,并且在该 post 中您已经为指向社交媒体的链接定义了转发器字段简介?如果你真的只有一个post,一个ACF Options Page可能更适合你想要达到的效果。