2015-12-27 107 views
1

我正在为我的论文开发拉丁文学数字图书馆。 现在我正在处理作者的存档页面。 作者是一种自定义帖子类型,注册为'auctores',并为其分配了名为'periodi'的自定义分类。 在档案页面(archive-auctores.php)中,我想显示所有列出的元字段和其他三列的作者:句点(它指的是我想制作过滤器的自定义分类),书的数量(应该有分配给该作者的书的数量(cpt))和Gener(在哪里显示分配给该作者的书的所有文学流派,并且这将是通过cpt分配给书cpt的另一个分类学 - 分类插件)。数字列应该可排序(asc/desc),而我想为期间和种类制作过滤器。 I.E. - >打开存档页面,它将显示完整的作者列表。因此应该有可能过滤作者的时间段,页面应该只显示那些具有该特定分类术语的“标记”的作者。WordPress的自定义邮政类型存档页与下拉Taxonomyes

我以为我已经找到了解决方案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> 

任何帮助将非常感激。 谢谢!

+0

您的'$ auctores_query'不会根据您描述的元字段进行过滤。它需要进行修改,以便基于这些下拉字段合并筛选。 –

+0

感谢您的回答。请让我明白。 $ auctores_query现在显示由meta_key'nome_classico'订购的所有作者,这正是我想要的。 我试图做的是只显示分类标准'periodi'的特定术语的作者,当它从下拉列表中选择时。 我的主要问题是第二栏。我无法确定如何在查询中加入过滤。 对不起,我的英语不好! –

回答

0

如果你想基于有限的下拉选择作者的名单,那么你就需要修改查询和利用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); 
+0

非常友善。这很奏效。有没有办法显示打开页面的作者的完整列表?在此期间感谢很多! (我不擅长于PHP :)) –

+0

这应该首先显示完整列表,并且只有在下拉列表中进行了选择时才缩小列表 –

+0

当我选择一个分类术语时,它完美工作,但如果什么都没有选中的列表不显示。 –

相关问题