Thursday, March 18, 2010

How To Remove the White Space in wp_list_pages

The Problem

The wp_list_pages() generates either a series of
  • elements containing links to all of you blog’s pages, or a full unordered list with a heading at the beginning.
    The output of this list looks like this:

    <ul>
    <li><a href="#">Page name</a></li>
    <li><a href="#">Page name</a></li>
    </ul>


    While some browsers don’t have a problem with elements being generated on new lines, in some, this method causes a white space to show up in front of each list item. This space becomes visible and annoying when trying to create a horizontal menu with background colors and equal horizontal spacing.

    The Fix

    To get rid of the white space, your output would need to look like this:


    <ul><li><a href="#">Page name</a></li><li><a href="#">Page name</a></li></ul>


    This can be achieved by using a small PHP function that will take the code generated by wp_list_pages() and turn it into something similar to the example above. To make this function available for use in your templates you need to edit the “functions.php” file, found in you theme’s directory. If you don’t have such a file, create an empty one and name it “functions.php”.

    In functions.php add the following code at the beginning or at the end of the file. Just be careful not to paste it inside another function and cause the script to break.


    function qbkl_nospace($input) {
    $output = str_replace(array("\n", "\r", "\t"), "", $input);
    echo $output;
    }


  • Check validate HTML and CSS

    http://jigsaw.w3.org/css-validator/#validate_by_uri

    http://validator.w3.org/check?verbose=1&uri=http://thy4u.blogspot.com

    Array Max


    <html>
    <head>
    <title>Array Max</title>
    <script type="text/javascript">
    // From: http://codingforums.com/showthread.php?t=152260

    var v=[10,8,42,50] // So looking to find 50
    function srchMaxV() {
    var maxV = 0; // or value smaller that smallest in array to search, like = -1000;
    for (i=0; i if (v[i] > maxV) { maxV = v[i]; }
    }
    return maxV;
    }

    function NumSort(a,b) { return a-b; } // required for sorting numbers only
    function sortMaxV() {
    var sortedV = new Array();
    sortedV = v.sort(NumSort);
    alert('Max: '+sortedV[sortedV.length-1]+'\nMin: '+sortedV[0]);
    }
    </script>
    </head>
    <body>
    <button onclick="alert('Max: '+srchMaxV())">Search for Max
    <button onclick="sortMaxV()">Sort for Max/Min
    </body>
    </html>