1
votes

Obtenez une méta personnalisée de taxonomie personnalisée en dehors de la boucle

J'ai créé un certain nombre de champs personnalisés pour la taxonomie personnalisée. Aucun problème pour extraire leurs valeurs dans la requête. Mais dans ce cas, je les fais répéter autant de fois que j'ai des messages dans cette taxonomie.

Voici le code avec celui-ci dans la boucle

$tables_terms = $atts['custom'];

$tabargs = array(
    'posts_per_page' => -1,
    'offset'         => 0,   
    'post_type'      => 'customtables',
    'tax_query'     => array(
          array(
            'taxonomy' => 'tables', 
            'field' => 'slug',
            'terms' => array( 
                $tables_terms
            )
          )
        )
);

$tabs = new WP_Query( $tabargs );


if( $tabs->have_posts() ){
    while( $tabs->have_posts() ) : $tabs->the_post();
        $terms = get_the_terms( get_the_ID(), 'tables' );
        foreach ( $terms as $term ) {
            $t_id = $term->term_id;
            $term_meta = get_option( "taxonomy_$t_id" );
            echo $term_meta['term_1'];
        }   
    endwhile;
    wp_reset_postdata();
    echo $custom;
}

Comment puis-je les faire afficher une fois, en dehors de la boucle?


0 commentaires

3 Réponses :


0
votes

Vous pouvez utiliser, get_term_meta ;

get_term_meta récupère les métadonnées d'un terme.

get_term_meta ($ term-> term_id, 'your_term_name', true)


1 commentaires

Je l'ai essayé juste après $ tabs = new WP_Query ($ tabargs); puis à l'intérieur de if ($ tabs-> have_posts ()) {, puis à l'intérieur de while ($ tabs-> have_posts ()): $ tabs-> the_post () ;, puis à l'intérieur de foreach. Cependant, dans les 2 derniers, il apparaîtrait encore deux fois - je pense. En tout cas, cela n'a fonctionné dans aucun de ces



0
votes

Le code suivant a donc fonctionné pour moi

echo $term_meta['term_1'];

et à partir de là, j'ai simplement fait écho à chaque méta comme ceci

$term_slug = $tables_terms;
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
    if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) {
        break;
    }
}
$t_id = $term_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );


0 commentaires

0
votes

Essayez comme ça:

Stockez votre sortie dans un tableau et vérifiez lors de son stockage que la valeur existe déjà ou non.

$tables_terms = $atts['custom'];

$tabargs = array(
'posts_per_page' => -1,
'offset'         => 0,   
'post_type'      => 'customtables',
'tax_query'     => array(
      array(
        'taxonomy' => 'tables', 
        'field' => 'slug',
        'terms' => array( 
            $tables_terms
        )
      )
    )
    );

  $tabs = new WP_Query( $tabargs );

  $term_meta_array = array();
  if( $tabs->have_posts() ){
  while( $tabs->have_posts() ) : $tabs->the_post();
    $terms = get_the_terms( get_the_ID(), 'tables' );

    foreach ( $terms as $term ) {
        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        echo $term_meta['term_1'];
        if(!in_array($term_meta['term_1'], $term_meta_array)) array_push($$term_meta_array, $term_meta['term_1']);
    }   
  endwhile;
  wp_reset_postdata();
  echo $custom;
  foreach($term_meta_array as $st){
  echo $st;
}
}

1 commentaires

déjà fait fonctionner (voir ma propre réponse ci-dessus), merci, cependant!