0

かなり難しいことが証明されていますが、これは十分に簡単だと思いました。私の最終的な目標は、jQuery isotope を私の wordpress ポートフォリオに統合することです。同位体を wordpress の外で動作するようにしましたが、カスタム分類法をクラス名として割り当てるのに非常に苦労しています。したがって、分類法をクラスとして割り当てるだけで、アイソトープの助けは必要ありません。

ポートフォリオのカスタム投稿タイプがあります

ポートフォリオには、アーカイブ ページで結果をフィルター処理するために使用したい 2 つのカスタム分類法があります。1 つの分類は「メディア」で、もう 1 つは「キャンペーン」です。

したがって、「印刷」のメディア分類と「ローカル」のキャンペーン分類をポートフォリオからの投稿に割り当てると、アーカイブ ページの出力は次のようになります。

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

しかし、私は現在これを持っています

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

get_the_terms のコーデックスの指示に従いました。このコードを 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 ページのループ内のクラス呼び出しにエコー呼び出しをドロップしました。

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

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

どんな助けでも大歓迎です。これは、私がこれを理解できないことを私に夢中にさせています。

4

1 に答える 1

1

wordpress には、投稿アイテムのクラス名を出力するクリーンな方法がありpost_classます。これを使用して、最初に div を次のように設定します。

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

分類名をクラスに追加するには、フィルターを追加する必要があります。functions.php にこれをドロップします (YOUR_TAXO_NAME をカスタム分類法の名前に変更します): (ここから取得)

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;

        }

    }

(複数のタクソノミーの場合は配列を追加します)

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

投稿タイプ名とタグ付けされた分類法を div クラスに追加する必要があります

于 2013-08-05T23:03:53.167 に答える