0

When having this:

$tag_list = get_the_tag_list( '', ', ' );

I substituted the meta print:

printf(
$tag_list,
$category:list,
etc
);

by this:

$printArray_tag_list = explode(', ', $tag_list);
echo $category_list."/".$printArray_tag_list[0].','.$printArray_tag_list[1];

to print only the first two tags from the list (the first tags inserted in the post editor). Only then I realized that they are alphabetically ordered and those are not the tags I want to print.

Is there a way to print selected tags? Maybe selecting them in the custom fields?

2 Answers 2

2

By get_the_tags() you receive an array of tags attached to the current post. So you could do the following:

$tags = get_the_tags();
$tag_ids_to_print = array( 1, 2, 3 ); //List of Tag IDs which you want to be printed

$print_tags = array();
if( is_array( $tags ) ){
    foreach( $tags as $tag ){
        if( in_array( $tag->term_id, $tag_ids_to_print ) )
            $print_tags[] = $tag;
    }
}

$tag_list = '';
if( count( $print_tags ) > 0 ){
    //Execute only, when tags found
    $i = 0;
    foreach( $print_tags as $tag ){
        if( $i == 1 ) $tag_list .= ', ';
        $tag_list .= '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>';
        $i = 1;
    }
}

Docs:

https://codex.wordpress.org/Function_Reference/get_the_tags

18
  • +1 but you might want to handle the case when the post is without any tags to avoid PHP warnings.
    – birgire
    Apr 11, 2015 at 16:20
  • thanks. i am not quite sure, i think you just get back an empty array so that count( $print_tags ) > 0 should be enough, but I added an if( is_array( $tags ) ) to be sure :) Apr 11, 2015 at 17:22
  • ps: get_the_tags() returns false in that case, hence the possible PHP warnings.
    – birgire
    Apr 11, 2015 at 17:28
  • thx. Good to know! Apr 11, 2015 at 17:31
  • 1
    Couldn't fix the messy comment here above. I'll rethink my approach as I can have a hard time understanding how to do this. Thanks for pointing out a direction. I like the idea of having the selected tags be printed in an automated way. But maybe I'll research more on the answer from birgire and have the authors insert the tags to be displayed through custom fields. Just saw your comment.I'll give it a try then. Thanks! Apr 12, 2015 at 9:19
1

Custom Field Tag filter:

You can try the following code snippet to filter tags from a custom field:

/**
 * Support the 'include_tags' custom field (comma seperated tag slugs) 
 * to filter out those tags to display.
 *
 * @see http://wordpress.stackexchange.com/a/183929/26350
 */

! is_admin() && add_filter( 'get_the_terms', function( $terms, $post_id, $taxonomy )
{
    if(    'post_tag' === $taxonomy 
        && ! empty( $terms ) 
        && $include = get_post_meta( $post_id, 'include_tags', true ) 
    ){      
        // We only want lower case and trimmed strings from the custom meta field:
        $include_tags = array_map( 'trim', array_map( 'strtolower', explode( ',', $include ) ) );

        // Let's filter out the terms to display:
        if( ! empty( $include_tags ) )
            $terms = array_filter( $terms, function( $t ) use ( $include_tags ) {               
                return in_array( $t->slug, $include_tags );
            });
    }
    return $terms;
}, 10, 3 );

For example if you use the_tags() in your template and have the following tags in a given post:

tags

then you can add the custom field include_tags with a comma seperated list of tags you want to filter out:

cf

Then it will show like this on your front end:

display

Similarly we could support exclude_tags filtering with:

return ! in_array( $t->slug, $include_tags );

instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.