【问题标题】:Exclude Category from Custom Post Type Loop / Archive从自定义帖子类型循环/存档中排除类别
【发布时间】:2013-08-28 15:57:03
【问题描述】:

我一直在阅读有关如何执行此操作的帖子,但似乎没有任何意义或工作。我在我的 functions.php 文件中创建了一个自定义帖子类型(研究)和一个自定义分类(分类)。我的帖子类型还有一个自定义存档页面 (archive-research.php)。

我想从自定义存档模板中排除一个名为“oldresearch”的类别(分类)。

以下是我的自定义帖子类型代码。有人可以帮助我,让我知道排除代码需要去哪里。

<?php
/* redirect users to front page after login */
function redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');

}
}
add_action('login_form', 'redirect_to_front_page');


if ( function_exists('register_sidebars') )
    register_sidebars(3);


add_action('init', 'register_custom_menu');

function register_custom_menu() {
register_nav_menu('custom_menu', __('Custom Menu'));
}

/** Registering Custom Post Type: Research **/

// Register Taxonomy for Research
$labels = array(
    'name'                          => 'Classifications',
    'singular_name'                 => 'Classification',
    'search_items'                  => 'Search Classifications',
    'popular_items'                 => 'Popular Classifications',
    'all_items'                     => 'All Classifications',
    'parent_item'                   => 'Parent Classifications',
    'edit_item'                     => 'Edit Classifications',
    'update_item'                   => 'Update Classifications',
    'add_new_item'                  => 'Add New Classification',
    'new_item_name'                 => 'New Classifications',
    'separate_items_with_commas'    => 'Separate Classifications with commas',
    'add_or_remove_items'           => 'Add or remove Classifications',
    'choose_from_most_used'         => 'Choose from most used Classifications'
    );

$args = array(
    'label'                         => 'Classifications',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'research/classifications', 'with_front' => false ),
    'query_var'                     => true
);

register_taxonomy( 'Classifications', 'Research', $args );

// Register Classification Column

add_filter( 'manage_research_posts_columns', 'ilc_cpt_columns' );
add_action('manage_research_posts_custom_column', 'ilc_cpt_custom_column', 10, 2);

function ilc_cpt_columns($defaults) {
    $defaults['Classifications'] = 'Classifications';
    return $defaults;
}

function ilc_cpt_custom_column($column_name, $post_id) {
    $taxonomy = $column_name;
    $post_type = get_post_type($post_id);
    $terms = get_the_terms($post_id, $taxonomy);

    if ( !empty($terms) ) {
        foreach ( $terms as $term )
            $post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
        echo join( ', ', $post_terms );
    }
    else echo '<i>No terms.</i>';
}

// Register Custom Post Type
function research_post_type() {
    $labels = array(
        'name'                => _x( 'Research', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Research', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Research', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Research', 'text_domain' ),
        'all_items'           => __( 'All Research', 'text_domain' ),
        'view_item'           => __( 'View Research', 'text_domain' ),
        'add_new_item'        => __( 'Add New Research', 'text_domain' ),
        'add_new'             => __( 'New Research', 'text_domain' ),
        'edit_item'           => __( 'Edit Research', 'text_domain' ),
        'update_item'         => __( 'Update Research', 'text_domain' ),
        'search_items'        => __( 'Search Research', 'text_domain' ),
        'not_found'           => __( 'No Research found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No Research found in Trash', 'text_domain' ),

    );

    $rewrite = array(
        'slug'                => 'research',
        'with_front'          => true,
        'rewrite_pages'       => true,
        'rewrite_feeds'       => true,
    );

    $args = array(
        'label'               => __( 'research', 'text_domain' ),
        'description'         => __( 'Agri-Gro product research', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
        'taxonomies'          => array( 'Classifications', 'post_tag' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 20,
        'menu_icon'           => 'http://www.agrigro.com/news/wp-content/uploads/2013/01/Documents-icon.png',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'capability_type'     => 'page',
    );




    register_post_type( 'research', $args );
}

// Hook into the 'init' action
add_action( 'init', 'research_post_type', 0 );




?>

【问题讨论】:

    标签: categories wordpress custom-post-type


    【解决方案1】:

    您可以在您的functions.php 中使用pre_get_posts 过滤器来更改特定post_type 的查询。这不是完成任务的唯一方法,但在我看来这是最简单的方法

    更新

    您需要从查询中排除自定义分类术语,因此我们必须设置 tax_query 对象,因此:

    例子:

    add_action('pre_get_posts','custom_get_posts');
    function custom_get_posts($query) {
      // We are not displaying the posts on admin panel and this is the main query
      if ( !is_admin() && $query->is_main_query() ) {
        //Define the tax_query
        $taxquery = array(
          array(
            'taxonomy' => 'Classifications', // also try lower case, remember a taxonomy name must be in lowercase
            'field' => 'slug',
            'terms' => array( 'oldresearch' ),
            'operator' => 'NOT IN'
          )
        );
        // We are displaying a custom posts type archive
        if( $query->is_post_type_archive( 'research' ) ){ 
          $query->set('tax_query', $taxquery );
        }
      }
    }
    

    希望对您有所帮助!如果您遇到困难,请告诉我。

    【讨论】:

    • 感谢您的提示。我尝试了上述方法并使用了我在 URL 中看到的 ID,但它不起作用。我想知道我是否使用正确的术语来解释这一点。这是我的自定义类别的 URL,但这表明它是一个标签,而不是我标记的类别,也许? ? ? domain.com/news/wp-admin/…
    • @user2612111 是正确的,如果您检查标准分类“类别”中的一个术语,您将看到相同的 URL 结构
    • @user2612111,我发现了问题,给我两分钟:)
    • 太好了,做到了!非常感谢您的帮助 :-) 必须保存这个。
    • @user2612111 不客气,请将答案设为“已接受”,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2014-11-18
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多