繁体   English   中英

如何显示特定类别的所有帖子

[英]How to display all the posts of a specific category

我来找你是因为我在创建 WordPress 主题时遇到了问题。 我是新手,有点迷茫。 让我解释 :

我有一个页面显示我网站的每个类别的标题。 我希望当客户点击一个类别时,它被定向到另一个页面,其中包含该类别中包含的所有文章(知道文章中只有照片,没有文字)。 在 WordPress codex 上,我找到了一些信息,但这些信息不起作用,或者更有可能的是,我错过了一些东西.. 小精度,我与 Timber 合作使用树枝文件进行查看。

 Category.twig {% block content %} {% for post in posts %} <li>{{ post.content }}</li> {% endfor %} {% endblock %}

实际上,问题在于它显示了我拥有的所有消息,而不仅仅是我单击的类别中的帖子。

 <?php $args = array( 'posts_per_page' => -1, 'orderby' => 'rand', 'order' => 'ASC', 'cat' => ( get_queried_object() )->term_id ); $context = Timber::get_context(); $context['posts'] = Timber::get_posts($args); Timber::render('category.twig', $context); ?>

请在您的主题中创建一个 taxonomy-{texonomy_name}.php,您可以在主题中使用 wp_query 循环来查看您的 texonomy 术语的所有帖子。

<?php 
 if ( have_posts() ) :
        while ( have_posts() ) : the_post();
            the_title();
        endwhile;
    endif;
?>

编辑器创建帖子并从静态首页上的 ACF(“特色主题”)字段中选择 3 个类别帖子总数)

php文件

$featured_topic_ids = get_field('featured_topics');

木材::get_terms();

$context['featured_topics'] = Timber::get_terms($featured_topic_ids);

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

twig 文件{% for ft in features_topics %}

{{ ft.name }}
 {% for catpost in ft.posts(3) %} <li><a href="{{ catpost.link }}">{{ catpost.title }}</a></li> {% endfor %}

您可以点击此链接Timber Twig Show Posts Associated with Category

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM