WordPress.org

Ready to get started?Download WordPress

Forums

Alphabetical category list without "The" or "An"? (10 posts)

  1. 8rittany
    Member
    Posted 6 years ago #

    Hi there! I don't know if this is possible:
    My posts are alphabetically listed when a category is clicked. Is there a way to exclude "The" "A" or "An" from the alphabetizing?
    Sure, I could title my post "Title, The" to solve that problem. But since they are titles of movies and books it would be easier for readers to find them without 50% being listed under "T" for "The".

  2. kulpreetsingh
    Member
    Posted 6 years ago #

    You can use trim in php (http://ca3.php.net/trim), and then set the output as an array variable, then sort the array in php (http://www.webcheatsheet.com/PHP/sorting_array.php).

  3. Mike Walker
    Member
    Posted 6 years ago #

    8rittany -- it would take a bit of work (depending on how may posts you have) but the AZIndex plugin allows you to sort posts into an index based on post titles, excerpts, or custom fields. So you could add modified titles of a book or movie (e.g. "Shining, The") to a custom field or excerpt and then sort on those, leaving the original titles of your posts alone.

    The plugin is: http://wordpress.org/extend/plugins/azindex/

  4. Ogre
    Member
    Posted 6 years ago #

    I don't believe trim would be a good choice for cleaning the title. This is best handled by a regular expression. For example:

    $wordlist = array("the", "a", "an");
    $string = "an animal in the house\n"; // example string
    $pattern = "/^(" . join("|", $wordlist) . ") /i"; // build the pattern
    echo preg_replace($pattern, "", $string);

    The pattern used in this example is "/^(the|a|an) /i", which translates to "match beginning of string only, "the" or "a" or "an" followed by a space.

    Delete the "an" and run it again. The "an" from the word "animal" is left intact as is the word "the" in the middle of the string.

    I used an array to hold the word-list as it can easily be converted to a function that obtained the words from a user setting if this were some kind of plugin.

  5. theadsmith
    Member
    Posted 6 years ago #

    Hi Ogre, this is something I want to accomplish on my blog as well-- sorting the category archives by name without "the," "an," or "a" factored in. Could you please go into a little more detail about how exactly I would use that code you offered? Also, would that interfere with the prev/next navigation of the archives?
    Aaron

  6. Ogre
    Member
    Posted 6 years ago #

    The code is not actually useful as-is. You would either need to hack the wp code or make a plugin that does the job. Of-hand, I don't know where to make the necessary changes. Perhaps when I have some free time I'll poke around a bit.

  7. KDesigns
    Member
    Posted 6 years ago #

    You could probably accomplish this using wp_list_categories() and setting the echo parameter to 0 so that it would keep it in a variable. Of course I'm not sure if that just throws the list in a variable or if it is an array (Haven't tested it out) so there would probably need to be some string trickery in there needed to use natural WP functions.

    wp_list_categories()
    How to Pass Tag Parameters - May not be a ton of help but thought it may be useful.

    These are just off the top of my head. Hope it helps get the ball rolling for you.

  8. dy_dream
    Member
    Posted 6 years ago #

    wp_list_categories() returns a string if I'm not mistaken.

    On the other hand I'm certain that if you want categories returned as an array, you'd use:

    get_categories();

  9. KDesigns
    Member
    Posted 6 years ago #

    You're right, wp_list_categories() does return a string but you can throw it in a variable instead of having it echoed out.

    get_categories(), like dy_dream says, would return it in an array. Filtering from there would be a little tricky as it would require ignoring certain words in ordering but still including them in the echoed version.

  10. Ogre
    Member
    Posted 6 years ago #

    This could be handled using 'usort' (sort with user-defined compare function). Just have the compare function strip out the words.

    Completely untested code!

    function mycompare($a, $b) {
    
      $pattern = "/^(the|a|an) /i";
      $a = strtolower(preg_replace($pattern, "", $a));
      $b = strtolower(preg_replace($pattern, "", $b));
      return strcmp($a, $b);
    }
    
    $categories = get_categories(); // I'm not sure of the parameter requirements
    usort($categories,"mycompare");

    The difficult part is figuring out where to stick the code.

Topic Closed

This topic has been closed to new replies.

About this Topic