SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Code help

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code help

    Right now, the current code causes this affect *notice the <li> wrapping all the <a>. I would like to be able to wrap each <a> in its own <li>
    Code:
    <ul>
    <li>
    <a href='http://test>Test/a>
    <a href='http://test>Test/a>
    <a href='http://test>Test/a>
    </li>
    </ul>
    here is the php code

    PHP Code:
    $a[] = "<a href='$tag_link' class='tag-link-$tag_id'>$tag</a>";
        }

        switch ( 
    $format ) :
        case 
    'array' :
            
    $return =& $a;
            break;
        case 
    'list' :
            
    $return "<ul class='wp-tag-cloud'>\n\t<li>";
            
    $return .= join("<li>\n\t</li>"$a);
            
    $return .= "</li>\n</ul>\n";
            break;
        default :
            
    $return join("\n"$a);
            break;
        endswitch; 
    Thanks for any help.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any suggestions on how I can get the <li> to wrap around each <a> link instead of all of them?

  3. #3
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would actually need to see a little more of the code (I'm not sure what $format does, or if it's already in a loop), but you basically need to use a foreach...loop on $a, and wrap it that way. Something like:
    Code:
    case 'list':
    	$return .= '<ul class="wp-tag-cloud">';
    	
    	//Add each list element
    	foreach($a as $link){
    		$return .= '<li>'.$link.'</li>';
    	}
    	
    	$return .= '</ul>';
    break;

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Carlos, here is the other part of the code. Im not sure what the $format does. Can I have two foreach since there is already one?

    PHP Code:
    $a = array();

        
    $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' '';

        foreach ( 
    $counts as $tag => $count ) {
            
    $tag_id $tag_ids[$tag];
            
    $tag_link esc_url($tag_links[$tag]);
            
    $tag str_replace(' ''&nbsp;'esc_html$tag ));
            
    $a[] = "<a href='$tag_link' class='tag-link-$tag_id'>$tag</a>";
        }

        switch ( 
    $format ) :
        case 
    'array' :
            
    $return =& $a;
            break;
        case 
    'list' :
            
    $return "<ul class='wp-tag-cloud'>\n\t<li>";
            
    $return .= join("<li>\n\t</li>"$a);
            
    $return .= "</li>\n</ul>\n";
            break;
        default :
            
    $return join("\n"$a);
            break;
        endswitch; 

  5. #5
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yep, you can even have a foreach inside a foreach! I was just curious with the $format in there...

    That code I gave you above should work. Try replacing:
    Code:
        case 'list' :
            $return = "<ul class='wp-tag-cloud'>\n\t<li>";
            $return .= join("<li>\n\t</li>", $a);
            $return .= "</li>\n</ul>\n";
            break;
    with
    Code:
    case 'list':
    	$return .= '<ul class="wp-tag-cloud">';
    	
    	//Add each list element
    	foreach($a as $link){
    		$return .= '<li>'.$link.'</li>';
    	}
    	
    	$return .= '</ul>';
    break;

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oddly enough, it is still wrapping all of them .

  7. #7
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Strange, I'm sure it has something to do with $format. Can you try something for me, try replacing:

    All this:
    Code:
        switch ( $format ) :
        case 'array' :
            $return =& $a;
            break;
        case 'list' :
            $return = "<ul class='wp-tag-cloud'>\n\t<li>";
            $return .= join("<li>\n\t</li>", $a);
            $return .= "</li>\n</ul>\n";
            break;
        default :
            $return = join("\n", $a);
            break;
        endswitch;
    with:

    Code:
    	$return .= '<ul class="wp-tag-cloud">';
    	
    	//Add each list element
    	foreach($a as $link){
    		$return .= '<li>'.$link.'</li>';
    	}
    	
    	$return .= '</ul>';
    If it works, then it's definitely the $format variable not selecting "list". It's not a permanent fix, but it would help us know where to go next.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That got me an undefined variable for this line
    Code:
    $return .= '<ul class="wp-tag-cloud">';
    . I searched the entire document for any other $format and could not come up with one.

  9. #9
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oops, remove the period next to the equal sign in that line. Only in that line though.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that did the trick Maybe $format is a variable defined in the wp_tag_cloud. I found the post where someone is doing something similar, but $format is not defined.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •