简体   繁体   English

使用TIMBER在自定义帖子类型存档页面上显示SELECT字段

[英]Displaying SELECT field on custom post type archive page using TIMBER

I have a custom post type, event, that has a SELECT field that grabs the Post Object of another custom post type, listing. 我有一个自定义帖子类型,事件,它具有一个SELECT字段,该字段可捕获另一种自定义帖子类型(列表)的Post Object。 So we have an event and we want to select a business listing that it's attached to it so we can display the business's contact information (phone, address, etc). 因此,我们有一个事件,我们想选择一个与其相关的公司列表,以便我们可以显示公司的联系信息(电话,地址等)。

functions.php functions.php

function register_post_types() {
    register_post_type( 'swfl_listing', array(
        'labels'             => array(
            'name'          => __( 'Listings' ),
            'singular_name' => __( 'Listing' )
        ),
        'rewrite'         => array(
            'slug'       => 'destinations/listings',
        ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'capability_type'    => 'post',
        'has_archive'        => true,
        'supports'           => array( 'title', 'thumbnail', 'editor', 'revisions', 'author', 'comments' ),
        'menu_icon'          => 'dashicons-businessman',
    ) );

    register_post_type( 'swfl_event', array(
        'labels'             => array(
            'name'          => 'Events',
            'singular_name' => 'Event',
        ),
        'rewrite'         => array(
            'slug'       => 'discover/events',
        ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'capability_type'    => 'post',
        'has_archive'        => true,
        'supports'           => array( 'title', 'thumbnail', 'editor', 'revisions', 'comments' ),
        'menu_icon'          => 'dashicons-calendar-alt',
    ) );
}

function add_to_context( $context ) {
    ...
    $context['events'] = Timber::get_posts( array(
        'post_type'      => 'swfl_event',
    ) );
    ...
}

I searched and found this line of code that allows me to grab the information on the Singular page listing_sponsoring_event being the custom field. 我搜索并找到了这行代码,它使我可以在作为自定义字段的单数页面listing_sponsoring_event上获取信息。

single.php & archive.php single.php和archive.php

...
$context['listing'] = new TimberPost( $post->listing_sponsoring_event );
...

Here is working code on the singular page 这是单页上的工作代码

single-swfl_event.twig single-swfl_event.twig

...
<strong>Where: </strong> {{ listing.title }} <br/>
<strong>Price: </strong> {{ post.get_field('event_price') }} <br/>
...

But the same code does not work if I put it into archive.php. 但是,如果我将其放入archive.php,则无法使用相同的代码。 It just shows the last post information in the loop. 它仅显示循环中的最新帖子信息。

archive-swfl_event.twig archive-swfl_event.twig

...
{% for post in posts %}
    ...
    <strong>Where: </strong> {{ listing.title }} <br/>
    <strong>Price: </strong> {{ post.get_field('event_price') }} <br/>
    ...
{% endfor %}
...

Edit: I haven't been able to figure out anything using Timber/Twig (nor a response), but I'm leaving this question open just in case it gets figured out eventually. 编辑:我无法使用Timber / Twig(也没有响应)找出任何问题,但是我将这个问题保留为开放状态,以防万一最终找到答案。 For now I am using a function with PHP to grab the information. 现在,我正在使用PHP函数来获取信息。 It's not a pretty solution. 这不是一个很好的解决方案。

Hopefully Timber will eventually be updated to support SELECT and WP Post objects more. 希望最终将更新Timber以支持SELECT和WP Post对象。

So my problem was really just with working with WP Post Objects and how to get it's information. 所以我的问题实际上只是与WP Post Objects一起使用以及如何获取其信息。 For any of you who also have problems: 对于任何还有问题的人:

Instead of {{ post.custom_field_name }} , access the object information with {{ post.get_field('custom_field_name') }} . 代替{{ post.custom_field_name }} ,使用{{ post.get_field('custom_field_name') }}访问对象信息。 {{ post.custom_field_name }} only gives me the ID of the Object. {{ post.custom_field_name }}只给我对象的ID。

For troubleshooting, call {{ post.get_field('custom_field_name')|print_r }} for a list of data you can retrieve. 要进行故障排除,请致电{{ post.get_field('custom_field_name')|print_r }}获取可检索的数据列表。

Now I can grab the data I want with {{ post.get_field('custom_field_name').post_title }} . 现在,我可以使用{{ post.get_field('custom_field_name').post_title }}获取所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM