使用木材(树枝)按首字母显示 Wordpress 自定义帖子类型 [英] Displaying Wordpress custom post type by first letter with Timber (twig)

查看:23
本文介绍了使用木材(树枝)按首字母显示 Wordpress 自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Timber for Wordpress,我想创建一个这样的字典系统:https://wordpress.stackexchange.com/问题/119163/displaying-custom-post-type-by-first-letter-through-custom-taxonomy

I use Timber for Wordpress and I would like to create a dictionary system like that : https://wordpress.stackexchange.com/questions/119163/displaying-custom-post-type-by-first-letter-through-custom-taxonomy

我是这样写的:

lexique.php

$query = get_posts(array('post_type' => 'lexique','posts_per_page' => -1));

但我不知道如何用 Timber 转换它:

But I don't know how to transform this with Timber :

$by_letter = array();
while( $query->have_posts() ) { $query->the_post();
  global $post;
  $letter = substr($post->post_name, 0, 1);
  if ( ! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
  $by_letter[$letter][] = $post;
}
wp_reset_postdata();

推荐答案

一次获取所有帖子是个好主意.通过添加 orderby 参数,您已经可以将它们放入正确的字母排序顺序中.我会使用 title 而不是 name 进行排序和定义第一个字母,因为 name/post_name是 URL 安全字符串(用于永久链接)可能与帖子的实际标题不同.

It’s a good idea to get all posts at once. By adding the orderby parameter, you can already put them into the correct order for the letter-sorting. I’d use title and not name for the sorting and to define the first letter, because name/post_name is a URL-safe string (used in permalinks) might be different from the actual title of the post.

您通过 Timber::get_posts() 获取帖子,因此您不必依赖 The Loop.在通过 Twig 文件渲染它之前,您会获得一个包含您可以使用的帖子的数组.这比您链接的方法更直接,因为您不必依赖附加功能和查询重置.

You get the posts through Timber::get_posts(), so that you don’t have to rely on The Loop. You get an array with posts that you can work with, before you render it through a Twig file. This is much more direct than the method you linked to, because you don’t have to rely on additional functions and query resets.

lexique.php

$posts = Timber::get_posts( array(
    'post_type' => 'lexique',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
) );

$posts_by_letter = array();

// Sort posts by letter
foreach ( $posts as $post ) {
    $first_letter = substr( $post->post_title, 0, 1);

    // Create array for letter if it doesn’t exist
    if ( ! isset( $posts_by_letter[ $first_letter ] ) ) {
        $posts_by_letter[ $first_letter ] = array();
    }

    $posts_by_letter[ $first_letter ][] = $post;
}

$context['posts_by_letter'] = $posts_by_letter;

Timber::render( [ 'lexique.twig' ], $context );

仅显示现有帖子的字母

lexique.twig

<dl>
{% for letter, posts in posts_by_letter %}
    <dt>{{ letter }}</dt>

    {% for post in posts  %}
        <dd><a href="{{ post.link }}">{{ post.title }}</a></dd>
    {% endfor %}
{% endfor %}
</dl>

因为你有一个嵌套数组,所以你必须做两个 for 循环.第一个循环遍历字母(它们是外部数组的键).分配给字母键的值是另一个数组,包含以该字母开头的所有帖子.要将帖子标题显示为链接,请使用第二个 for 循环.

Because you have a nested array, you have to do two for-loops. The first loop goes through the letters (which are the keys of the outer array). The value assigned to a letter key is another array, containing all the posts starting with that letter. To display the post titles as links, you use the second for-loop.

如果您想生成从 A 到 Z 的所有字母的列表并显示现有帖子,您可以使用 range 并检查 posts_by_letter 中是否存在该字母的帖子.

If you want to generate a list of all letters from A to Z and display existing posts, you can use range and check if posts exists for that letter in posts_by_letter.

此外,您可以使用 range 创建一个锚链接列表,让访问者跳转到特定字母.

Additionally you can use range to create a list of anchor links that lets a visitor jump to a specific letter.

lexique.twig

 {# Anchor links to jump to letter #}
 {% for letter in range('A', 'Z') %}
     <a href="#{{ letter }}">{{ letter }}</a>
 {% endfor %}

 <dl>
 {% for letter in range('A', 'Z') %}
     <dt><a id="{{ letter }}">{{ letter }}</a></dt>
     {% if posts_by_letter[letter] is defined %}
         {% for post in posts_by_letter[letter]  %}
             <dd><a href="{{ post.link }}">{{ post.title }}</a></dd>
         {% endfor %}
     {% endif %}
 {% endfor %}
 </dl>

这篇关于使用木材(树枝)按首字母显示 Wordpress 自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆