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

まず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 に答える