• deepbevel

    (@deepbevel)


    This is code which makes an array of tags in a category. I’m trying to make it a list of actual tag links.

    <?php
    	query_posts('category_name=work');
    	if (have_posts()) : while (have_posts()) : the_post();
            $posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
    			}
    		}
    	endwhile; endif; 
    
    	$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
    	echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY
    
    ?>

    I found this advice but have had no luck applying it:

    Basically, you have to use a foreach instruction on the $tags_arr array.

    Example:

    foreach ($tags_arr as $tag) {
    echo “<li>”.$tag.”</li>”;
    }

    Any help appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael

    (@alchymyth)

    a:
    make sure not to copy/paste anything with squint quotation marks.
    only use ' and "

    b:
    http://codex.wordpress.org/Function_Reference/get_tag_link

    having an array with only tag names makes this more difficult.

    try to change this section:
    $all_tags_arr[] = $tag -> name;
    to:
    $all_tags_arr[] = $tag;

    then this might work:

    foreach ($tags_arr as $tag) {
    echo '<li><a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a></li>';
    }

    don’t forget the <ul> tags

    (untested)

    Thread Starter deepbevel

    (@deepbevel)

    Thank you, however I think I’m having the same problem knowing how to apply these edits. I don’t know where to add the

    foreach ($tags_arr as $tag) {
    echo '<li><a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a></li>';

    }

    Do I replace part of the original code or add this at the end?

    Sorry about the “squint quotation marks”, I don’t know what that is or why it’s wrong but hopefully I won’t do it again.

    Thread Starter deepbevel

    (@deepbevel)

    This is what I tried

    <?php
    	query_posts('category_name=uncategorized');
    	if (have_posts()) : while (have_posts()) : the_post();
            $posttags = get_the_tags();
    		if ($posttags) {
    			foreach ($tags_arr as $tag) {
    echo '<li><a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a></li>';
    }
    			}
    
    	endwhile; endif; 
    
    	$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
    	echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY
    
    ?>

    this is the error:

    Warning: Invalid argument supplied for foreach() in /home2/fourstic/public_html/TravelBlog3000/wp-content/plugins/wp-php-widget/wp-php-widget.php(52) : eval()’d code on line 6

    So I don’t know if it works because I don’t know if I’m doing what you intended with your suggestion. I obviously don’t understand the functions involved here or how it’s working, so It’s all trial and error for me.

    Michael

    (@alchymyth)

    my idea $all_tags_arr[] = $tag; did not work.

    general, the foreach loop would replace the print_r line of your code.

    first step, build an array of tag ids; then make them unique; then output them in the froeach loop, getting the name from the id.

    this seems to be working:

    http://pastebin.com/e8j4h9BH

    —-
    about the ‘squint’ quotation marks:

    this is what you posted:
    echo “<li>”.$tag.”</li>”;
    see the quotation marks, probably made this way by a text editor.
    these would not work in code.
    they should read like this:
    echo "<li>".$tag."</li>";

    Thread Starter deepbevel

    (@deepbevel)

    Thanks so much, yes it works perfectly, I’ve been struggling with this for weeks.
    I think I understand about replacing the print_r with the foreach. I was under the impression that an array had to use print_r.
    You really made my day, thanks again!

    I understand now that squint quotation marks are different than the ones in code, but I wouldn’t know how to use one instead of the other, My keyboard only has one kind which I use for everything. I don’t know why they don’t come out right in the forums sometimes. My apologies.

    Michael

    (@alchymyth)

    My keyboard only has one kind which I use for everything.

    and that is perfect so – these are the right ones to use in coding.

    the other ones are usually made by text processors to make the text ‘look better’ – unfortunately also somtimes in articles that share code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Single instance of each tag in a category’ is closed to new replies.