如何只获取特定的标签ID

时间:2016-08-17 作者:Dan Hawkins

我有这个PHP脚本来获取标签(因为我们使用WordPress作为CMS,标签是我们合作伙伴的“类别”)。。。。然而,我正在尝试释放“标签”,以便在博客帖子上使用-但目前下面的代码获取所有标签-如何将其限制为仅限于我指定的标签(很乐意输入每个标签ID)

代码如下:

        <?php 
/*
Template Name: Find A Partner Index
*/
get_header(); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
  $(function() {
    $( "#tabs" ).tabs();
  });
  </script>
<style>
.head_padding {
    display: none;
}

.partner_tabs {
    padding:0;
    margin:0;
    list-style:none;
}

.nav-sidebar > .active,
.nav-sidebar > .active > a:hover,
.nav-sidebar > .active > a:active,
.nav-sidebar > .active > a:visited,
.nav-sidebar > .active > a:link,
.nav-sidebar > .inactive,
.nav-sidebar > .inactive > a:hover,
.nav-sidebar > .inactive > a:active,
.nav-sidebar > .inactive > a:visited,
.nav-sidebar > .inactive > a:link,
.nav-sidebar > .active > a,
.nav-sidebar > .active > a:hover {
    border-left: 2px solid #f08f00;
    color: #f08f00;
    padding-left: 12px
}
.nav-sidebar > .inactive > a {
    padding-left: 5px;
    font-family: \'Open Sans\', sans-serif
}
.nav-sidebar > .subinactive > a {
    font-size: .9rem;
    margin-left: 65px;
    margin-right: 45px
}
.nav-sidebar > .subinactive > a:hover {
    font-size: .9rem;
    margin-left: 65px;
    margin-right: 45px;
    border-left: 2px solid #f08f00;
    color: #f08f00;
    transition: all .3s ease-in-out;
    padding-left: 15px
}
.nav-sidebar > .inactive > a:hover {
    border-left: 2px solid #f08f00;
    color: #f08f00;
    padding-left: 10px;
    transition: all .3s ease-in-out;
    background-color: transparent
}
.partners_menu  {
    border-bottom:2px solid #e4e4e4;
    border-top:1px solid #fff;
}
.partner-more-icon {
    display: inline-block;
    width: 93px;
    height: 17px;
    background: url(/wp-content/themes/volo/images/volo-partner-details-button.png) no-repeat 0 0;
    text-indent: -2000em;
    z-index: 999;
    position:relative;
}
.ui-tabs-active a .partners_menu {
    border-left: 2px solid #f08f00;
    color: #f08f00;
    padding-left: 20px;
}
.sub_nav_background {
    border: 1px solid #d4d4d4;
    border-radius: 7px;
    background: url(/wp-content/themes/volo/images/partner-sub-nav-lng-bg.png) repeat-x 0 0;
    height:50px;
}
.sub_nav_background_shadow {
    border-radius: 7px;
    box-shadow: 4px 4px 6px #ebebeb;
}

.search-submit {
    border: 1px solid #008C83!important;
    border-radius:5px; 
    background-color: #02A79C!important;
    color:#fff;
    cursor: pointer;
    display: inline-block;
}
.region-button {
    border: 1px solid #008C83!important;
    border-radius:5px; 
    background-color: #02A79C!important;
    color:#fff;
    cursor: pointer;
    display: inline-block;
}
input[type="search"] {
    -webkit-appearance: none;
    background-color: rgba(255,255,255,.7);
    border-top: 1px solid #e4e4e4;
    border-left: 1px solid #e4e4e4;
    border-right: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    font-family: Open Sans;
    font-weight: normal;
    color: black;
    padding-left: 3px;
}
</style>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
$thumb_id = get_post_thumbnail_id(get_the_ID());
if(count($alt)) echo $alt;;
$thumb_url_array = wp_get_attachment_image_src($thumb_id, \'full\' , true);
$thumb_url = $thumb_url_array[0]; ?>

<div class="misc_banner_con para_1" style="background-image:url(<?php echo $thumb_url; ?>)"></div>
<div class="bottom_right_corner_bar">
<div class="padbot80">
<div class="container marbot30">
  <div class="row martop30">
    <div class="col-md-12">
      <h1 class="marbot20 font34 title green_txt">
        <?php the_title(); ?>
      </h1>
    </div>
  </div>
  <div class="row">
    <div class="col-md-<?php if(get_field(\'sidebar\')) { echo \'8\'; } else { echo \'12\'; }?>">
      <?php the_content(); ?>
    </div>
    <?php if(get_field(\'sidebar\')) { ?>
    <div class="col-md-4 lbreak padleft30 martop20 padtop30 padbot30"> <?php echo get_field(\'sidebar\');?> </div>
    <?php }?>
  </div>
  <?php endwhile; else : ?>
  <p>
    <?php _e( \'Sorry, no posts matched your criteria.\' ); ?>
  </p>
  <?php endif; ?>

  <div class="row grey_bg martop20 hbreak" id="tabs">
  <div  class="col-sm-3 padtop30 rbreak">
    <div class="pad10 red_txt font20 title">Partner type</div> 
  <ul class="nav-sidebar partner_tabs" style="padding-top:20px;">
    <?php 
            $tags = get_tags(\'order=ASC&orderby=name\');
            foreach ( $tags as $tag ) {
            $tag_name = $tag->slug;
                echo \'<li><a href="#tabs-\'.$tag->slug.\'">\';
                echo \'<div class="partners_menu green_txt padleft20 padright20 padtop10 padbot10 float_left marright20" style="width:100%;">\' .$tag->name. \'</div>\';
                echo \'</a></li>\';
                wp_reset_postdata(); }
     ?>
  </ul> 
  </div>
  <div class="col-sm-9 white_bg">   
    <div class="row padleft20 padtop5">
        <div class="sub_nav_background_shadow">
            <div class="sub_nav_background pad10">


                                    <div style="padding-bottom:9px;padding-top:1px;float:left;">
                                        <div class="search-box float">
                                            <form role="search" method="get" class="search-form" action="<?php echo get_permalink( 6411 ); ?>">
                                                <label>
                                                    <span class="screen-reader-text" style="font-family:Open Sans;font-weight:normal;">I want to search for:</span>
                                                    <input type="search" class="search-field" placeholder="Search our partners…" value="" name="swpquery" title="Search for:">
                                                </label>
                                                <input type="submit" class="search-submit" value="Search">
                                            </form>
                                        </div>
                                    </div>

                                    <div style="padding-left:40px;padding-bottom:9px;padding-top:1px;float:left;">
                                        <div class="search-box">
                                            <div class="dropdown">
                                                    <button class="region-button dropdown-toggle" type="button" data-toggle="dropdown">Region
                                                    <span class="caret"></span></button>
                                                    <ul class="dropdown-menu">
                                                      <li><a href="#">UK</a></li>
                                                      <li><a href="#">US</a></li>
                                                      <li><a href="#">All</a></li>
                                                    </ul>
                                            </div>
                                        </div>
                                    </div>


            </div>
        </div>
    </div>
    <div class="row padleft10 padtop30">
<?php
$args = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10,
    \'orderby\' => \'slug\',
    \'order\' => \'ASC\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'category\',
            \'field\' => \'term_id\',
            \'field\' => 11
        ),
        array(
            \'taxonomy\' => \'tag\',
            \'field\' => \'term_id\',
            \'field\' => array( /* Your Tag IDs */ ),
            \'operator\' => \'IN\'
        )
    )
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
    while ( $post_query->have_posts() ) :
        $post_query->the_post();

        // Inner loop stuff code
        ?>
                        <div class="row">
                          <?php
                        while ( have_posts() ) : the_post();
                        $thumb_id = get_post_thumbnail_id();
                        $thumb_url_array = wp_get_attachment_image_src($thumb_id, \'medium\', true);
                        $thumb_url = $thumb_url_array[0];
                        $post_id = get_the_ID(); 
                        ?>
                          <div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
                            <div class="partners_container">
                                <div class="row" style="height:125px;">
                                    <div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%"  alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>

                                </div>
                              <div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo \'<div>\' . the_title() . \'</div>\';?> </div>
                              <div class="padtop10 left"> <?php echo \'<div>\' . the_field(\'partner_page_description\', $post_id ) . \'</div>\';?>  </div>
                            </div>
                          </a> </div>
                          <?php
                        endwhile; 
                            ?>
                      </div>
          </div>
          <?php
            endif;
wp_reset_postdata();?>
    </div>

</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
如果有人能这么好心地告诉我如何只获取特定的标签ID,那将是绝对完美的!

非常感谢!

1 个回复
最合适的回答,由SO网友:Krzysztof Grabania 整理而成

get_tags 函数接受include 论点您可以传递想要获取的标签的ID。

$tags = get_tags(\'order=ASC&orderby=name\');
更改为

$tags = get_tags(\'order=ASC&orderby=name&include=12,45,67\');
12、45和67是标签的ID

get_tags on codex

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果