• Resolved jondownin

    (@jondownin)


    I am trying to get post taxonomies to show up as linked images, but have hit a wall with my extremely limited PHP knowledge. I found the following code that will accomplish what I want, only with categories:

    <h3><b>Posted in:</b></h3>
    <?php foreach((get_the_category()) as $cat)
    	{
    	$catname =$cat->category_nicename;
    	echo "<a href=\"/category/";
    	echo $catname;
    	echo "/\">";
    	echo "<img src=\"/wp-content/themes/gtd1.0/images/";
    	echo $catname;
    	echo ".jpg\" alt=\"$catname category image\" border=\"0\" /></a>\n";
    	}
    ?><br />

    This results in images with the same name as the category being pulled from a specified folder and being used as category page links. I tried modifying this for a “people” taxonomy:

    <h3><b>Posted in:</b></h3>
    <?php foreach((get_the_terms($post->ID, 'people')) as $tax)
    	{
    	$taxname =$tax->slug;
    	echo "<a href=\"/people/";
    	echo $tax;
    	echo "/\">";
    	echo "<img src=\"/wp-content/themes/gtd1.0/images/";
    	echo $tax;
    	echo ".jpg\" alt=\"$tax category image\" border=\"0\" /></a>\n";
    	}
    ?><br />

    This returns a link to “…/people/Array” instead of a “…/people/person” result. Could anyone lend a hand and help me with the code for taxonomies? Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Change both instances of echo $tax; to echo $taxname.

    Thread Starter jondownin

    (@jondownin)

    Caught that, changed it and it still isn’t quite functioning. Now instead of “…/people/person” I’m just getting “…/people//”. Thanks for the insight though.

    Anyone else have any ideas?

    Do you still have $taxname = $tax->slug; at the beginning of the loop? Your result suggests you don’t and therefore echo $taxname would obviously output nothing. If you don’t have it, use echo $tax->slug; instead.

    Thread Starter jondownin

    (@jondownin)

    Here is my current code that returns “../people//”:

    <h3><b>Posted in:</b></h3>
    <?php foreach((get_the_terms($post->ID, 'people')) as $tax)
    	{
    	$taxname =$tax->slug;
    	echo "<a href=\"/people/";
    	echo $taxname;
    	echo "/\">";
    	echo "<img src=\"/wp-content/themes/gtd1.0/images/";
    	echo $taxname;
    	echo ".jpg\" alt=\"$tax category image\" border=\"0\" /></a>\n";
    	}
    ?><br />
    Thread Starter jondownin

    (@jondownin)

    Oops, double post. Damn.

    Thread Starter jondownin

    (@jondownin)

    …and triple.

    Ok, try adding the following above your loop and let me know if it outputs anything:

    <?php var_dump(get_the_terms($post->ID, 'people')); ?>

    It should output a whole block of jibberish looking text.

    Thread Starter jondownin

    (@jondownin)

    Okay, found a big error on my part. I didn’t have the taxonomies registered in my test theme. Now, after registering the taxonomies in my function.php, nothing shows up where the code should execute.

    Here the code output by the code you said to insert before the loop:

    array(3) { [39]=> object(stdClass)#185 (10) { ["term_id"]=> string(2) "39" ["name"]=> string(16) "Anthony Gallegos" ["slug"]=> string(16) "anthony-gallegos" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(4) "2031" ["taxonomy"]=> string(6) "people" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(2) "61" ["object_id"]=> string(5) "11363" } [429]=> object(stdClass)#186 (10) { ["term_id"]=> string(3) "429" ["name"]=> string(11) "Arthur Gies" ["slug"]=> string(11) "arthur-gies" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(4) "2030" ["taxonomy"]=> string(6) "people" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(2) "41" ["object_id"]=> string(5) "11363" } [40]=> object(stdClass)#196 (10) { ["term_id"]=> string(2) "40" ["name"]=> string(16) "Matt Chandronait" ["slug"]=> string(16) "matt-chandronait" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(4) "1973" ["taxonomy"]=> string(6) "people" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(2) "48" ["object_id"]=> string(5) "11363" } }

    Thanks for sticking with me on this. I appreciate the help!

    If you get that block of text then the rest of your code should work so you can remove it now.

    I did notice another mistake though. In the following line,

    echo ".jpg\" alt=\"$tax category image\" border=\"0\" /></a>\n";

    it should be ".$tax->name." instead of just $tax.

    Thread Starter jondownin

    (@jondownin)

    Thanks man! That last change totally made it functional! Really appreciate it!

    You’re welcome.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Taxonomy image link help’ is closed to new replies.