【问题标题】:How do I display posts from most ''recent custom taxonomy'' dynamically in WordPress?如何在 WordPress 中动态显示来自大多数“最近的自定义分类”的帖子?
【发布时间】:2020-04-20 04:44:21
【问题描述】:

我目前正在开发一个 WordPress 期刊主题,我想知道最好的方法

1. 根据卷数和问题(例如,第 1 卷第 1 卷、第 1 卷第 2 卷或第 1 卷第 3 卷)对文章(自定义帖子类型)进行排序。
2. 在首页显示当前期(如第一卷第三期)

到目前为止我做了什么
1. 我使用链接在一起的 ACF 创建了一个自定义分类(卷和问题)和一个自定义字段类型(分类)。使用下面的代码,我能够显示分类中的所有帖子,无论它们是在第 1 卷第 1 卷、第 1 卷第 2 卷还是第 1 卷第 3 卷中。

       <?php

  $homepageArticles = new WP_Query(array(
        'posts_per_page'        =>  -1,
        'post_type'             =>  'article',
        'meta_key'              =>  'current_issue',
        'orderby'               =>  'meta_value',

   ));

    while($homepageArticles->have_posts()){
        $homepageArticles->the_post(); ?>
    <div class="j-header shadow">

                <div class= "j-category"> <?php the_field('article_category'); ?>
        </div>

                    <div class="j-section">
                        <div class="j-article-thumbnail ">
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a>
                        </div>
                        <div class="j-article-excerpts jp-excerpt ">
                            <div class="j-title">
                               <h1 class='j-title'><a href="<?php the_permalink(); ?>"> 
                                    <?php the_title(); ?>
                                   </a></h1>

                            </div>
                            <div class="j-authors"> <span>By: </span>     
                           <?php the_field('authors'); ?></div>
                            <div id="mini-navbar">
                                <ul class="mini-navbar">
                                    <li><a href="#">Abstract</a></li>
                                    <li><a href="<?php the_permalink(); ?>">HTML Full Text</a></li>
                                    <li><a href="<?php the_field('uplaod_pdf'); ?>">PDF</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix"></div>
                </div>


<?php
    } wp_reset_postdata();
?>
  1. 我还尝试使用第二个代码使用 meta_query 过滤帖子,但是当我需要比较我创建的变量时我感到困惑`

    $currentIssue = get_field('current_issue');
    $pastIssue = get_field('current_issue');
    

    $homepageArticles = new WP_Query(array( 'posts_per_page' => -1, 'post_type' => '文章', 'meta_key' => 'current_issue', 'orderby' => 'meta_value', 'meta_query' => 数组( 大批( 'key' => $currentIssue, '比较' => '>', '价值' => $pastIssue )
    )

    ));
    
    while($homepageArticles->have_posts()){
        $homepageArticles->the_post(); ?>
    <div class="j-header shadow">
    
                <div class= "j-category"> <?php the_field('article_category'); ?>
        </div>
    
                    <div class="j-section">
                        <div class="j-article-thumbnail ">
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a>
                        </div>
                        <div class="j-article-excerpts jp-excerpt ">
                            <div class="j-title">
                               <h1 class='j-title'><a href="<?php the_permalink(); ?>"> 
                                    <?php the_title(); ?>
                                   </a></h1>
    
                            </div>
                            <div class="j-authors"> <span>By: </span>     
                           <?php the_field('authors'); ?></div>
                            <div id="mini-navbar">
                                <ul class="mini-navbar">
                                    <li><a href="#">Abstract</a></li>
                                    <li><a href="<?php the_permalink(); ?>">HTML Full Text</a></li>
                                    <li><a href="<?php the_field('uplaod_pdf'); ?>">PDF</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
    
                    <div class="clearfix"></div>
                </div>
    

    `

我也想知道是否有更好的方法来对 WordPress 中基于卷和问题的文章进行排序

【问题讨论】:

    标签: wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:
      $homepageArticles = new WP_Query(array(
        'posts_per_page'        =>  -1,
        'post_type'             =>  'article',
        'orderby'               =>  'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'fabric_building_types', //Change taxonomy name*/
                'field' => 'term_id', 
                'terms' => $cat->term_id, // Change it with terms it 
            )
        )
    

    ));
    试试这个

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多