Skip to content

Instantly share code, notes, and snippets.

@jarednova
Last active July 13, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarednova/6306689 to your computer and use it in GitHub Desktop.
Save jarednova/6306689 to your computer and use it in GitHub Desktop.
### Let's maximize best practice. From my perspective this means making objects out of our Article so we can handle they byline permutations:
<?php
/* client-article.php */
class ClientArticle extends TimberPost {
function writers(){
return $this->get_terms('writers', true, 'ClientWriter');
}
function writer(){
$writers = $this->writers();
return $writers[0];
}
}
<?php
class ClientWriter extends TimberTerm {
function get_title(){
$now = get_the_tiem('U');
foreach($titles as $title){
if (strtotime($title['start_date']) < $now && strtotime($title['end_date']) > $now){
return $title;
}
}
}
}
<?php
/* single.php */
require_once('client-article.php');
require_once('client-writer.php');
$data = Timber::get_context();
$data['post'] = new ClientArticle();
Timber::render('single.twig', $data);
<div class="meta-byline source">By
{% for writer in post.writers %}
<a href="{{writer.link}}" class="writer-name">{{writer.name}}</a>
{% endfor %}
{% if post.writers|length == 1 %}
<span class="writer-title">{{post.writer.title}}</span>
{% else %}
<span class="writer-title">Staff Writer</span>
{% endif %}
</div>
<?php
/* I'm being as efficent as possible -- I'm assuming that this would reference PHP files as well that don't extend Timber */
<div class="meta-byline source">By
<?php foreach($post->writers() as $writer){ ?>
<a href="<?= $writer->link(); ?>" class="writer-name"><?= esc_attr($writer->name()); ?></a>
<?php } ?>
<?php if (count($post->writers()) == 1): ?>
<span class="writer-title"><?= esc_attr($post->writer()->title); ?></span>
<?php else: ?>
<span class="writer-title">Staff Writer</span>
<?php endif; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment