This is what I'm trying to achieve:
Originally I was trying to include the first tag of a post above the post on the homepage, using the following:
$tags_i = 0;
$tags = get_the_tags();
if ($tags) {
foreach ($tags as $tag) {
$tags_i++;
if ($tags_i == 1) { ?>
<h3><a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a></h3>
<?php
}
}
}
The issue is that this actually grabs the first tag in alphabetical order (it seems at least). I was originally hoping to grab the first tag assigned to that post.
I was wondering if I would be able to use custom tag to achieve this. I've never used custom tag types, and I can't really find much information on how they work, so any additional info would be helpful. Or, if you can suggest a solution to my problem, that would also be very appreciated.
This is what I'm trying to achieve:
Originally I was trying to include the first tag of a post above the post on the homepage, using the following:
$tags_i = 0;
$tags = get_the_tags();
if ($tags) {
foreach ($tags as $tag) {
$tags_i++;
if ($tags_i == 1) { ?>
<h3><a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a></h3>
<?php
}
}
}
The issue is that this actually grabs the first tag in alphabetical order (it seems at least). I was originally hoping to grab the first tag assigned to that post.
I was wondering if I would be able to use custom tag to achieve this. I've never used custom tag types, and I can't really find much information on how they work, so any additional info would be helpful. Or, if you can suggest a solution to my problem, that would also be very appreciated.
Share Improve this question edited Apr 23, 2014 at 9:06 cybmeta 20.6k5 gold badges47 silver badges57 bronze badges asked Apr 23, 2014 at 3:36 atbatb 1011 bronze badge 2- Can you define 'first'? Taxonomy terms don't have an order, and any term is just as much 'first' as any other term. – Tom J Nowell ♦ Commented Apr 23, 2014 at 9:08
- Perhaps your original problem would be better suited by asking how to define a 'primary tag' via custom meta. – Tom J Nowell ♦ Commented Apr 23, 2014 at 9:09
1 Answer
Reset to default 0Assign taxonomy terms (ie tags) to a post in a specific order has no sense form me unless the taxonomy is hierarchical. The core tags taxonomy is no-hierarchical and category is hierarchical. So, you can define a hierarchical custom taxonomy or use the built-in category taxonomy.
Think in a natural flow of description terms. In a no-hierarchical context, what description term should be the first? There is no reason to put one over another, you can order them just like you can order a string. In a hierarchical taxonomy the order can be define as you want or need.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745282528a4620361.html
评论列表(0条)