doulizhi1247 2013-08-05 19:37
浏览 32
已采纳

将自定义分类法作为类名添加到自定义帖子类型的文档页面,wordpress

I thought this would be easy enough, although it's proving quite difficult. My end goal is to integrate jQuery isotope into my wordpress portfolio. I've gotten the isotope to work outside of wordpress, but I am having a very difficult time assigning my custom taxonomies as class names. So I don't need help with isotope, just assigning taxonomies as classes.

I have a custom post type of portfolio

the portfolio has 2 custom taxonomies that I want to use to filter my results on an archive page. One taxonomy is "media" the other is "campaigns"

So if I assign a media taxonomy of "print" and a campaign taxonomy of "local" to a post from portfolio, I'd like the output on the archive page to be something like this:

<div id="post-34" class="print local">...</div>

However I currently have this

<div id="post-34" class>...</div>

i followed instructions from the codex on get_the_terms. I added this code to my functions.php file:

<?php // get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // get post by post id
    $post = &get_post($post->ID);
// get post type by post
    $post_type = $post->post_type;
// get post type taxonomies
    $taxonomies = get_object_taxonomies($post_type);
    foreach ($taxonomies as $taxonomy) {
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            $out = array();
            foreach ( $terms as $term )
                $out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
        $return = join( ', ', $out );
    }
}
return $return;
} ?>

Then I dropped in the echo call into the class call in my loop on the archive-portfolio.php page like this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div id="post-<?php the_ID(); ?>" class="<?php echo custom_taxonomies_terms_links(); ?>">

Any help would be greatly appreciated. This is driving me nuts that I can't figure this out.

  • 写回答

1条回答 默认 最新

  • douzhou7656 2013-08-05 23:03
    关注

    wordpress has a clean way of outputting the class name for a post item - using post_class so in your case first set the div to

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...</div>
    

    and to add the taxonomy name to the class you have to add a filter. So in your functions.php drop this in (change YOUR_TAXO_NAME to the name of your custom taxonomy): (taken from here)

    add_filter( 'post_class', 'custom_taxonomy_post_class', 10, 3 );
    
        if( !function_exists( 'custom_taxonomy_post_class' ) ) {
    
            function custom_taxonomy_post_class( $classes, $class, $ID ) {
    
                $taxonomy = 'YOUR_TAXO_NAME';
    
                $terms = get_the_terms( (int) $ID, $taxonomy );
    
                if( !empty( $terms ) ) {
    
                    foreach( (array) $terms as $order => $term ) {
    
                        if( !in_array( $term->slug, $classes ) ) {
    
                            $classes[] = $term->slug;
    
                        }
    
                    }
    
                }
    
                return $classes;
    
            }
    
        }
    

    (for multiple taxonomies add an array)

    $taxonomy = array('YOUR_TAXO_NAME_1', 'YOUR_TAXO_NAME_2');
    

    and that should add the post type name as well as the taxonomy its tagged in to the div class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私