简体   繁体   中英

Get post archive on author page with Twig / Timber

This is the current setup I have:

  1. I have a Wordpress site.
  2. I use Twig as the template engine.
  3. I use the Advance Custom Field plugin to create custom field groups.
  4. I have registered a custom post type called People.
  5. The custom post type People has an ACF group assigned to it.
  6. I have default Wordpress posts that I render under the blog section.
  7. Each blog post has an ACF group assigned to it.
  8. The ACF group assigned to the post type Posts contains a relationship field that links this group with the People group, so that a blog post can display one or many "authors" (entries from the custom post type People).

I am able to access individually each person (entries from the custom post type People). And what I would like to do is from that view, be able to also have access to all blog post entries and filter by the relationship field to display only the blog posts written by that person.

Any idea on how to do this with Twig? Once I am on the People "context" I can't find the way to get access to the blog posts "context" as well.

Usually you would need to create a custom query for this which you would add in single-people.php :

    // an author's blog articles
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => '9',
      'orderby' => 'date',
      'order'=> 'DESC',
      'post_status' => 'publish',
      'meta_query' => array(
        array(
          'key' => 'people', // name of custom field
          'value' => '"' . get_the_ID() . '"',
          'compare' => 'LIKE'
        )
      )
    );
    $context['blogs'] = Timber::get_posts( $args );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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