Timber,Twig:让作者在作者档案页面上展示

时间:2017-03-05 21:27:25

标签: timber

我在我的函数文件中添加了co authors网站提供的功能posted_on。普通作者和合着者出现在单个页面上,但我似乎无法让它在作者档案页面上工作。当我点击共同作者的链接时,它会转到合作者的正确网址,但显示的内容是针对普通作者的。我看过共同作者的github问题,但对如何在我的树枝上实现共同作者感到困惑。

我确定$ context需要一些东西,但我真的不确定要添加什么。

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 %}

1 个答案:

答案 0 :(得分:0)

将插件集成到Timber和Twig中并不总是很容易,因为许多插件都依赖于The Loop,它基本上只适用于显示单个对象的模板文件。

通常我们需要手动将当前帖子的ID传递给一个函数。为此,函数需要有可能将ID作为参数传递。有了Co-Authors插件,你很幸运。有一个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 );

Timber具有作者功能,但author属性仅在帖子对象上设置,而不在归档页面上设置。我们可以使用get_queried_object()函数,该函数为作者存档页面上的当前帖子返回WP_User对象。我们将其转换为Timber\User对象,以便在模板中更方便,更通用。

对于帖子,我们查询默认帖子,没有任何参数。这样,Timber使用归档页面的默认值。传递参数时,就像在

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

然后Timber将仅针对您设置的两个参数进行查询,忽略设置为获取作者帖子的查询参数。假设您将访问没有自己帖子的作者的存档,但只是其他帖子的共同作者,那么您不会在该存档中看到该作者的任何帖子,因为Co-Authors插件会挂钩到默认查询

要使用现有参数并使用我们自己的参数覆盖它们以便稍后在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 );

<强> author.twig

<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())可用于调用Twig中通常不可用的函数。我们传入当前帖子的帖子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。通过仍然使用它,您将使具有辅助技术(如屏幕阅读器)的用户更难以阅读您的页面。