将自定义分类添加为css类

时间:2014-06-25 作者:leanda

在自定义post类型归档中,我试图将自定义分类术语slug作为css类添加到标记中。我设法让它输出页面->ID,但很难让$term->slug正常工作。感觉我错过了一些非常简单的事情。以下是完整代码,感谢您的帮助:

     <?php

$parent_pages = get_pages( array( \'parent\' => 0, \'post_type\'=> \'archive\', \'orderby\' => \'menu_order\' , \'order\' => \'ASC\', \'sort_column\' => \'menu_order\' ) );
foreach ( $parent_pages as $parent_page ) {


echo \'<h1 class="page-heading" id="\';
echo $parent_page->post_name;
echo \'">\';

echo $parent_page->post_title;
echo \'</h1>\';
 echo \'<div class="wrapper grid4">\';

$all_pages = get_pages(array( \'post_type\'=> \'archive\',  \'orderby\' => \'menu_order\' , \'order\' => \'ASC\', \'sort_column\' => \'menu_order\' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
       $terms = get_the_terms( $child_page->ID, \'media\' );
   var_dump($terms);
  echo \'<article class="post col \' . $terms->slug .\'">\';

 echo \'<a class="fancybox" data-fancybox-type="iframe" href="http://www.iofpi.co.uk/civicworks.net/wp-content/plugins/pdfjs-viewer-shortcode/web/viewer.php?file=http://www.iofpi.co.uk/civicworks.net/wp-content/uploads/2014/05/Citizen_Manchester.pdf" title="\' . the_title_attribute(\'echo=0\') . \'" >\';

  echo get_the_post_thumbnail( $child_page->ID, \'medium\');

  echo \'</a>\';


    echo \'<h1>\';
  echo $child_page->post_title;
  echo \'</h1>\';
  echo \'</article>\';
}
     echo \'</div>\';
  }
  ?>

1 个回复
SO网友:David

您的代码显示它如何获取帖子,但不显示它如何获取$term. 论合理的发展环境(WP_DEBUG 设置为TRUE) 此代码应触发一个通知$term 此时未知。

但是,我建议使用模板功能post_class() 将帖子的相关信息显示为HTML类。(注意如何setup_postdata()wp_reset_postdata() 已使用。)

<?php

global $post;
foreach ( $child_pages as $post ) {
    setup_postdata( $post ); ?>
    <article <?php post_class();?>>
        <a class="fancybox" data-fancybox-type="iframe" href="" title="<?php the_title_attribute(); ?>" >
            <?php the_post_thumbnail( \'medium\' ); ?>
        </a>
        <h1><?php the_title(); ?></h1>
    </article>
    <?php
}
wp_reset_postdata();
据我所知,post_class() 打印有关默认分类法术语的类category, post_tagpost_format. 我不确定自定义分类法是否如此。先看看这个!

如果不是,则此函数(在functions.php) 将把术语作为HTML类带到post_class() 输出:

/**
 * adds terms of custom taxonomies to a set of
 * html classes
 *
 * @wp_hook post_class
 * @param array $classes
 * @param string $class
 * @param int $post_ID
 * @return array
 */
function wpse_151731_add_custom_tax_terms( $classes = array(), $class = \'\', $post_ID = 0 ) {

    $terms = wp_get_post_terms( $post_ID, array( \'your_taxonomy_slug\' ) );
    if ( empty( $terms ) )
        return $classes;

    foreach ( $terms as $t ) {
        $classes[] = $t->taxonomy . \'-\' . $t->slug;
    }

    return $classes;
}
add_filter( \'post_class\', \'wpse_151731_add_custom_tax_terms\', 10, 3 );

结束

相关推荐

分类自定义列-‘Manage_{Taxonomy}_Custom_Column’筛选器仅传递2个参数

我正在尝试向标记上显示的表中添加自定义列(&a);Wordpress中的类别管理页面。我创建了一个函数,并使用add_filter( \'manage_post_tag_custom_column\' , \'my_custom_column\' , 10 , 2 ); 我的职能是my_custom_column( $out , $name , $term_id ){ switch( $name ){ case \'my_column_slug\' :