I've created a custom post type 'Projects' and attached a customer taxonomy 'Services' to it. I've set the taxonomy up as categories (I tried tags too and still have the same issue). It shows up in the wordpress admin and I'm able to create tags/categories on a project post. The tags/categories are saved in the database, because they still appear on the post edit page. However, I'm struggling to have them displayed on the front end. Please see code below from my function.php, archive-projects.php and single-projects.php. Please note, that the single-project.php file also includes a custom field called 'project-collection' which iterates through the respective project portfolio images and copy. Also, the tags are not rendered in the HTML at all (when inspecting element in Google Chrome). Thanks
functions.php
// Custom Post Types
function create_projects_post_type() {
$args = array(
'labels' => array(
'name' => 'Projects',
'singular_name' => 'Project'
),
'hierarchical' => false,
'menu_icon' => 'dashicons-portfolio',
'public' => true,
'has_archive' => true
);
register_post_type('projects', $args);
}
add_action('init', 'create_projects_post_type');
// Custom Taxonomy
function create_taxonomy(){
$args = array(
'labels' => array(
'name' => 'Services',
'singular_name' => 'Service'
),
'public' => true,
'has_archive' => true,
'hierarchical' => true
);
register_taxonomy('services', array('projects'), $args);
}
add_action('init', 'create_taxonomy');
archive-projects.php
<?php
get_header();
?>
<div data-aos="fade-up">
<div >
<h1>Our Projects</h1>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form.</p>
</div>
</div>
</div>
<div id="particles-js"></div>
</section>
<section id="portfolio" >
<div >
<div >
<?php if(have_posts('projects')) : ?>
<?php while(have_posts('projects')) : the_post(); ?>
<div >
<figure data-aos="fade-up">
<a href="<?php the_permalink(); ?>">
<?php $image = get_field('project_main_image'); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
</a>
<figcaption>
<h3>
<a href="<?php the_permalink(); ?>">
<?php echo get_field('project_name'); ?>
</a>
</h3>
<dl>
<?php
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>
</dd>
<?php endforeach; endif; ?>
</dl>
</figcaption>
</figure>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</section>
<?php get_footer(); ?>
single-projects.php
<?php
$GLOBALS['background'] = get_field('gradient_class');
get_header();
?>
<div >
<div >
<h1><?php the_field('project_name'); ?></h1>
<?php the_field('project_description'); ?>
</div>
</div>
</div>
<div id="particles-js"></div>
</section>
<?php if(have_rows('project_collection')): ?>
<?php $i = 1; ?>
<?php while(have_rows('project_collection')): the_row(); ?>
<section >
<div >
<div >
<div >
<?php $image = get_sub_field('image'); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
</div>
<div >
<h2><?php the_sub_field('sub_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</div>
</div>
</div>
</section>
<?php $i ; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php
// Get Tage NOT WORKING!
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>
</dd>
<?php endforeach; endif; ?>
<?php
// Get Catergories NOT WORKING!
$categories = get_the_category();
foreach($categories as $cat): ?>
<dd>
<a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a>
</dd>
<?php endforeach; ?>
<?php get_template_part('includes/section', 'result-slider'); ?>
<?php get_footer(); ?>
CodePudding user response:
I think instead of using this code block for your tag
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>
</dd>
<?php endforeach; endif; ?>
try this code block. I think it will solve your problem
$tags = wp_get_post_terms(get_the_ID(), 'Your tag/taxonomy slug name', array("fields" => "all"));
if($tags):
foreach($tags as $tag): ?>
<dd>
<a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>
</dd>
<?php endforeach; endif; ?>
explicitly to say instead of using get_the_tags() use wp_get_post_terms() function. I hope this will solve your tag problem. :)
