0

I have set up a custom post type and a custom taxonomy. Then I am displaying the list of taxonomies as a set of links so that if someone clicks on that link, it should bring up all the posts under that taxonomy. Currently this is not working. It keeps taking me to the 404 page with the 'This is somewhat embarrassing isn't it?' message.

Code is as follows:

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
  ));
 }

Then on another page called 'page-company.php' I use the following code to output the list of taxonomies as links:

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

When I hover over one of these links the URL is displayed as:

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

Graphic Design being one of the categories I have added to my custom taxonomy.

However clicking this link always takes me to the 404 page.

I have set up an archives page called archive-companies.php and I thought all of this would do the trick.

Any help that anyone can provide would be greatly appreciated.

Thanks in advance.

4

3 回答 3

2

OMG OMG OMG ... 在阅读了关于如何解决问题、使用重写规则和使用永久链接重写代码的帖子数天之后,您的解决方案是唯一一个完美运行的解决方案!我需要应用的唯一更改是在自定义分类声明中:

这段代码

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

对于此代码

            'rewrite' => true,

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

于 2013-12-13T15:01:17.050 回答
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 ) );
}
于 2013-04-17T17:02:21.230 回答
0

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

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

例如;

$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;
于 2016-08-31T22:05:35.477 回答