Wordpress标签和类别显示在后端(编辑后页面),但不显示在前端



我创建了一个自定义的帖子类型"Projects",并为其附加了一个客户分类法"Services"。我已经将分类法设置为类别(我也尝试了标记,但仍然存在相同的问题(。它显示在wordpress管理员中,我可以在项目帖子上创建标签/类别。标记/类别保存在数据库中,因为它们仍然显示在编辑后页面上。然而,我很难把它们展示在前端。请参阅下面来自我的function.php、archive-projects.php和single-projects.hp的代码。请注意,single-project.php文件还包括一个名为"项目集合"的自定义字段,该字段迭代各个项目组合图像和副本。此外,标签根本不会在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');

archive-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(); ?>

单项目.pp

<?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((函数。我希望这将解决您的标签问题。:(

AR。阿里夫,非常感谢你的回答。它帮助我解决了下一个问题。我为公文包项目添加了类别。管理面板中的还可以,但前端的输出很奇怪。一个投资组合项目的类别在另一个项目附近输出。

所以我换了这个$portfolio_categories=get_the_trms(get_the_ID((,'portfolio_category'(;

用这个$portfolio_categories=wp_get_post_terms(get_the_ID((,'portfolio_category',数组("字段"="所有"(;

在主题的模板/分类法/类别-投资组合.php中(当然在子主题中(

并且它最终正确地工作。

最新更新