Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将自定义分类法作为类名添加到自定义文章类型的归档页面wordpress_Php_Wordpress_Custom Post Type_Taxonomy_Custom Taxonomy - Fatal编程技术网

Php 将自定义分类法作为类名添加到自定义文章类型的归档页面wordpress

Php 将自定义分类法作为类名添加到自定义文章类型的归档页面wordpress,php,wordpress,custom-post-type,taxonomy,custom-taxonomy,Php,Wordpress,Custom Post Type,Taxonomy,Custom Taxonomy,我认为这很容易,尽管事实证明很难。我的最终目标是将jQuery集成到我的wordpress组合中。我已经让同位素在wordpress之外工作,但我很难将自定义分类法指定为类名。所以我不需要同位素方面的帮助,只需要将分类法指定为类 我有一个自定义的职位类型的投资组合 公文包有两个自定义分类法,我想用它们在归档页面上过滤我的结果。一种分类法是“媒体”,另一种分类法是“活动” 因此,如果我将媒体分类法指定为“打印”,将活动分类法指定为“本地”,则我希望归档页面上的输出如下所示: <div id=

我认为这很容易,尽管事实证明很难。我的最终目标是将jQuery集成到我的wordpress组合中。我已经让同位素在wordpress之外工作,但我很难将自定义分类法指定为类名。所以我不需要同位素方面的帮助,只需要将分类法指定为类

我有一个自定义的职位类型的投资组合

公文包有两个自定义分类法,我想用它们在归档页面上过滤我的结果。一种分类法是“媒体”,另一种分类法是“活动”

因此,如果我将媒体分类法指定为“打印”,将活动分类法指定为“本地”,则我希望归档页面上的输出如下所示:

<div id="post-34" class="print local">...</div>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div id="post-<?php the_ID(); ?>" class="<?php echo custom_taxonomies_terms_links(); ?>">
。。。
但是我现在有这个

<div id="post-34" class>...</div>
。。。
我按照法典中关于获取条款的指示行事。我在functions.php文件中添加了以下代码:

<?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;
} ?>

然后,我在archive-portfolio.php页面的循环中将echo调用放入类调用中,如下所示:

<div id="post-34" class="print local">...</div>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

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


wordpress有一种干净的方式输出post项目的类名-使用
post_class
,因此在您的情况下,首先将div设置为

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...</div>
(对于多个分类法,请添加一个数组)


这应该将post类型名称及其标记的分类添加到div类中

,这很好,也很有效。我甚至不知道post_类函数。有没有办法使它适用于两种分类法?在我的网站上,我有一个“活动”分类法和一个“媒体”分类法,我希望这两个分类法的值都被添加到classSweet中!非常感谢,这个很好用。我真的很感激!