php – 在分类页面上显示标签

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在分类页面上显示标签脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前在我的CMS中有选项,可以将标签添加到我的自定义帖子类型单页面.

现在,我想将此标记显示为“特色”项目.
所以,在我的分类–‘filename’中,我使用以下代码收集标记并在分类页面中显​​示它们:

<?PHP 
        $args = array(
          'tag_slug__and' => array('sector1'),'post_tyPE' => array( 'sectors' )
          );
        $loop = new WP_Query( $args );
        while ($loop->have_posts() ) : $loop->the_post();
        ?>
        <a href="<?PHP echo get_permalink(); ?>">
         <?PHP echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
         <div class="row mobilemargin">
          <div class="categorytiletextsector1">
            <div class="col-md-6 col-sm-6 col-xs-12 noPR"><?PHP echo get_the_post_thumbnail( $page->ID,'categoryimage',array('class' => 'sector1img hovereffect')); ?> </div>
            <div class="col-md-6 col-sm-6 col-xs-12">
              <div class="testdiv">
               <h5><?PHP the_tITle(); ?></h5>
               <p><?PHP the_excerpt(); ?></p>
             </div>
           </div>
         </div>
       </div>
       <?PHP echo "</div>"; ?>

     </a>
   <?PHP endwhile; ?>
   <?PHP wp_reset_query(); ?>

现在,我的问题是,这将在每个类别页面显示所选标签,因为它在分类页面上设置.

如何才能在当前类别中设置此项.

因此,如果我的项目位于“类别A”中,则只有“A”的类别页面会使用项目类别显示此项?

任何帮助都会很棒

编辑.
使用此代码,希望这应该工作,但没有运气

$args = array(
    'tag_slug__and' => array( 'sector1' ),'post_type'     => array( 'sectors' ),'tax_query'     => array(
        array(
            'taxonomy' => 'sectors','terms'    => get_queried_object_id(),),);
您的问题是您的自定义查询.这里一个非常重要的注意事项是,永远不要更改主查询在任何类型的存档页面或主页上用自定义查询替换.我最近在 this post详细解释了一切.请务必阅读它和所有链接的帖子,因为这将使您受益匪浅

您的解决方案是删除自定义查询,并将其替换为我们都知道认循环

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        // Your template tags and htML mark up

    }
}

如果您需要更改主查询中的任何内容,请使用pre_get_posts执行此操作

编辑

您最好的想法是使用完整的tax_query来显示所选分类语和标记中的帖子

您可以尝试这样的事情:(至少需要PHP 5.4.此外,这个未经测试)

$q = get_queried_object();
$args = [
    'post_type' => 'sectors','tax_query' => [
        [
            'taxonomy' => $q->taxonomy,'terms' => $q->term_id,'include_children' => false // Exclude child terms
        ],[
            'taxonomy' => 'post_tag','field' => 'slug','terms' => 'sector1',//I believe this is the slug
        ],],];

对于较旧的PHP版本,请使用以下内容

$q = get_queried_object();
$args = array(
    'post_type' => 'sectors','tax_query' => array(
        array(
            'taxonomy' => $q->taxonomy,'include_children' => false // Exclude child terms
        ),array(
            'taxonomy' => 'post_tag',//I believe this is the slug
        ),);

编辑2

要排除sector1标记和任何其他sectorX标记中的帖子,您可以执行以下操作

您可以尝试这样的事情:(至少需要PHP 5.4.此外,//I believe this is the slug 'operator' => 'NOT_IN' ],//I believe this is the slug 'operator' => 'NOT_IN' ),);

请注意,您可以像这样将一系列标签传递给terms参数

'terms' => array( 'sector1','sector2','etc' ),

或短数组语法

'terms' => ['sector1','etc'],

编辑3

由于这是您的主要查询,因此您需要进行一些更改.正如我所说,删除自定义查询.你的主循环看起来应该是这样的

<?PHP if (have_posts()) : ?> 
    <?PHP while (have_posts()) : the_post(); ?> 
        <a href="<?PHP echo get_permalink(); ?>"> 
        <?PHP echo "<div class='col-md-6 col-sm-6 col-xs-12' style='margin-bottom:30px;'>"; ?> 
        <div class="row mobilemargin"> 
            <div class="categorytiletext2"> 
                <div class="col-md-6 col-sm-12 col-xs-12 nopr"><?PHP echo get_the_post_thumbnail( $page->ID,array('class' => 'hovereffect newimgheight')); ?> </div> 
                <div class="col-md-6 col-sm-12 col-xs-12 mobilewhite"> 
                    <div class="testdiv"> 
                        <h5 class="captext"><?PHP the_title(); ?></h5> 
                        <?PHP $trimexcerpt = get_the_excerpt(); 

                        $shortexcerpt = wp_trim_words( $trimexcerpt,$num_words = 10,$more = '… ' ); 

                        echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>'; 

                        ?> 
                    </div> 
                </div> 
            </div> 
        </div> 
        <?PHP echo "</div>"; ?> 

        </a> 
        <!-- If there is no posts,display an error message --> 
    <?PHP endwhile; 
else: ?> 
    <p><?PHP _e('Sorry,no posts matched your criteria.'); ?></p> 
<?PHP endif; ?> 
<!-- If there is no posts,display an error message -->

您现在可以使用pre_get_posts从分类页面删除所需的标记.在functions.PHP中,执行以下操作:(需要PHP 5.3,并且未经测试)

add_action( 'pre_get_posts',function ( $q )
{
    if ( !is_admin() && $q->is_main_query() &amp;& $q->is_tax() ) {
       $q->set( 'tag__not_in',array( 145 ) );
    }
});

对于旧版本使用

add_action( 'pre_get_posts','so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
       $q->set( 'tag__not_in',array( 145 ) );
    }
}

只需记住将145更改为您的确切标记ID或标记数组

编辑4

如果您没有标记ID,则可以使用get_term_by()从标记slug中获取标记ID.这样的事情会做:(需要PHP 5.3,并且还未经测试)

add_action( 'pre_get_posts',function ( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_object = get_term_by( 'slug','sector1','post_tag' ); 
        $tagID = $tag_object->term_id; 

       $q->set( 'tag__not_in',array( $tagID ) );
    }
});

对于旧版本使用

add_action( 'pre_get_posts','so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_object = get_term_by( 'slug',array( $tagID ) );
    }
}

如果您有一系列标签段,则可以替换以下内容

$tag_object = get_term_by( 'slug','post_tag' ); 
$tagID = $tag_object->term_id; 

$q->set( 'tag__not_in',array( $tagID ) );/*

$tag_array = array( 'slug1','slug2','slug3' );
foreach ( $tag_array as $tag ) {
    $tag_object = get_term_by( 'slug',$tag,'post_tag' ); 
    $tagids[] = $tag_object->term_id;
} 
$q->set( 'tag__not_in',$tagids );

只记得相应地改变slu ..

编辑5

你在带有pre_get_posts的functions.PHP中的最终代码应该是

add_action( 'pre_get_posts','so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_array = array( 'sector1','sector3','sector4' );
        foreach ( $tag_array as $tag ) {
            $tag_object = get_term_by( 'slug','post_tag' ); 
            $tagids[] = $tag_object->term_id;
        } 
        $q->set( 'tag__not_in',$tagids );    
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在分类页面上显示标签全部内容,希望文章能够帮你解决php – 在分类页面上显示标签所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。