• canwild

    (@canwild)


    My posts have an activity tag (e.g, fishing, hunting, etc.) and a location tag (e.g., BC, Alberta, etc.). I am using archive.php in my template to display posts which have, say, both a ‘fishing’ tag and a ‘BC’ tag using a URL like ‘domain.com/category/fishing+bc’.

    How do I get the tag names and combine them into a page title like ‘Fishing in BC’?

    For a URL like ‘domain.com/category/fishing’, I want a page title like ‘Fishing in Canada’, and for a URL like ‘domain.com/category/bc’, I want a page title like ‘All Activities in BC’.

Viewing 3 replies - 1 through 3 (of 3 total)
  • banago

    (@banago)

    You have to create a custom page template.

    1. Create an php file on your theme directory by coping the page.php
    2. Add <?php /* Template Name: Fishing */ ?> at the top of the template.
    3. Add <?php query_posts('category_name=fishing'); ?> just above the loop i.e. <?php if(have_posts()) while ....

    Read more here: http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters

    Thread Starter canwild

    (@canwild)

    That works okay for a couple of activities and locations, but I have several of each and want to produce pages with useful titles on each of the combinations, including groupings of activities for a location (e.g., Fishing and Hunting in BC).

    I understand that get_query_var(‘tag’) in archive.php will return ‘fishing+bc’ from the query generated by ‘http://domain.com/category/fishing+bc&#8217;. I would like to explode this to get the individual tags, then get the tag names that go with the tag slugs and combine the names in a way that makes sense as a page title.

    If I have the tag slugs from get_query_var(‘tag’), how do I get the tag names?

    Thread Starter canwild

    (@canwild)

    I eventually came up with the following which works for my limited application. My activity specific tags are all named like “Fishing in Canada” with slugs like “fishing”. My location specific tags are all named like “Activities in British Columbia” with slugs like “bc”. When I call archive.php using two tags (e.g., “fishing+bc”) the following code finds the 2 tags names and joins them back together as “Fishing in British Columbia”. The code bypasses situations where there is only one tag in the query. The code does not handle “bc+fishing” just “fishing+bc”. With 20 or so activities and 10 provinces, this is easier than coding a separate page for each combination.

    $page_title = wp_title('', false);
    	$tags = get_query_var('tag');
    	$tag = explode(' ',$tags);
    	if (count($tag)>1) {
    	$tag_name = get_tags(array('slug'=>$tag[0]));
    	$page_title = str_replace(' in Canada','', $tag_name[0]->name);
    	$tag_name = get_tags(array('slug'=>$tag[1]));
    	$page_title .= str_replace('Activities','', $tag_name[0]->name);
    	}

    Not very elegant but it works.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display a title based on multiple tags’ is closed to new replies.