【发布时间】:2022-01-04 13:34:53
【问题描述】:
我创建了一个自定义帖子类型“项目”并附加了一个客户分类“服务”。我已将分类法设置为类别(我也尝试了标签,但仍然有同样的问题)。它显示在 wordpress 管理员中,我可以在项目帖子上创建标签/类别。标签/类别保存在数据库中,因为它们仍然出现在帖子编辑页面上。但是,我很难将它们显示在前端。请从我的 function.php、archive-projects.php 和 single-projects.php 中查看下面的代码。请注意,single-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');
归档项目.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(); ?>
single-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(); ?>
【问题讨论】:
-
您是否将
the_post()放在模板顶部?否则,当前的帖子 ID 可能不会被初始化,并且不会获取正确的类别。 developer.wordpress.org/reference/functions/the_post -
谢谢@jrswgtr。是的,我做到了。如上所示,仅在 archive-projects.php 文件中。
-
你也应该把它放在你的
single-projects.php之上,紧跟在<?php之后 -
再次感谢。但还是一无所获。我在
标签: php wordpress wordpress-theming categories custom-wordpress-pages