WordPress.org

Ready to get started?Download WordPress

Forums

Sort tags alphabetically (4 posts)

  1. liberto_io
    Member
    Posted 1 year ago #

    Hello everyone,

    Any idea to sort tags alphabetically like this page?

    I wrote code below but I think it's a beat heavy:

    <?php
    $tags = wp_tag_cloud( $args = array('smallest'=>22, 'largest'=>22, 'format'=>'array') );
    		foreach ($tags as $num => $tag) {
    			$st = strip_tags($tag);
    			$substr = substr($st, 0, 1);
    			switch ($substr) {
    				case 'a' : $arr[$tag] = $substr;
    				case 'b' : $arr[$tag] = $substr;
    				case 'c' : $arr[$tag] = $substr;
    				case 'd' : $arr[$tag] = $substr;
    				case 'e' : $arr[$tag] = $substr;
    				case 'f' : $arr[$tag] = $substr;
    				case 'g' : $arr[$tag] = $substr;
    				case 'h' : $arr[$tag] = $substr;
    				case 'i' : $arr[$tag] = $substr;
    				case 'j' : $arr[$tag] = $substr;
    				case 'k' : $arr[$tag] = $substr;
    				case 'l' : $arr[$tag] = $substr;
    				case 'm' : $arr[$tag] = $substr;
    				case 'n' : $arr[$tag] = $substr;
    				case 'o' : $arr[$tag] = $substr;
    				case 'p' : $arr[$tag] = $substr;
    				case 'q' : $arr[$tag] = $substr;
    				case 'r' : $arr[$tag] = $substr;
    				case 's' : $arr[$tag] = $substr;
    				case 't' : $arr[$tag] = $substr;
    				case 'u' : $arr[$tag] = $substr;
    				case 'v' : $arr[$tag] = $substr;
    				case 'w' : $arr[$tag] = $substr;
    				case 'x' : $arr[$tag] = $substr;
    				case 'y' : $arr[$tag] = $substr;
    				case 'z' : $arr[$tag] = $substr;
    			}
    
    		}

    I tried to sort tags in different arrays. Is it right?

  2. Bas Schuiling
    Member
    Posted 1 year ago #

    Try to put your tags first in an array and try something like 'array_sort' PHP function.

    I think with some googling you can find many examples of how to sort more effectively.

  3. liberto_io
    Member
    Posted 1 year ago #

    Look, in my code $tags is an array that WordPress generated like this:

    $tags = array([0]=>Alex [1]=>Mat [2]=>Michael [3]=>Peter);

    I want to turn this array to something like this:

    $tags = array([a]=>Alex [m]=>Mat [m]=>Michael [p]=>Peter);

    [ Please do not bump, that's not permitted here. ]

  4. slydawggiex916
    Member
    Posted 9 months ago #

    @liberto_lo - did you ever get this resolved?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.