0

投稿 (single.php) で分類法の配列用語が必要です。私は試しています

<?php $term_list = wp_get_post_terms($post->ID, 'agencia', array("fields" => "names")); print_r($term_list); ?>

しかし、html出力は次のとおりです。

Array ( [0] => Agencia 1 )

「Agencia 1」のみを表示したい。2 つの用語がある場合は、「Agencia 1、Agencia 2」を表示する必要があります。

私は何を間違っていますか?

分類の作成:

// カスタムタクソノミーの登録 function custom_taxonomy() {

$labels = array(
    'name'                       => _x( 'Agencias', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Agencia', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Genre', 'text_domain' ),
    'all_items'                  => __( 'All Genres', 'text_domain' ),
    'parent_item'                => __( 'Parent Genre', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Genre:', 'text_domain' ),
    'new_item_name'              => __( 'New Genre Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Genre', 'text_domain' ),
    'edit_item'                  => __( 'Edit Genre', 'text_domain' ),
    'update_item'                => __( 'Update Genre', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate genres with commas', 'text_domain' ),
    'search_items'               => __( 'Search genres', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove genres', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used genres', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'agencia', 'post', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy', 0 );
4

2 に答える 2

0

問題はprint_r、 のコンテンツをダンプするために使用していることです$term_list$term_listは配列であり、print_rその構造とともにそれを示しています。

次のようなものを試してください

foreach ($term_list as $term) {
    echo $term;
}

必要なラッピング タグを追加します。

于 2013-10-11T12:10:41.023 に答える
0

私はすべてを試しています。最後に、何時間もかけてグーグルでブラウジングする正しい答えを見つけました。誰にとっても解決策は次のとおりです。

<?php $terms = wp_get_post_terms($post->ID,'agencia');
$count = count($terms);
if ( $count > 0 ){
echo "<span>";
foreach ( $terms as $term ) {
echo $term->name . "<comma>, </comma>";
}
echo "</span>";} ?>

css:last-child で最後のコンマを削除するために作成しました

于 2013-10-11T12:19:15.560 に答える