【问题标题】:How to display all the posts of a specific category如何显示特定类别的所有帖子
【发布时间】:2019-06-24 18:28:47
【问题描述】:

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

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

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

{% endblock %}

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

【问题讨论】:

    标签: javascript php wordpress categories posts


    【解决方案1】:

    <?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);
    
    ?>

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 嗨!如果我只想要一个类别,它会起作用。但我想对所有类别执行此操作而不创建 200 taxonomy_name.php
      【解决方案3】:

      编辑创建帖子并从静态首页上的 ACF(“特色主题”)字段中选择 3 个类别 在首页,用户将看到三个选定的类别 在每个类别下,用户会看到与该类别相关的三个 OTHER 帖子(总共 9 个帖子)

      php 文件

      $featured_topic_ids = get_field('featured_topics');

      木材::get_terms();

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

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

      树枝文件 {% 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.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-03
        • 1970-01-01
        相关资源
        最近更新 更多