Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Taxonomy Archive WordPress

I have a custom post type named <strong>Treatments</strong>, and a taxonomy called <strong>Stains</strong> attached.

I need to make a taxonomy archive template to view the children taxonomy of stains.

Here is the url:
http://grampianscarpetcleaning.grampians.wpengine.com/stains/asphalt/

Here is my Custom Post Type:

function grampianscarpetcleaning_post_type_stains() {
register_post_type( 'treatments',
array(
'label' => __('Treatments'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'rewrite' => true,
'hierarchical' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-plugins',
'supports' => array(
'title',
'editor',
)
)
);
register_taxonomy('stains', 'treatments', array('hierarchical' => true, 'label' => __('Stains'), 'singular_name' => 'Stains'));
}


add_action('init', 'grampianscarpetcleaning_post_type_stains');


Here is the default archive template:
<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package Shrake
* @since 1.0.0
*/

get_header();
?>

<main id="primary" class="content-area" role="main">

<?php do_action( 'shrake_main_top' ); ?>

<?php if ( have_posts() ) : ?>

<header class="page-header">
<?php the_archive_title( '<h1 class="page-title" itemprop="headline">', '</h1>' ); ?>
<?php the_archive_description( '<div class="page-content" itemprop="text">', '</div>' ); ?>
</header>

<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'templates/parts/content', get_post_format() ); ?>

<?php endwhile; ?>

<?php shrake_content_navigation(); ?>

<?php else : ?>

<?php get_template_part( 'templates/parts/content', 'none' ); ?>

<?php endif; ?>

<?php do_action( 'shrake_main_bottom' ); ?>

</main>

<?php
get_footer();


Please provide:
> Template
> Template title
> Any functions for the functions file.

Answers (3)

2016-02-03

Andrea P answers:

first of all you can simply make a duplicate of the archive.php file, and call the duplicate:
taxonomy-stains.php

wordpress will pick that file automatically to display archives for the stain taxonomy.

then the rest depends on what do you want to display in the archives for each post. I mean that if the custom post type treatements has some custom fields or data, you'll have to write into the archive strings of code to pick and display that data within the loop listing.

also, I see that there are 2 functions which are specific for your theme, and are used to display a title and description for the archive, but I don't know how those functions are coded and if they'll work for a custom post type..


parksey18 comments:

Yes, I already tried copying the archive.php and renaming it to taxonomy-stains.php, however just results in a 404.


Andrea P comments:

yes, you probably have just to update the permalinks, so that the rules are flushed and refreshed.

2016-02-03

Romel Apuya answers:

register_taxonomy('stains', 'treatments', array('hierarchical' => true, 'label' => __('Stains'), 'singular_name' => 'Stains'));
should be

register_taxonomy('stains', array( "treatments", "page" ), array('hierarchical' => true, 'label' => __('Stains'), 'singular_name' => 'Stains'));


parksey18 comments:

Applied that, however it's still producing a 404.
http://grampianscarpetcleaning.grampians.wpengine.com/stains/asphalt/

2016-02-03

Bob answers:

go to settings > Permalink.

set it to Plain/Default and again set it to desired type like "Post name".


parksey18 comments:

Bob, I'm a moron, of course!


Bob comments:

:)

Please also check other pages and confirm that it did not break any other page.