I have a registered a custom post type called "projects" and assigned a taxonomy called "disciplines". Here's how it looks:
<?php
// Register projects cpt
function projects() {
$labels = array(
'name' => _x( 'Projects', 'Post Type General Name', 'sitename' ),
'singular_name' => _x( 'Project', 'Post Type Singular Name', 'sitename' ),
'menu_name' => __( 'Projects', 'sitename' ),
'name_admin_bar' => __( 'Projects', 'sitename' ),
'archives' => __( 'Item Archives', 'sitename' ),
'attributes' => __( 'Item Attributes', 'sitename' ),
'parent_item_colon' => __( 'Parent Item:', 'sitename' ),
'all_items' => __( 'All Items', 'sitename' ),
'add_new_item' => __( 'Add New Item', 'sitename' ),
'add_new' => __( 'Add New', 'sitename' ),
'new_item' => __( 'New Item', 'sitename' ),
'edit_item' => __( 'Edit Item', 'sitename' ),
'update_item' => __( 'Update Item', 'sitename' ),
'view_item' => __( 'View Item', 'sitename' ),
'view_items' => __( 'View Items', 'sitename' ),
'search_items' => __( 'Search Item', 'sitename' ),
'not_found' => __( 'Not found', 'sitename' ),
'not_found_in_trash' => __( 'Not found in Trash', 'sitename' ),
'featured_image' => __( 'Featured Image', 'sitename' ),
'set_featured_image' => __( 'Set featured image', 'sitename' ),
'remove_featured_image' => __( 'Remove featured image', 'sitename' ),
'use_featured_image' => __( 'Use as featured image', 'sitename' ),
'insert_into_item' => __( 'Insert into item', 'sitename' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'sitename' ),
'items_list' => __( 'Items list', 'sitename' ),
'items_list_navigation' => __( 'Items list navigation', 'sitename' ),
'filter_items_list' => __( 'Filter items list', 'sitename' ),
);
$args = array(
'label' => __( 'Projects', 'sitename' ),
'description' => __( 'A custom post type for projects.', 'sitename' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'taxonomies' => array( 'disciplines' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'menu_icon' => 'dashicons-admin-appearance',
'show_in_rest' => true,
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'projects', 0 );
// Register disciplines taxonomy
function disciplines() {
$labels = array(
'name' => _x( 'Disciplines', 'Taxonomy General Name', 'sitename' ),
'singular_name' => _x( 'Discipline', 'Taxonomy Singular Name', 'sitename' ),
'menu_name' => __( 'Disciplines', 'sitename' ),
'all_items' => __( 'All Items', 'sitename' ),
'parent_item' => __( 'Parent Item', 'sitename' ),
'parent_item_colon' => __( 'Parent Item:', 'sitename' ),
'new_item_name' => __( 'New Item Name', 'sitename' ),
'add_new_item' => __( 'Add New Item', 'sitename' ),
'edit_item' => __( 'Edit Item', 'sitename' ),
'update_item' => __( 'Update Item', 'sitename' ),
'view_item' => __( 'View Item', 'sitename' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'sitename' ),
'add_or_remove_items' => __( 'Add or remove items', 'sitename' ),
'choose_from_most_used' => __( 'Choose from the most used', 'sitename' ),
'popular_items' => __( 'Popular Items', 'sitename' ),
'search_items' => __( 'Search Items', 'sitename' ),
'not_found' => __( 'Not Found', 'sitename' ),
'no_terms' => __( 'No items', 'sitename' ),
'items_list' => __( 'Items list', 'sitename' ),
'items_list_navigation' => __( 'Items list navigation', 'sitename' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
);
register_taxonomy( 'disciplines', array( 'projects' ), $args );
}
add_action( 'init', 'disciplines', 0 );
I've then created an archive-projects.php
page, like so:
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package sitename
*/
get_header();
?>
<div class="has-sidebar">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<article class="navy">
<?php if ( have_posts() ) : ?>
<header class="entry-header screen-reader-text">
<h1>Projects</h1>
</header><!-- .page-header -->
<div class="button-group filters-button-group narrow" id="filters">
<button class="button is-checked" data-filter="*">All</button>
<?php
// Loop through categories - https://developer.wordpress.org/reference/functions/get_categories
$categories = get_categories( array(
'taxonomy' => 'disciplines',
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $categories as $category ) {
echo '<button class="button" data-filter=".'.$category->slug.'">';
echo $category->name;
echo '</button>';
}
?>
</div>
<div class="entry-content filter" id="images">
<div class="gutter-sizer"></div>
<?php
// Loop through posts
while ( have_posts() ) :
the_post();
// Get post ID - Important for pulling ACF fields from single posts
$post_id = get_the_ID();
// Get post terms
$terms = wp_get_post_terms( $post_id, 'disciplines' );
?>
<a href="<?php the_permalink(); ?>" class="filter-item <?= $terms->slug; ?>" data-category="<?= $terms->slug; ?>">
<figure>
<?php
if ( get_field('thumbnail_image', $post_id) ) {
echo wp_get_attachment_image( get_field('thumbnail_image', $post_id), 'medium-square');
}
else {
echo wp_get_attachment_image( get_field('hero_image', $post_id), 'medium-square');
}
?>
</figure>
<h2><?php the_title(); ?></h2>
</a>
<?php
endwhile;
?>
</div><!-- .entry-content -->
<?php
else:
echo '<p>Please add some projects.</p>';
endif;
?>
</article>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- .has-sidebar -->
<?php get_footer(); ?>
I'm trying to get the taxonomy term by slug for each post class and data attribute, but the method I've used isn't working. I've followed this documentation as a guide.
What am I doing wrong here?