【问题标题】:Wordpress Custom Post Type Archive page with Drop down Taxonomyes带有下拉分类的 Wordpress 自定义帖子类型存档页面
【发布时间】:2016-04-01 16:35:39
【问题描述】:

我正在为我的论文开发一个拉丁文学数字图书馆。 现在我正在作者的存档页面上工作。 作者是注册为“auctores”的自定义帖子类型,并为其分配了一个名为“periodi”的自定义分类。 在存档页面(archive-auctores.php)中,我想显示由元字段和其他三列列出的所有作者:时期(指的是我想制作过滤器的自定义分类法),数字of books(应该有分配给该作者的图书数量(cpt))和 Gener(在哪里显示分配给该作者的图书的所有文学体裁,这将是通过 cpt-分配给图书 cpt 的另一种分类法-词组插件)。数字列应该是可排序的(asc/desc),而我想为句点和种类制作一个过滤器。 IE。 --> 打开存档页面,它将显示完整的作者列表。因此,应该可以过滤作者的时期,并且页面应该只显示使用该特定分类术语“标记”的作者。

我以为我找到了解决方案 here,但是当我尝试在下拉列表中选择一个术语时,列表保持不变。

我肯定错过了什么。 这就是我现在模板的代码:

<form method="post" action="<?php the_permalink()?>">
  <select name="periodi" id="selectperiodo" class="postform" onchange="submit();">
    <option value="">Tutti i periodi</option>
       <?php
       $args = array(
       'orderby'                => 'ID',
       'order'                  => 'ASC',
       'hide_empty'             => false,
       'fields'                 => 'all',
       'hierarchical'           => true,
       'pad_counts'             => false,
       'get'                    => '',
       'child_of'               => 0,
       'parent'                 => '',
       'childless'              => false,
       'cache_domain'           => 'core',
       'update_term_meta_cache' => true,
       'meta_query'             => '',
       'parent'                  => 0

     ); 
       $terms = get_terms('periodi', $args);
        if ( $terms ) {
            foreach ( $terms as $term ) {?>
     <option <?php if($term->slug == $_POST['periodi']){ echo 'selected="selected"';} ?> value="<?php echo esc_attr( $term->slug )?>"><?php echo esc_html( $term->name ) ?></option>
<?php }
        }
?>
   </select>
</form>

<table class="dataTable table table-hover">
<tr>
  <th>Nome</th>
  <th>Periodo</th>
  <th>Opere</th>
  <th>Genere</th>
</tr>
<?php $auctores_query = new WP_Query(array( 
                                    'post_type' => 'auctores', 
                                    'posts_per_page' => -1,
                                    'order'     => 'ASC',
                                    'meta_key' => 'nome_classico',
                                    'orderby'   => 'meta_value',
                                    )
                                   ); ?>
<?php $j = 0 ?>
<?php while ($auctores_query->have_posts()) :     $auctores_query->the_post(); ?>
<?php $additional_class = (++$j % 2 == 0) ? 'even' : 'odd'; ?>
<tr class="<?php echo $additional_class ?>">
  <td><?php the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( sprintf( __( 'Collegamento alla pagina di %s', 'antiquarialatina' ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark">', '</a></h3>' )?></td>
  <td>
    <?php
    $terms = get_the_terms( $post->ID, 'periodi' );
    if ( $terms && ! is_wp_error( $terms ) ) :      
        $periodi_links = array();  
        foreach ( $terms as $term ) {
            $periodi_links[] = $term->name;
        }                               
        $on_draught = join( ", ", $periodi_links );
    ?>
    <?php echo $on_draught; ?>
    <?php endif; ?>
  </td>
  <td><span class="badge"><?php 
$args = array('post_type' => 'texti',
'tax_query' => array (
  array ( 'taxonomy' => 'auctores',
     'field'    => 'id',
     'terms'    => get_the_ID()
  )
));
$query = new WP_Query( $args );               
// the query

echo $query->found_posts;

    ?></span></td>
</tr>
<?php endwhile; ?>
</table>

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 您的$auctores_query 不会尝试根据您描述的元字段进行过滤。需要对其进行更改以合并基于这些下拉字段的过滤。
  • 感谢您的回答。请让我理解。 $auctores_query 现在显示所有按我想要的 meta_key 'nome_classico' 排序的作者。我要做的是在从下拉列表中选择分类法“periodi”的特定术语时仅显示其作者。我的主要问题是第二列。我不知道如何将过滤合并到查询中。对不起我的英语不好!

标签: php wordpress custom-post-type archive custom-taxonomy


【解决方案1】:

如果您希望根据下拉选择限制作者列表,那么您需要修改查询并使用Taxonomy Query Parameters

修改代码,使其响应表单发布如下:

// First we have to take the args out into a variable
$args = array(
        'post_type'      => 'auctores', 
        'posts_per_page' => -1,
        'order'          => 'ASC',
        'meta_key'       => 'nome_classico',
        'orderby'        => 'meta_value'
);

// Then, we watch for the post and modify the args if appropriate:
if ( ! empty( $_POST['periodi'] ) ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'periodi',
            'field'    => 'slug',
            'terms'    => $_POST['periodi']
        )
    );
}

// Then we can execute the query:
$auctores_query = new WP_Query( $args );

【讨论】:

  • 你真好。那行得通。有没有办法显示打开页面的作者的完整列表?在此非常感谢! (我不太擅长 PHP :))
  • 这应该首先显示完整列表,并且只有在下拉列表中进行选择后才会缩小列表
  • 当我选择一个分类术语时效果很好,但如果没有选择任何内容,则列表不会显示。
  • 我已经更新了答案 - 请注意,我们现在使用的是 isset,而不是 ! empty(如“非空”)。请测试并告诉我。
猜你喜欢
  • 1970-01-01
  • 2016-06-05
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2023-03-30
  • 2017-11-21
相关资源
最近更新 更多