自定义分类链接自定义帖子类型不起作用

时间:2013-04-17 11:50:24

标签: wordpress custom-taxonomy

我已经设置了自定义帖子类型和自定义分类。然后我将分类列表显示为一组链接,这样如果有人点击该链接,它应该显示该分类下的所有帖子。目前这不起作用。它一直把我带到404页面,“这有点令人尴尬不是吗?”消息。

代码如下:

的functions.php

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
    register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}

add_action('init', 'register_mypost_type');
function register_mypost_type() {
  register_post_type('companies',array(
    'labels' => array(
      'name' => 'Companies',
      'singular_name' => 'Company',
      'add_new' => 'Add New Company',
      'add_new_item' => 'Add New Company',
      'edit_item' => 'Edit Company',
      'new_item' => 'Add New Company',
      'view_item' => 'View Company',
      'search_items' => 'Search Companies',
      'not_found' => 'No companies found',
      'not_found_in_trash' => 'No companies found in trash'
    ),   
    'public' => true,
    'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt'),
    'capability_type' => 'post',
    'rewrite' => array('slug' => 'companies'),
    'taxonomies' => array('category'),
    'menu_position' => 7,
    'has_archive' => true,
    'hierarchical' => false
  ));
 }

然后在另一个名为'page-company.php'的页面上,我使用以下代码将分类法列表作为链接输出:

<?php
$args = array( 'taxonomy' => 'companies' );
wp_list_categories( $args );
?>

当我将鼠标悬停在其中一个链接上时,网址会显示为:

'http://localhost:81/?companies=graphic-design'

平面设计是我添加到自定义分类中的类别之一。

但是,点击此链接始终会将我带到404页面。

我已经设置了一个名为archive-companies.php的档案页面,我认为所有这些都可以解决问题。

任何人都可以提供的帮助将不胜感激。

提前致谢。

3 个答案:

答案 0 :(得分:1)

OMG OMG OMG ...经过几天阅读有关如何解决问题的帖子,使用重写规则和ussing永久链接重写代码,您的解决方案是唯一一个完美的解决方案! 我需要应用的唯一变化是自定义分类声明:

此代码

         'rewrite' => array(
                'slug' => 'pubs/type',
                'with_front' => false
             ),

代码

            'rewrite' => true,

就是这样。像魅力一样工作!

答案 1 :(得分:0)

注意:

在我重写之前,我测试了你的代码并得到了404。

1)我重新为您写了自定义帖子类型,并使用了您的自定义公司类别。

2)然后我从默认值循环到/%postname%/并且它有效。

Functions.php

在这里,您可以使用自定义帖子类型:

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

    $rewrite = array(
        'slug'                => 'company',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    );

    $args = array(
        'label'               => __( 'company'),
        'description'         => __( 'Companies Posts' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', ),
        'taxonomies'          => array( 'companies' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 100,
        'menu_icon'           => '',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'query_var'           => 'company',
        'rewrite'             => $rewrite,
        'capability_type'     => 'post',
    );

    register_post_type( 'company', $args );
}


add_action( 'init', 'register_mypost_type', 0 );

您的自定义类别

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
    register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}

答案 2 :(得分:0)

首先在主题的根目录中创建taxonomy-companies.php模板。该模板将负责显示您的分类术语帖子。

然后在该模板上,您需要使用get_queried_object()来获取所有分类法详细信息。

e.g;

$queries_obj = get_queried_object();
echo '<pre>';
print_r( $queries_obj );
echo '</pre>';

它会返回

WP_Term Object
(
    [term_id] => 10
    [name] => Featured companies
    [slug] => featured-companies
    [term_group] => 0
    [term_taxonomy_id] => 10
    [taxonomy] => companies-category
    [description] => 
    [parent] => 0
    [count] => 2
    [filter] => raw
)

然后查询下面的帖子。

$q = new WP_Query( array(
    'post_type' =>  'companies', // post type name
    'posts_per_page' =>  get_option( 'posts_per_page' ),
    'tax_query' =>  array(
        array(
            'taxonomy'  => $queries_obj->taxonomy,
            'field' => 'term_id',
            'terms' =>  array( $queries_obj->term_id )
        )
    )
) );
if ( $q->have_posts() ) :
    while ( $q->have_posts() ) :
        $q->the_post();

        // loop do stuf
        the_title();

    endwhile;

    wp_reset_query();

endif;
相关问题