dshtze500055 2015-05-16 11:31
浏览 40

仅显示WordPress中某些类别的标签

I currently have a custom post type, named Sectors. This has categories too. Sectors

    add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {

register_post_type( 'sectors',
    array(
        'labels' => array(
            'name'          => __( 'Sectors' ),
            'singular_name' => __( 'sector' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => 'dashicons-heart',
        'public'       => true,
        'rewrite'      => array( 'slug' => 'sectors', 'with_front' => false ),
        'supports'     => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies'   => array( 'sectors', 'post_tag' ),
    ));

}

In here, I have individual sectors, which can have tags attatched Tags

What I am currently trying to do, is add tags to a certain sector, and the tagged sector will be a 'featured sector' In its category page.

Using the code below, on my taxonomy-sectors.php page, I can do this:

    <?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>

The Issue is, this displays these tagged sectors on EVERY category page, as all categories use the taxonomy page. Is there a way, that it would only display these tagged sector pages to its own category? No sectors can be in 1 category, if that helps.

Any help would be great, this has been bugging me for days now :(

EDIT

The code below is from an answer, but this does not return anything now, is there something missing??

$args = array(
    'tag_slug__and' => array( 'sector1' ),
    'post_type'     => array( 'sectors' ),
    'tax_query'     => array(
        array(
            'taxonomy' => 'sectors',
            'terms'    => get_queried_object_id(),
        ),
    ),
);
  • 写回答

2条回答 默认 最新

  • dousi1970 2015-05-16 12:38
    关注

    try this one

    <div class="col-md-6 col-sm-6 col-xs-12"> 
       <div class="testdiv"> 
              <h5><?php the_title(); ?></h5> 
              <p><?php the_excerpt(); ?></p> 
              <p><?php the_tags( 'Tags: ', ', ', '<br />' ); ?></p> 
       </div> 
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)