如何打印当前的帖子类别名称,以便在HTML中以ID的形式输出它们,每个类别名称之间留有一个空格。
我目前拥有代码:
<?php
$args = array(
\'post_type\' => \'work\',
\'posts_per_page\' => \'9\'
);
$work_loop = new WP_Query( $args );
if ( $work_loop->have_posts() ) :
while ( $work_loop->have_posts() ) : $work_loop->the_post();
// Set variables
$cat_ids = get_the_ID();
$cat_names_array = get_the_category($ids);
$work_title = get_field( \'work_title\' );
$work_main_image = get_field( \'work_main_image\' );
$work_link = get_field( \'work_title\' );
$work_about = get_field( \'work_about\' );
// $work_category = get_the_terms( the_post()->ID, \'taxonomy\' );
// Output
?>
<a href="<?php echo $work_main_image[\'url\']; ?>" class="single_item link <?php print_r(array_values($cat_names_array)); ?> col-md-4 col-sm-6 wow fadeInUp" data-wow-delay="0.3s">
<img src="<?php echo $work_main_image[\'url\']; ?>" alt="">
</a>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
其输出:
<a href="http://localhost:8888/josh-richa/wp-content/uploads/2016/11/work-6.jpg" class="single_item link Array
(
[0] => WP_Term Object
(
[term_id] => 2
[name] => Web Design
[slug] => web-design
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[cat_ID] => 2
[category_count] => 1
[category_description] =>
[cat_name] => Web Design
[category_nicename] => web-design
[category_parent] => 0
)
[1] => WP_Term Object
(
[term_id] => 3
[name] => Web Development
[slug] => web-development
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
[cat_ID] => 3
[category_count] => 2
[category_description] =>
[cat_name] => Web Development
[category_nicename] => web-development
[category_parent] => 0
)
)
col-md-4 col-sm-6 wow fadeInUp" data-wow-delay="0.3s" style="visibility: visible; animation-delay: 0.3s; animation-name: fadeInUp; position: absolute; left: 0px; top: 0px;">
<img src="http://localhost:8888/josh-richa/wp-content/uploads/2016/11/work-6.jpg" alt="">
</a>
我想要每个数组中用空格分隔的“category\\u nicename”。
最合适的回答,由SO网友:Syed Fakhar Abbas 整理而成
您正在使用get_the_terms()
但此函数用于获取自定义分类法。
使用该功能get_the_category();
在WordPress循环中传递get_the_ID()
函数,该函数将获取循环中的当前帖子ID。
get\\u the\\u category(get\\u the\\u ID());
<?php
$args = array(
\'post_type\' => \'work\',
\'posts_per_page\' => \'9\'
);
$work_loop = new WP_Query( $args );
if ( $work_loop->have_posts() ) :
while ( $work_loop->have_posts() ) : $work_loop->the_post();
// Set variables
$cat_ids = get_the_ID();
$cat_names_array = get_the_category($ids);
$work_category = get_the_category( get_the_ID() );
$work_title = get_field( \'work_title\' );
$work_main_image = get_field( \'work_main_image\' );
$work_link = get_field( \'work_title\' );
$work_about = get_field( \'work_about\' );
// $work_category = get_the_terms( the_post()->ID, \'taxonomy\' );
// Output
?>
<a href="<?php echo $work_main_image[\'url\']; ?>" class="single_item link <?php foreach ( $work_category as $key => $value) { echo $value->category_nicename . " "; } ?> col-md-4 col-sm-6 wow fadeInUp" data-wow-delay="0.3s">
<img src="<?php echo $work_main_image[\'url\']; ?>" alt="">
</a>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
干杯:)