在分类页面上显示标签

8 php tags wordpress taxonomy custom-post-type

我目前在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(); ?>
Run Code Online (Sandbox Code Playgroud)

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

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

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

任何帮助都会很棒

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

$args = array(
    'tag_slug__and' => array( 'sector1' ),
    'post_type'     => array( 'sectors' ),
    'tax_query'     => array(
        array(
            'taxonomy' => 'sectors',
            'terms'    => get_queried_object_id(),
        ),
    ),
);
Run Code Online (Sandbox Code Playgroud)

Pie*_*sen 1

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

\n\n

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

\n\n
if ( have_posts() ) {\n    while ( have_posts() ) {\n        the_post();\n\n        // Your template tags and html mark up\n\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您需要更改主查询中的任何内容,请使用pre_get_posts这样做

\n\n

编辑

\n\n

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

\n\n

你可以尝试这样的事情:(至少需要 PHP 5.4+。此外,这未经测试

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

对于较旧的 PHP 版本,请使用以下命令

\n\n
$q = get_queried_object();\n$args = array(\n    \'post_type\' => \'sectors\',\n    \'tax_query\' => array(\n        array(\n            \'taxonomy\' => $q->taxonomy,\n            \'terms\' => $q->term_id,\n            \'include_children\' => false // Exclude child terms\n        ),\n        array(\n            \'taxonomy\' => \'post_tag\',\n            \'field\' => \'slug\',\n            \'terms\' => \'sector1\', //I believe this is the slug\n        ),\n    ),\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑2

\n\n

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

\n\n

你可以尝试这样的事情:(至少需要 PHP 5.4+。此外,这未经测试

\n\n
$q = get_queried_object();\n$args = [\n    \'post_type\' => \'sectors\',\n    \'tax_query\' => [\n        [\n            \'taxonomy\' => $q->taxonomy,\n            \'terms\' => $q->term_id,\n            \'include_children\' => false // Exclude child terms\n        ],\n        [\n            \'taxonomy\' => \'post_tag\',\n            \'field\' => \'slug\',\n            \'terms\' => \'sector1\', //I believe this is the slug\n            \'operator\' => \'NOT_IN\'\n        ],\n    ],\n];\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于较旧的 PHP 版本,请使用以下命令

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

请注意,您可以将标签数组传递给参数,terms如下所示

\n\n
\'terms\' => array( \'sector1\', \'sector2\', \'etc\' ),\n
Run Code Online (Sandbox Code Playgroud)\n\n

或短数组语法

\n\n
\'terms\' => [\'sector1\', \'sector2\', \'etc\'],\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑3

\n\n

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

\n\n
<?php if (have_posts()) : ?> \n    <?php while (have_posts()) : the_post(); ?> \n        <a href="<?php echo get_permalink(); ?>"> \n        <?php echo "<div class=\'col-md-6 col-sm-6 col-xs-12\' style=\'margin-bottom:30px;\'>"; ?> \n        <div class="row mobilemargin"> \n            <div class="categorytiletext2"> \n                <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> \n                <div class="col-md-6 col-sm-12 col-xs-12 mobilewhite"> \n                    <div class="testdiv"> \n                        <h5 class="captext"><?php the_title(); ?></h5> \n                        <?php $trimexcerpt = get_the_excerpt(); \n\n                        $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = \'\xe2\x80\xa6 \' ); \n\n                        echo \'<a href="\' . get_permalink() . \'"><p>\' . $shortexcerpt . \'</p></a>\'; \n\n                        ?> \n                    </div> \n                </div> \n            </div> \n        </div> \n        <?php echo "</div>"; ?> \n\n        </a> \n        <!-- If there is no posts, display an error message --> \n    <?php endwhile; \nelse: ?> \n    <p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p> \n<?php endif; ?> \n<!-- If there is no posts, display an error message -->\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
add_action( \'pre_get_posts\', function ( $q )\n{\n    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {\n       $q->set( \'tag__not_in\', array( 145 ) );\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于旧版本使用

\n\n
add_action( \'pre_get_posts\', \'so30256167_remove_tags\' );\nfunction so30256167_remove_tags( $q )\n{\n    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {\n       $q->set( \'tag__not_in\', array( 145 ) );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

编辑4

\n\n

如果您没有标签 ID,则可以使用get_term_by()标签 slug 获取标签 ID。像这样的事情就可以了:(需要 PHP 5.3+,并且也未经测试

\n\n
add_action( \'pre_get_posts\', function ( $q )\n{\n    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {\n        $tag_object = get_term_by( \'slug\', \'sector1\', \'post_tag\' ); \n        $tagID = $tag_object->term_id; \n\n       $q->set( \'tag__not_in\', array( $tagID ) );\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于旧版本使用

\n\n
add_action( \'pre_get_posts\', \'so30256167_remove_tags\' );\nfunction so30256167_remove_tags( $q )\n{\n    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {\n        $tag_object = get_term_by( \'slug\', \'sector1\', \'post_tag\' ); \n        $tagID = $tag_object->term_id; \n\n       $q->set( \'tag__not_in\', array( $tagID ) );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
$tag_object = get_term_by( \'slug\', \'sector1\', \'post_tag\' ); \n$tagID = $tag_object->term_id; \n\n$q->set( \'tag__not_in\', array( $tagID ) );/*\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
$tag_array = array( \'slug1\', \'slug2\', \'slug3\' );\nforeach ( $tag_array as $tag ) {\n    $tag_object = get_term_by( \'slug\', $tag, \'post_tag\' ); \n    $tagids[] = $tag_object->term_id;\n} \n$q->set( \'tag__not_in\', $tagids );\n
Run Code Online (Sandbox Code Playgroud)\n\n

只需记住相应地更改段头即可

\n\n

编辑5

\n\n

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

\n\n
add_action( \'pre_get_posts\', \'so30256167_remove_tags\' );\nfunction so30256167_remove_tags( $q )\n{\n    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {\n        $tag_array = array( \'sector1\', \'sector2\', \'sector3\', \'sector4\' );\n        foreach ( $tag_array as $tag ) {\n            $tag_object = get_term_by( \'slug\', $tag, \'post_tag\' ); \n            $tagids[] = $tag_object->term_id;\n        } \n        $q->set( \'tag__not_in\', $tagids );    \n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n