【问题标题】:Displaying Tags on taxonomy page在分类页面上显示标签
【发布时间】:2015-07-27 04:21:00
【问题描述】:

我目前在我的 CMS 中可以选择将标签添加到我的自定义帖子类型单页。

现在,我想将此标签显示为“特色”项目。 因此,在我的分类-'文件名'中,我使用以下代码收集标签并将它们显示在分类页面中:

            <?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(),
        ),
    ),
);

【问题讨论】:

  • 有人有想法吗????

标签: php wordpress tags custom-post-type taxonomy


【解决方案1】:

您的问题是您的自定义查询。这里有一个非常重要的注意事项,永远不要在任何类型的存档页面或主页上用自定义查询替换主查询。我最近在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,
            'terms' => $q->term_id,
            'include_children' => false // Exclude child terms
        ),
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'sector1', //I believe this is the slug
        ),
    ),
);

编辑 2

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

你可以尝试这样的事情:(至少需要 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
            'operator' => 'NOT_IN'
        ],
    ],
];

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

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

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

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

或短数组语法

'terms' => ['sector1', 'sector2', '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, 'categoryimage', 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() && $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 或标签数组ids

编辑 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', 'sector1', 'post_tag' ); 
        $tagID = $tag_object->term_id; 

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

如果你有一个标签 slugs 数组,你可以替换下面的

$tag_object = get_term_by( 'slug', 'sector1', '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 );

只要记住相应地改变 slug

编辑 5

你在functions.php中的最终代码应该是pre_get_posts

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', 'sector2', 'sector3', 'sector4' );
        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 );    
    }
}

【讨论】:

  • 我想我可能误解了你的问题
  • 循环正在工作,它正在获取被标记的帖子,但它会在每个类别中显示它。我只需要一种方法让它只显示在它相关的类别上,我以为我已经用 'terms' => get_queried_object_id(),
  • 好的,我不明白你的问题,这是你的主循环吗。
  • 这只是一个循环,它会获取任何带有名称“sector1”的标记帖子。这个循环工作正常,它获取标记的帖子并显示它。但它显示在每个类别中,我需要它只显示在它所属的类别中
  • 你的术语让我很困惑,sector1 到底是什么。你是如何创造这个的。您知道类别、分类法和术语之间的区别吗?你想要做什么。当您用我的答案中的循环替换所有代码(自定义循环)时会发生什么
猜你喜欢
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 2017-05-13
相关资源
最近更新 更多