按自定义分类术语筛选帖子

时间:2014-08-13 作者:amespower

我正在尝试按作者筛选帖子。“post\\u author”是一种自定义分类法,我已将其包含在我的参数中,并且帖子显示为链接的作者姓名,但当我单击作者姓名链接时,我会得到所有作者。如何仅显示单击的作者?我已经包含了相关的代码片段:

//ADD THE TAXONOMY \'post_authors\' TO MY CUSTOM POST TYPE
function add_custom_taxonomies() {
   register_taxonomy(\'post_authors\', \'my-post-type\', array(
   \'hierarchical\' => true,
   \'labels\' => array(
   \'name\' => _x( \'Authors\', \'taxonomy general name\' ),
   \'singular_name\' => _x( \'Author\', \'taxonomy singular name\' ),
   \'search_items\' =>  __( \'Search Authors\' ),
   \'all_items\' => __( \'All Authors\' ),
   \'parent_item\' => __( \'Parent Author\' ),
   \'parent_item_colon\' => __( \'Parent Author:\' ),
   \'edit_item\' => __( \'Edit Author\' ),
   \'update_item\' => __( \'Update Author\' ),
   \'add_new_item\' => __( \'Add New Author\' ),
   \'new_item_name\' => __( \'New Author Name\' ),
   \'menu_name\' => __( \'Authors\' ),
   ),
   // Control the slugs used for this taxonomy
   \'rewrite\' => array(
   \'slug\' => \'post-authors\', // This controls the base slug that will display before each term
   \'with_front\' => false, // Don\'t display the category base before "/authors/"
   \'hierarchical\' => true // This will allow URL\'s like "/authors/lorem/ipsum/"
 ),
));
}
add_action( \'init\', \'add_custom_taxonomies\', 0 );


// and in my archive page......

//ARGS
$args = array( 
    \'post_type\' => \'my-post-type\',
    \'order\' => \'DESC\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_authors\',
            \'field\'    => \'slug\',
            \'terms\'    => \'Jill Jones\',
        ),
    ),
);



//RETRIEVES AUTHOR NAME FROM CUSTOM TAXONOMY
$terms = get_the_terms( $post->ID, \'post_authors\' );

if ($terms) {
    $terms_name = array();
    foreach ( $terms as $term ) {
    // The $term is an object, so we don\'t need to specify the $taxonomy.
    $term_link = get_term_link( $term );
    $terms_name[] = $term->name;
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
      continue;
    }
}
$author_name = $terms_name[0]; 
}



//RETRIEVING THE AUTHOR NAME IN LOOP
if ( $post_typed == (\'my-post-type\') && $author_name  != \'\' )  {  echo \'<a href="\' . esc_url( $term_link ) . \'    ">\' . $author_name . \'</a>\'; } 

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这是您代码中的预期输出。切勿在任何存档页或主页上更改自定义查询的主查询。自定义查询总是很麻烦,因为主查询在这些页面上非常具体。

我建议你pre_get_posts 要更改特定归档页或主页上的主查询,请将默认循环保留在适当的位置。

此外,您的税务查询完全错误。它应该是一个数组中的一个数组。请查看在中构造税务查询WP_Query

如需进一步阅读和更好地理解,请参阅my answer 我最近也问了一个类似的问题

示例代码(放在functions.php中并返回taxonomy.php中的默认循环):

function custom_author_page( $query ) {
    if ( !is_admin() && $query->is_tax( \'post_authors\' ) && $query->is_main_query() ) {

        $query->set( \'post_type\', \'my-post-type\' );
        $query->set( \'order\', \'DESC\' );

    }
}
add_action( \'pre_get_posts\', \'custom_author_page\' );

EDIT

根据您的评论,您已经将您的分类法模板命名为taxonomy post-author。php与分类法相关post 和期限author. 将分类法模板重命名为taxonomy-post\\u作者。php

EDIT 2

OPTION 1

将分类法页面更改为仅默认循环,无自定义循环。

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if
?>
将主查询更改为pre_get_posts 如上所述。这是正确的方法

OPTION 2

使用获取查询的术语get_queried_object(\'term\');, 从那返回术语的鼻涕虫PS!此代码应在分类页面中使用

$queried_object = get_queried_object(\'term\');
$term_slug = $queried_object->slug;
返回$term_slug 到您的tax_queryterms

EDIT 3

要从特定分类法中获取术语名称/段塞/ID的数组,请使用get_terms().

$terms = get_terms(\'post_authors\');

$term_names = array();

if ( !empty( $terms ) && !is_wp_error( $terms ) ) { 

    foreach ( $terms as $key=>$term){
        $term_names[$key] = $term->name;
    }
}
然后你就可以通过$term_namesterms,

\'terms\'    => $term_names

EDIT 4

使用

<pre><?php var_dump($term_names); ?></pre>
打印阵列。输出如下所示

array(6) {
  [0]=>
  string(12) "Admin se pen"
  [1]=>
  string(11) "Besprekings"
  [5]=>
  string(12) "Toets Parent"
  [6]=>
  string(15) "Uit die grapkas"
  [7]=>
  string(14) "Uit die koskas"
  [8]=>
  string(11) "Uit my lewe"
}

结束

相关推荐

GET Taxonomy ID

我有一行,其中“5”是分类ID。<?php echo function xyz (5,\'product_cat\'); ?>如何更改此项以使其始终自动识别当前页面的分类ID?尝试此操作但未成功:<?php echo function xyz (get_term_by(\'id\',\'\',\'product_cat);,\'product_cat\'); ?>我该怎么做?谢谢