当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to restrict what tags are fetched in WordPress

问题描述:

I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)

Here is the code:

<?php

$tags = get_tags('order=ASC&orderby=name);

foreach ( $tags as $tag ) { ?>

<?php $tag_name = $tag->slug;

echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';

echo '<div class="green_txt font20">' .$tag->name. '</div>';

//echo $tag_name;

$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );

query_posts( $args );

// The Loop

?>

<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

wp_reset_postdata(); }?>

If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!

Thanks muchly!

网友答案:

This is a basic example of WP_Query which should get you started:

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

    endwhile;
endif;
wp_reset_postdata();?>
网友答案:

get_tags function accepts include argument. You could pass by it ids of tags you want to get.

$tags = get_tags('order=ASC&orderby=name');

change to

$tags = get_tags('order=ASC&orderby=name&include=12,45,67');

12, 45 and 67 are ids of tags

get_tags on codex

分享给朋友:
您可能感兴趣的文章:
随机阅读: