在所有自定义分类帖子中获得相同的描述

时间:2022-02-19 作者:Hasnain Ghias

我开发了一个自定义的分类法帖子类型。我已显示类别(&A);内部类别职位/(产品)。

我得到了职位名称;图片应该是这样的(我加了),但所有帖子的描述都是一样的。

我不知道为什么相同的desc会一直显示,即使其他一切都是动态的。

以下是我使用的代码:

$taxID = get_queried_object()->term_id;
$args = array(
    \'post_type\' => \'industrial_product\',
    \'fields\' => \'ids\',
    \'posts_per_page\' => -1,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'industrial_product_cat\',
            \'terms\' => $taxID,
            \'field\' => \'term_id\',
            \'operator\' => \'IN\'
        )
    ),
  );
     $query = new WP_Query($args);
    if ($query->have_posts()):
        $i=1;
    foreach( $query->posts as $id ):?>
            <div class="row mb-4">
                <div class="col-md-4 col-12">
                    <?php
                    $image_palette = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), \'single-post-thumbnail\' ); 
                    ?>
                <img src="<?php echo $image_palette[0]; ?>" alt="" class="img-fluid" style="max-width:100%;">
                    
                </div>
                <div class="col-md-8 col-12">
                     <ul class="nav nav-tabs" id="myTab" role="tablist">
                        <li class="nav-item m-0" role="presentation">
                            <a class="nav-link active  c-2" id="productInfo<?php echo $i;?>-tab" data-toggle="tab" href="#productInfo<?php echo $i;?>" role="tab" aria-controls="productInfo<?php echo $i;?>" aria-selected="true">Product Information</a>
                         </li>
                        <li class="nav-item m-0" role="presentation">
                            <a class="nav-link  c-2" id="std_tds<?php echo $i;?>-tab" data-toggle="tab" href="#std_tds<?php echo $i;?>" role="tab" aria-controls="std_tds<?php echo $i;?>" aria-selected="false">STD / TDS</a>
                        </li>
                    </ul>
                    <div class="tab-content p-3" id="myTabContent<?php echo $i;?>">
                        <div class="tab-pane fade show active" id="productInfo<?php echo $i;?>" role="tabpanel" aria-labelledby="productInfo<?php echo $i;?>-tab">
                            <h5><?php echo get_the_title($id); ?></h5>
                            <p><?php echo get_the_content($id); ?></p>
                        </div>
                        
                        <div class="tab-pane fade" id="std_tds<?php echo $i;?>" role="tabpanel" aria-labelledby="std_tds<?php echo $i;?>-tab">
                        <?php
                                if(get_field(\'std_tds_description\', $id)){
                                    the_field(\'std_tds_description\',  $id);
                                }
                                else{
                                    echo "No, Content.";
                                }
                            ?>
                        </div>
                    </div>  
                </div>
        </div>
        <?php
        $i++;
    endforeach;
    endif;
//              
            }
             
    ?>

Here is a screenshot of the result:

enter image description here

有人能指出这里的问题是什么吗。我尝试了所有类型的东西,但它仍然显示相同的描述。谢谢&;抱歉,如果我在做一些愚蠢的事情,还在学习!

2 个回复
最合适的回答,由SO网友:Hasnain Ghias 整理而成

我刚换了电话

Old: <?php echo get_the_content( $id ); ?>

New: echo get_post_field(\'post_content\', $id);

不知道出了什么问题,但它起了作用。

SO网友:Sébastien Serre

正确的WordPress循环结构是:

// WP_Query arguments
$args = array(
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
此代码是否正常工作?

    $taxID = get_queried_object()->term_id;
    $args  = array(
        \'post_type\'      => \'industrial_product\',
        \'fields\'         => \'ids\',
        \'posts_per_page\' => - 1,
        \'tax_query\'      => array(
            array(
                \'taxonomy\' => \'industrial_product_cat\',
                \'terms\'    => $taxID,
                \'field\'    => \'term_id\',
                \'operator\' => \'IN\'
            )
        ),
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ):
        $i = 1;
        while ( $query->have_posts() ):
            $query->the_post();
            $id = get_the_ID();
            ?>
            <div class="row mb-4">
                <div class="col-md-4 col-12">
                    <?php
                    $image_palette = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), \'single-post-thumbnail\' );
                    ?>
                    <img src="<?php echo $image_palette[0]; ?>" alt="" class="img-fluid" style="max-width:100%;">

                </div>
                <div class="col-md-8 col-12">
                    <ul class="nav nav-tabs" id="myTab" role="tablist">
                        <li class="nav-item m-0" role="presentation">
                            <a class="nav-link active  c-2" id="productInfo<?php echo $i; ?>-tab" data-toggle="tab"
                               href="#productInfo<?php echo $i; ?>" role="tab" aria-controls="productInfo<?php echo $i; ?>" aria-selected="true">Product
                                Information</a>
                        </li>
                        <li class="nav-item m-0" role="presentation">
                            <a class="nav-link  c-2" id="std_tds<?php echo $i; ?>-tab" data-toggle="tab" href="#std_tds<?php echo $i; ?>" role="tab"
                               aria-controls="std_tds<?php echo $i; ?>" aria-selected="false">STD / TDS</a>
                        </li>
                    </ul>
                    <div class="tab-content p-3" id="myTabContent<?php echo $i; ?>">
                        <div class="tab-pane fade show active" id="productInfo<?php echo $i; ?>" role="tabpanel"
                             aria-labelledby="productInfo<?php echo $i; ?>-tab">
                            <h5><?php echo get_the_title( $id ); ?></h5>
                            <p><?php echo get_the_content( $id ); ?></p>
                        </div>

                        <div class="tab-pane fade" id="std_tds<?php echo $i; ?>" role="tabpanel" aria-labelledby="std_tds<?php echo $i; ?>-tab">
                            <?php
                            if ( get_field( \'std_tds_description\', $id ) ) {
                                the_field( \'std_tds_description\', $id );
                            } else {
                                echo "No, Content.";
                            }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
            $i ++;
        endwhile;
    endif;
    wp_reset_postdata();

相关推荐