web-dev-qa-db-fra.com

afficher tous les titres de catégories sur la page d'accueil

comment puis-je afficher dans mon thème tous les titres de catégories woocommerce sont des valeurs personnalisées.

exemple html comme ci-dessous

<li><a href="#">
          <div>
            <h2>category title</h5>
            <h3>custom field one value</h6>
          </div>
          <div class="imgpos">custom field two value</div>
          </a></li>

j'ai essayé de nombreuses manières mais toujours pas le succès

<?php
$post_type = 'product';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) : 
    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy );
    foreach( $terms as $term ) : 
        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
          ?>
          <a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><br>
          <?php
        endwhile; endif;
    endforeach;
endforeach;
?>

UPDATE 1 le code ci-dessous ne me donne que le titre et le lien de la catégorie mais ne peut pas comprendre comment obtenir une valeur de costume déposée aussi

<?php
$args = array(
    'number'     => $number,
    'orderby'    => 'title',
    'order'      => 'ASC',
    'hide_empty' => $hide_empty,
    'include'    => $ids
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
    foreach ( $product_categories as $product_category ) {
        echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
        $args = array(
            'posts_per_page' => -1,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    // 'terms' => 'white-wines'
                    'terms' => $product_category->slug
                )
            ),

        );

    }
}

?>

exemple pour obtenir le code de valeur personnalisé sera ci-dessous. je veux aller à l'intérieur div

 <?php if( get_field('hexagon_thumbnail') ): ?>

    <img src="<?php the_field('hexagon_thumbnail'); ?>" />

<?php endif; ?>
3
pagol007
<?php

$post_type = 'product';

$taxonomies = get_object_taxonomies((object) array( 'post_type' => $post_type ));

foreach ($taxonomies as $taxonomy) : 

    $terms = get_terms($taxonomy);

    foreach ($terms as $term) : 

        $term_link = get_term_link($term->term_id);

        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" ); ?>

        <li>

            <h2>
                <a href="<?php echo $term_link; ?>"><?php echo $term->name; ?></a>
            </h2>

            <?php 

            if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

                <div>

                <?php if( get_field('hexagon_thumbnail') ): ?>

                    <img src="<?php the_field('hexagon_thumbnail'); ?>" />

                <?php endif; ?>

                </div>

            <?php endwhile; endif; ?>

        </li> <!-- end list item -->

<?php endforeach; endforeach; ?>
1
userabuser

Pour extraire la valeur de ACF dans la boucle du produit, vous devez transmettre l'id du produit, si acf dans le produit ou si ACF dans la catégorie de produit, l'identifiant sera term id suivi de product_cat_ {term_id} en tant que second paramètre de the_field ou obtenir la fonction de champ.

0
user101626