php 如何显示分配了相同类别的所有帖子?

qyswt5oh  于 2022-11-21  发布在  PHP
关注(0)|答案(4)|浏览(139)

我有一个产品类别叫电缆,我想抓住分配到这个类别的所有职位,并输出说的职位的标题.这个类别是从自定义分类'类别',woo-commerce添加,我不知道这是否使事情更难?但我还没有找到一个解决方案.
有人能帮忙吗?

gt0wga4j

gt0wga4j1#

请尝试以下代码:
并根据您的要求进行修改:

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();
}
qij5mzcb

qij5mzcb2#

使用此代码显示分配给类别电缆的所有帖子

<?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; ?>
2o7dmzc5

2o7dmzc53#

我已使用列表创建

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();}
jchrr9hc

jchrr9hc4#

您可以将WP_Query()与tax_query()一起使用。

编辑:

试用此代码以获得类别页面上的产品。

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;
}

相关问题