WordPress 标签和类别显示在后端(post 编辑页面)但不显示在前端?

Wordpress Tags and Categories show on backend (post edit page) but not on frontend?

我创建了一个自定义 post 类型 'Projects' 并为其附加了客户分类法 'Services'。我已经将分类法设置为类别(我也尝试了标签但仍然有同样的问题)。它显示在 wordpress 管理员中,我能够在项目 post 上创建 tags/categories。 tags/categories 保存在数据库中,因为它们仍然出现在 post 编辑页面上。但是,我正在努力让它们显示在前端。请参阅下面来自我的 function.php、archive-projects.php 和 single-projects.php 的代码。请注意,单个 project.php 文件还包含一个名为 'project-collection' 的自定义字段,它遍历各个项目组合图像和副本。此外,标签根本不会在 HTML 中呈现(在 Google Chrome 中检查元素时)。谢谢

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

存档-projects.php

<?php

get_header();

?>


<div class="row vertical-align d-flex align-items-center pt-0" data-aos="fade-up">
        <div class="col-md-8 offset-md-2 col-lg-6 offset-lg-3 text-center z-index">
          <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" class="container-fluid">
    <div class="container-fluid">
      <div class="row">

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

        <?php while(have_posts('projects')) : the_post(); ?>
        
        <div class="col-md-6">
            <figure data-aos="fade-up">
                <a href="<?php the_permalink(); ?>">
                    <?php $image = get_field('project_main_image'); ?>
                    <img class="img-fluid" 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(); ?>

单-projects.php

<?php

$GLOBALS['background'] = get_field('gradient_class');

get_header();

?>
<div class="row vertical-align d-flex align-items-center pt-0">
        <div class="col-md-8 offset-md-2 text-center z-index">
          <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 class="container-fluid portfolio-item">
            <div class="container-fluid">
                <div class="row d-flex align-items-center">
                    <div class="col-md-6 <?php if($i%2 != 0) : ?>order-md-2 <?php endif; ?>portfolio-image">
                        <?php $image = get_sub_field('image'); ?>
                        <img class="img-fluid" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
                    </div>  
                    <div class="col-md-6 portfolio-content">
                        <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(); ?>

我认为不要将此代码块用于您的标签

 $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; ?>

试试这个代码块。我想它会解决你的问题

 $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; ?>

明确地说而不是使用 get_the_tags() 使用 wp_get_post_terms()函数。 我希望这能解决您的标签问题。 :)