2017-03-05 46 views
1

我在我的函數文件中添加了co作者站點提供的函數posting_on。常規作者和合作作者在單個頁面上顯示,但我似乎無法在作者檔案頁面上工作。當我點擊共同作者的鏈接時,它會轉到合作作者的正確網址,但顯示的內容是針對正常作者的。我已經看過共同作者的github問題,但對如何在我的樹枝中實現合作作者感到困惑。Timber,Twig:Getting Co作者在作者檔案頁面上顯示

我確定有一些我需要的$上下文,但我真的不確定在那裏添加什麼。

的single.php

<span class="author"><a href="{{post.author.link}}" rel="author"> 
    {{function('name_posted_on')}}</a> 
    </span> 

我有一個author.php文件

$context = Timber::get_context(); 
$args = 'post_type=post&numberposts=2'; 
$context['posts'] = Timber::get_posts($args); 

Timber::render(array('author.twig'), $context); 

在我author.twig文件:

<div class="col-md-12"> 

    //I need the author or co author to be displayed in the h3 and their posts in the loop 

    <h3>Latest From {{posts.author.first_name}} {{posts.author.last_name}}</h3> 
</div> 
{% for post in posts %} 

    <article class="latest-article-widget col-md-6"> 
     {% block author %} 


      {% if post.get_thumbnail %} 
       <img src="{{post.thumbnail.src}}" class="img-responsive" /> 
      {% endif %} 

      <div class="author"> 
       <span>By:</span> 
       <a href="" rel="author">{{post.author.first_name}} {{post.author.last_name}}</a> 
      </div> 
      <h1><a href="{{post.link}}">{{post.title}}</a></h1> 
      {% for item in post.get_field('post_or_page_content') %} 
      <p>{{item.standard_text_content|excerpt(25)}} <a href="{{post.link}}"></a></p> 

      {% endfor %} 
     {% endblock %} 
    </article> 
{% endfor %} 

回答

0

這並不總是很容易能集成插入到Timber和Twig中,因爲許多插件都依賴於The Loop,它基本上只適用於顯示一個si的模板文件討厭的對象。

通常我們需要手動將當前帖子的ID傳遞給一個函數。爲此,一個函數需要有可能將ID作爲參數傳遞。使用共同作者插件,你很幸運。有一個get_coauthors函數,該函數接受一個帖子ID以返回所有作者,包括帖子的合作者。該功能可能未被記錄,但是由插件內部使用。

讓我們過目一下你如何能夠使這項工作一步一步:

author.php

<?php 

$context = Timber::get_context(); 

$context['author'] = new \Timber\User(get_queried_object()); 
$context['posts'] = Timber::get_posts(); 

Timber::render(array('author.twig'), $context); 

木材有一個作者的功能,而是一個author屬性只在後置對象,不在歸檔頁面上。我們可以使用get_queried_object()函數,該函數返回作者歸檔頁面上當前帖子的WP_User對象。我們將其轉換爲Timber\User對象,以便在模板中更方便,更通用。

對於文章,我們查詢默認文章,沒有任何參數。這樣,Timber使用歸檔頁面的默認值。當您在參數傳遞,就像你做

$context['posts'] = Timber::get_posts('post_type=post&numberposts=2'); 

然後木材會做一個查詢只爲您所設置的兩個參數,忽略設置,以獲得作者的帖子查詢參數。假設您將訪問一個沒有自己的帖子,但只是其他帖子的合着者的作者檔案,那麼您不會在該作者的檔案中看到任何帖子,因爲共同作者插件掛鉤到默認查詢中。

要使用現有參數並用我們自己的參數覆蓋它們以便稍後在Timber::get_posts()中使用它們,我們可以使用wp_parse_args。我們將我們的新參數作爲第一個參數,並將主查詢的查詢變量作爲默認參數進行覆蓋。

<?php 

global $wp_query; 

// Set query args before posts are queried in get_context() 
$args = wp_parse_args(array(
    'posts_per_page' => 2, 
), $wp_query->query_vars); 

$context = Timber::get_context(); 

$context['author'] = new \Timber\User(get_queried_object()); 
$context['posts'] = Timber::get_posts($args); 

Timber::render(array('author.twig'), $context); 

作者。小枝

<div class="col-md-12"> 
    <h3>Latest From {{ author.first_name }} {{ author.last_name }}</h3> 
</div> 

{% for post in posts %} 
    <article class="latest-article-widget col-md-6"> 
     {% block author %} 
      {% if post.get_thumbnail %} 
       <img src="{{ post.thumbnail.src }}" class="img-responsive" /> 
      {% endif %} 

      <div class="author"> 
       <span>By:</span> 
       {% set authors = fn('get_coauthors', post.id) %} 

       {% for author in authors %} 
        {% set author = TimberUser(author) %} 

        <a href="{{ author.link }}" rel="author"> 
         {{ author.first_name }} {{ author.last_name }} 
        </a>{% if not loop.last %}, {% endif %} 
       {% endfor %} 
      </div> 

      <h1><a href="{{ post.link }}">{{ post.title }}</a></h1> 
      {% for item in post.get_field('post_or_page_content') %} 
       <p>{{ item.standard_text_content|excerpt(25) }} <a href="{{  post.link }}"></a></p> 
      {% endfor %} 
     {% endblock %} 
    </article> 
{% endfor %} 

現在get_coauthors進場。在for循環每篇文章裏面,我們可以使用

{% set authors = fn('get_coauthors', post.id) %} 

fn速記(或function())可以被用來調用通常不提供嫩枝功能。我們傳遞當前帖子的帖子ID,以獲取該帖子的所有作者(默認作者加合作者)。

然後我們遍歷這些作者:

{% for author in authors %} 
    {% set author = TimberUser(author) %} 

    <a href="{{ author.link }}" rel="author"> 
     {{ author.first_name }} {{ author.last_name }} 
    </a>{% if not loop.last %}, {% endif %} 
{% endfor %} 

爲了能夠獲得鏈接每個作者,我們把每一個成Timber\User對象。

我希望這是你所需要的。

有關輔助

小記我看到你使用<h1>的文章標題。您可能試圖實施文檔大綱。雖然有很多博客文章告訴你如何做到這一點,Document Outline is not implemented in any browser yet。通過仍然使用它,您使輔助技術(如屏幕閱讀器)的用戶更難以閱讀您的頁面。

+0

啊呀。我從另一個開發者繼承了這個項目。我假設他們應該在文章元素中使用標題元素,然後添加標題標籤。據我瞭解。像section> header> h1 – lnickel

+0

@lnickel這沒關係。在頭文件中嵌套H1也是文檔模型的一部分。解決方法是根據文檔模型不編碼。這基本上意味着你應該像我們以前那樣做。就標題而言,它意味着每頁僅使用一個H1,而不是跳過標題級別並嵌套標題,以便在僅查看標題(如輔助技術的人員)時提供頁面結構。 – Gchtr

+0

Bueno!感謝您的澄清! – lnickel

相關問題