簡體   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