WordPress 获取自定义文章类型分类和调用

网站教程2个月前更新 忆余函
353 0 0

WordPress 涉及到自定义文章类型的时候,有些调用就变得麻烦了起来,特别是对分类使用了自定义字段的时候, 使用 wp list categories 函数只能把分类名称和链接调用起来,遇到这种情况,怎么办呢?下面是我经过多种尝试后得出来的方法, 仅供参考。

<?php
  $post_type = get_query_var('post_type'); //获取文章类型别名
  $taxonomies = get_object_taxonomies($post_type); //获取与文章类型相关联的分类法别名
  $terms = get_terms( $taxonomies, 'orderby=count&hide_empty=0' ); //获取该分类法的所有分类数组

  foreach ($terms as $term) { //遍历分类,然后输入分类链接
    $term_cover = get_term_meta($term->term_id, 'upload_media', false); //输入分类图片(这里用了piklist插件实现)
?>

<li>
  <div class="pic"><a href="<?php echo get_term_link( $term, 'procat' ); ?>" target="_blank">< ?php echo wp_get_attachment_image( $term_cover[0], 'term_desc' ); ?></a></div>
  <div class="title"><a href="<?php echo get_term_link( $term, 'procat' ); ?>" target="_blank">< ?php echo $term->name; ?></a></div>
</li>
<?php } ?>
© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...

欢迎进入官方群聊(关注最新消息)

QQ群698305982 点击进入  


登录即可开启个人书签等新功能! 点击登录

如果网站对您有帮助的话,希望您能把快导航网分享给身边朋友,感激不尽!


(登录后有效期内不再显示)

点击下方关闭此窗口