I've added tags to my Wordpress pages (which are working fine) but I'm trying to list them on the page.
The idea is to get all the tags a particular page (not post) has and format them. What I'm getting is either 2 random (unrelated) tags, or the entire tag list. I'm not sure what I'm missing here.
$relatedArea = the_slug(false); //gets the slug of the page - false represses the echo statement
$allTags = get_tags(); //gets all the tags
$areasOfLaw = array(); //starts the array
// check if the tag is included on the page
foreach ($allTags as $tag) {
if(has_tag($relatedArea)) {
$areasOfLaw[] = $tag; //add to the array
}
}
Tried $areasOfLaw = get_the_tags()
but it gave me random tags.
Here's the formatting, but I'm pretty sure it's working correctly.
foreach($areasOfLaw as $tag){
echo '
<a href="'.get_site_url().'/services/'.$tag->slug.'">
<li>'.$tag->name.'
<span class="glyphicon glyphicon-chevron-right pull-right action"></span>
</li></a>';
}
edit: this is my tag support included in the functions file
// add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
// ensure all tags are included in queries
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}
// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');