【发布时间】:2018-04-06 20:51:37
【问题描述】:
我有这段代码:
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<h2>
<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>
</h2>
</div>';
}
}
}
?>
</div>
这将显示我的自定义帖子类型“短期课程”的类别列表 以下是当前的外观:http://staging.seedcreativeacademy.co.uk/short-courses/
这很好,工作正常,但我也想为我的类别添加图像,我看起来像这样:
我有高级自定义字段,我在其中创建了一个图像字段,因此我现在可以为每个类别分配一个图像。到目前为止,一切都很好!
主要问题是当我尝试在上面的代码中显示图像时......
这是在任何正常情况下都会显示图像的代码:
<img src="<?php the_field('course_type_image'); ?>">
但我想我需要将它添加到上面的代码中......所以我想出了这个但它不起作用,因为我认为我不能将 php 标签放在另一个 PHP 标签中!?
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<img src="<?php the_field('course_type_image'); ?>">
<h2>
<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>
</h2>
</div>';
}
}
}
?>
</div>
所以我有点糊涂了……
【问题讨论】:
标签: php wordpress advanced-custom-fields categories