0

I'm trying to add a location tag to my wordpress posts that will appear under the title? Like the example below. Does anyone have any suggestions for how I could do this? I looked in the wordpress codex and there appears to be nothing. I read something about taxonomies, would that be my best solution? If so any insight for how I could achieve this. Thanks in advance.

example...

enter image description here

my single.php

<?php
get_header();
the_post_thumbnail('banner-image'); 
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post">

<?php wpb_set_post_views(get_the_ID()); ?>
    <div class="post-title"><?php the_title(); ?></div>
    <div class="post-content"><?php the_content(); ?></div>

<?php comments_template(); ?>
</article>
<?php endwhile;
else :
echo '<p>No content found</p>';
endif;
get_footer();

?>

1 Answer 1

1

I think a custom taxonomy would be better served for this than a tag. I can see tags more used (in your case) to words more like "adventure, family, lodging" etc.

You might need to modify registration to meet your needs. GenerateWP has a step-by-step walkthrough to generate taxonomies that might be helpful (https://generatewp.com/taxonomy/) or Custom Post Type UI (plugin).

// Register Custom Taxonomy
function location() {

    $labels = array(
        'name'                       => 'Locations',
        'singular_name'              => 'Location',
        'menu_name'                  => 'Location',
        'all_items'                  => 'All Items',
        'parent_item'                => 'Parent Item',
        'parent_item_colon'          => 'Parent Item:',
        'new_item_name'              => 'New Item Name',
        'add_new_item'               => 'Add New Item',
        'edit_item'                  => 'Edit Item',
        'update_item'                => 'Update Item',
        'view_item'                  => 'View Item',
        'separate_items_with_commas' => 'Separate items with commas',
        'add_or_remove_items'        => 'Add or remove items',
        'choose_from_most_used'      => 'Choose from the most used',
        'popular_items'              => 'Popular Items',
        'search_items'               => 'Search Items',
        'not_found'                  => 'Not Found',
        'no_terms'                   => 'No items',
        'items_list'                 => 'Items list',
        'items_list_navigation'      => 'Items list navigation',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'location', array( 'post' ), $args );

}
add_action( 'init', 'location', 0 );

You can then echo the location name in your loop like so:

<?php echo the_terms( $post->ID, 'location', 'Location: ', ', ', ' ' );?>
6
  • See above (edited).
    – Aibrean
    Commented Dec 22, 2016 at 17:24
  • I was hoping I could have my post category and my location separate. I've re-uploaded the example photo above. As you can see they have the category "travel" above the post tittle. And the location below the post title. Is this possible with this method? Commented Dec 22, 2016 at 17:38
  • You need to make a new taxonomy for location. I'll update to show how it would work with a taxonomy.
    – Aibrean
    Commented Dec 22, 2016 at 17:41
  • It works. Wow, thank you being so helpful! Thank you so much! Commented Dec 22, 2016 at 18:08
  • I'm sorry, I know you already answered my question. But is there a way to remove the links from this taxonomy. For example if I made it Location: California the "california" will show up as a link and I would prefer it not to be a link. Commented Dec 22, 2016 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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