title: Passing a post ID to a Twig/Timber function
body: <p>How do I pass a post ID to a Twig/Timber function like edit_post_link?</p>
  
  <p>Reading the docs at <a href="https://timber.github.io/docs/guides/functions/#function-with-arguments" rel="nofollow noreferrer">https://timber.github.io/docs/guides/functions/#function-with-arguments</a></p>
  
  <blockquote>
    <p>A function like edit_post_link will try to guess the ID of the post
    you want to edit from the current post in The Loop. the same function
    requires some modification in a file like archive.twig or index.twig.
    There, you will need to explicitly pass the post ID.</p>
  </blockquote>
  
  <p>And that is what happens; when I use this</p>
  
  <p><code>{{ function('edit_post_link', 'Edit', '&lt;span class="edit-link"&gt;', '&lt;/span&gt;', post.ID) }}</code></p>
  
  <p>in <code>index.twig</code>, all the edit links have the post ID of the page that displays the loop of custom post types, not the post ID of each custom post type that is in the loop.</p>
  
  <p>I'm using the function below in functions.php, which also forces a <code>target="_blank"</code> on edit links:</p>
  
  <pre><code>add_filter( 'edit_post_link', 'newwindow_edit_post_link', 10, 3 );
  
  global $post;
  $post_id = $post-&gt;ID;
  
      function newwindow_edit_post_link( $link, $post_id, $text ) {
          if( !is_admin() )
              $link = str_replace( '&lt;a ', '&lt;a target="_blank" ', $link );
          return $link;
      }
  </code></pre>
  
  <p>This is the basic loop on <code>index.twig</code>. "people" is a standard WordPress custom post type:</p>
  
  <pre><code> {% if people %}
  
              {% for person in people %}
  
                      &lt;a href="{{ person.link }}"&gt;{{ person.name }}&lt;/a&gt;
  
                          {{ function('edit_post_link', 'Edit', '&lt;span class="edit-link"&gt;', '&lt;/span&gt;', post.ID) }}
  
              {% endfor %}
  
      {% else %}
  
   {% endif %}
  </code></pre>
  
  <p>That results in all of the edit links pointing to that page, not each custom  post type "person."</p>
  
  <p>So how do I call the post ID? Do I need to call the post ID in the custom Post Type function?</p>
  
  <p>The main index.php file has standard Twig functions:</p>
  
  <pre><code>$context = Timber::get_context();
  $context['posts'] = Timber::get_posts();
  $templates = array( 'index.twig' );
  Timber::render( $templates, $context );
  </code></pre>