在分类页面上显示标签

用户名

我目前在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(),
        ),
    ),
);
彼得·古森

您的问题是您的自定义查询。这里有一个非常重要的说明,那就是永远不要更改在任何类型的存档页面或主页上用自定义查询替换主查询。最近这篇文章中详细解释了所有内容请务必阅读它和所有链接的帖子,因为这将使您受益匪浅

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

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或标签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', 'sector1', 'post_tag' ); 
        $tagID = $tag_object->term_id; 

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

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

$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 );

只要记住要相应地改变弹头

编辑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 );    
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

在WooCommerce商店页面上显示自定义分类术语

来自分类Dev

在.NetCore Razor页面上显示文本标签的最佳实践

来自分类Dev

如何在子页面上显示离子标签?

来自分类Dev

ACF字段未显示在wordpress自定义分类页面上

来自分类Dev

在页面上显示JSON?

来自分类Dev

在页面上显示数组

来自分类Dev

如何制作标签分类页面?

来自分类Dev

页面上的Jekyll标签列表

来自分类Dev

图片未显示在页面上-在新标签页中打开会显示index.php

来自分类Dev

快速标签栏试图在某些页面上显示登录视图控制器

来自分类Dev

值未分配给angularjs控制器MVC在布局页面上显示的标签

来自分类Dev

从aspx页面上显示的本地文本文件替换标签

来自分类Dev

如何在WooCommerce的同一产品页面上显示产品标签

来自分类Dev

是否可以在页面上显示的所有图像上添加<br>标签?

来自分类Dev

替换显示在aspx页面上的本地文本文件中的标签

来自分类Dev

在Wordpress中在页面上显示带有特定标签的文本

来自分类Dev

如何在WooCommerce的同一产品页面上显示产品标签

来自分类Dev

为什么我的页面上的结束正文和 html 标签在括号中显示为红色?

来自分类Dev

使用自定义 html 标签在页面上显示图像

来自分类Dev

在页面上多次隐藏/显示

来自分类Dev

在特定页面上显示图片

来自分类Dev

在特定页面上显示内容

来自分类Dev

在页面上显示加载时间

来自分类Dev

页面上的语言未显示

来自分类Dev

在WPF页面上显示图像

来自分类Dev

在我的页面上显示图像

来自分类Dev

在特定页面上显示内容

来自分类Dev

在页面上显示数据,wordpress

来自分类Dev

无法在页面上显示JSON

Related 相关文章

热门标签

归档