¿Cómo mostrar todas las publicaciones asignadas a la misma categoría?

3 minutos de lectura

Tengo una categoría de producto llamada Cable y quiero tomar todas las publicaciones asignadas a esta categoría y mostrar el título de dichas publicaciones. Esta categoría es de la taxonomía personalizada ‘Categoría’ que agrega woo-commerce, no estoy seguro de si eso dificulta las cosas. pero no he encontrado una solución todavía.

¿Alguien podría ayudar con esto?

ingrese la descripción de la imagen aquí

Avatar de usuario de Krunal Bhimajiyani
Krunal Bhimajiyani

Prueba este código:

Y modificar según sus requisitos:

add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {

    $query = new WP_Query(
    array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => array( '18' ), //Your custom category id
            ),
        ),
    )
);

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
    }
}
wp_reset_postdata();
}

  • Para ‘product_cat’, ¿habría alguna forma de usar la identificación en su lugar? Me gustaría usar la identificación de la página actual en lugar de escribir el nombre si es posible

    – matt.mcg

    16 de noviembre a las 11:07

Use este código para mostrar todas las publicaciones asignadas a la categoría cable

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cable' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

<div> 
    <?php 
    the_title(); 
    the_excerpt(); ?>
</div>

<?php endforeach; ?>

  • En lugar de ‘category_name’, ¿hay alguna forma de usar la identificación de gato de las páginas actuales? Me gustaría usar la identificación de la página actual en lugar de escribir el nombre si es posible

    – matt.mcg

    16 de noviembre a las 11:08

he creado con lista

add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1, //--1 for unlimited and number to limit
    'tax_query'      => array(
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
        ),
    ),
));

if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
    $pi_query ->the_post();
    echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}

avatar de usuario de zillionera_dev
zillionera_dev

Puedes usar WP_Query() con tax_query().

EDITAR:

Pruebe este código para obtener productos en la página de categoría.

add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
    if(!is_product_category()){
        return;
    }
    $term = get_queried_object();
    
    ob_start();
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'tax_query' => array(
             'taxonomy' => 'product_cat',
             'field'    => 'term_id',
             'terms'     =>  $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
             'operator'  => 'IN'
        )
    );
    $the_query = new WP_Query($args);

    // The Loop
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            echo '<h3>' . get_the_title() . '</h3>';
        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    $contents = ob_get_clean();
    return $contents;
}

¿Ha sido útil esta solución?