Richard
Richard

Reputation: 190

Custom Fields Not Displaing on Category or Archive Page

I have set up an Advanced Custom Field item to display a featured image, description etc at the top of each Category & Archive of a custom post type. Image Attached

The Custom Post Type & Taxonomy are

$event_args = array(
    'has_archive'           => 'news-events',
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $event_rewrite,
    'capability_type'       => 'post',
);
register_post_type( 'events', $event_args );
register_taxonomy( 'event_category', array( 'events' ), $events_args );

In archive-events.php I have

$context['pagination'] = Timber::get_pagination();
$context['title'] = 'Archive Tours';
$context['posts'] = Timber::get_posts();
$content['events'] = Timber::get_terms('event_category');
Timber::render($templates, $context);

In the Twig file

<div class="col-sm-12 col-md-12">
  <article class="post post-container">
    <div class="entry-header">
      <div class="post-thumb thumb">
        {% if events.featured_event_image %}
          <img src="{{TimberImage(events.featured_event_image).src}}"  alt="{{ events.featured_event_title }}"
               class="img-responsive" />
        {% else %}
          {# TODO: replace this with something we serve. #}
          <img src="http://placehold.it/500x340" alt="Default Thumbnail" class="img-responsive">
        {% endif %}
      </div>
    </div>
    <div class="sa_blog_content">
      <div class="sa_blog_center">
        <h4 class="sa_blog_title"><a target="_self" title="{{ events.featured_event_title }}" href="#">{{
            events.featured_event_title }}</a></h4>
        <div class="meta">
          <span><time datetime="{{post.post_date|date('F j, Y')}}">{{post.post_date|date('F j, Y')}}</time></span>
          <span>359 Views</span>
        </div>
      </div>
      {{ events.featured_event_summary }}
      <div class="tagcloud">
        {% for term in post.terms('featured_event_tags') %}
          <span><a href="{{post.term.link}}" title="{{post.term.name}}">{{post.term.name}}</a></span>
        {% endfor %}
      </div>
    </div>
  </article>
</div>

Yet none of my Custome Fields are available in the Archive or Category pages. Can anyone suggest a solution to this problem. Thanks in advance...

Upvotes: 1

Views: 571

Answers (1)

Gchtr
Gchtr

Reputation: 2794

When you use Timber::get_terms(), you’ll get an array in return.

Instead of checking for the existence of a custom field with {% if events.featured_event_image %} you probably have to loop over the elements in events:

{% for event in events %}
    {% if event.featured_event_image %}
        {# your code #}
    {% endif %}
{% endfor %}

Or you can directly access the first element of the array with

{% if event[0].featured_event_image %}

In the same way, {{ events.featured_event_summary }} will not show up, because the field featured_event_summary can’t be found on the array, but probably exists in each of the elements of the array.


When you use {% for term in post.terms('featured_event_tags') %}, then you ask to get all terms of a featured_event_tags taxonomy assigned to that post. From your code excerpts I can see that you only register event_category as a taxonomy. In you screenshots I can see that featured_event_tags is the name of a custom field set on the taxonomy event_category.

To get the terms for a post, you then should do:

{% for term in post.terms('event_category') %}

Inside this for loop, you seem to trying to get the link and name for that term, but you access it from post with {{ post.term.link }}. Probably, it should be

{% for term in post.terms('event_category') %}
    {{ term.link }}

{{ post.term }} won’t work, because term() is not a method of Timber’s Post class. There’s only the terms() method, which will get you all terms assigned to a post.


These could all be reasons why nothing will show up in your code. Remember that you can also do {{ dump( variable ) }} to check the contents of a variable in Twig.

If you still get stuck, then update your question with more code and I can update this answer.

Upvotes: 1

Related Questions