Making a category based menu with posts in wordpress 2.9.2

Sunday, May 23rd, 2010

I was not entirely happy with the way wordpress handles menus for this (my own) website. I like the idea of making all the content posts so they’re bound to a date and such. Then I’d like to see them in the menu based on the category they’re in. There are category menus, and even nice folding ones, but I couldn’t find one that displays all the posts per category in a nice way.

So I decided to hack one in myself. It’s a very nasty hack… and I just put a bunch of code in the sidebar.php of my template. But that said… it works and I now have a menu that does what I want.

If you’d like to use this… just remember it’s a great big bad hack and very inefficient!

Here’s the html/php code I added to my template’s sidebar.php:

  1. // Get the id of the current post so we can give it a "current" class later.
  2. $postid = $post->ID;
  3.  
  4. // Arguments for getting all the categories
  5. $args = array(
  6. 'show_option_all'    => '',
  7. 'orderby'            => 'name',
  8. 'order'              => 'ASC',
  9. 'show_last_update'   => 0,
  10. 'style'              => 'list',
  11. 'show_count'         => 0,
  12. 'hide_empty'         => 1,
  13. 'use_desc_for_title' => 0,
  14. 'child_of'           => 0,
  15. 'feed'               => '',
  16. 'feed_type'          => '',
  17. 'feed_image'         => '',
  18. 'exclude'            => '',
  19. 'exclude_tree'       => '',
  20. 'include'            => '',
  21. 'hierarchical'       => true,
  22. 'title_li'           => '',
  23. 'number'             => NULL,
  24. 'echo'               => 0,
  25. 'depth'              => 0,
  26. 'current_category'   => 1,
  27. 'pad_counts'         => 0,
  28. 'taxonomy'           => 'category' );
  29.  
  30. // Get all categories (in a silly html menu)
  31. $catmenu = wp_list_categories( $args );
  32.  
  33. // Split the menu to get all categories sepparately
  34. $cats = split('
  35. <li class="cat-item cat-item-', $catmenu);
  36.  
  37. $catmenu = '';
  38.  
  39. // Loop through the categories.
  40. foreach($cats as $key => $cat){
  41.  
  42.         $current = '';
  43.  
  44.         // Get the code and html sepparately
  45.         list($id, $code) = split('"><a', $cat);
  46.  
  47.         // Make sure they're there.
  48.         if($id && $code){
  49.  
  50.                 // If this is the current category, then remove that text and remember for later
  51.                 if(strpos($id, ' current-cat') !== False){
  52.                         $current = ' current-cat';
  53.                         $id = str_replace(' current-cat', '', $id);
  54.                 }
  55.  
  56.                 // In case we got an id... and this category doesn't have sub categories.
  57.                 if(is_numeric($id) && strpos($code, "
  58. <ul class='children'>") === False){
  59.  
  60.                         // Arguments for getting the categorie's posts
  61.                         $args = array(
  62.                                 'post_type' => 'post',
  63.                                 'post_status' => 'published',
  64.                                 'numberposts' => -1,
  65.                                 'category' => $id
  66.                                 );
  67.  
  68.                         // Get the posts
  69.                          $myposts = get_posts($args);
  70.  
  71.                          // If we got any posts returned
  72.                          if(count($myposts)){
  73.  
  74.                                 // Start a nice html post list
  75.                                 $postlist = "\n".'
  76. <ul class="posts">';
  77.  
  78.                                 // Check east post to see if it's current and add to the html
  79.                                  foreach($myposts as $post) {
  80.                                         $current_post = '';
  81.                                         if($postid == $post->ID){
  82.                                                 $current_post = ' class="current-post"';
  83.                                                 $current = ' current-cat';
  84.                                         }
  85.                                         $postlist .= "\n".'
  86. <li'.$current_post.'><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>
  87.  
  88. ';
  89.                                  }
  90.  
  91.                                  $postlist .= "\n".'</ul>
  92.  
  93. ';
  94.  
  95.                                 // Add the post list to the code of the current category
  96.                                 $code = str_replace('</a>', '</a>'.$postlist, $code);
  97.  
  98.                         }
  99.  
  100.                 }
  101.  
  102.                 // Put everything that was split before back together
  103.                 $cat = '
  104. <li class="cat-item cat-item-'.$id.$current.'"><a'.$code;
  105.  
  106.         }
  107.  
  108.         // Add back into the complete category menu
  109.         $catmenu .= $cat;
  110. }
  111.  
  112. // Print out the category menu
  113. echo '
  114. <li id="menu">
  115. <ul>'.$catmenu.'</ul>
  116. </li>
  117.  
  118. ';

Display clean php code for copying

// Get the id of the current post so we can give it a "current" class later.
$postid = $post->ID;

// Arguments for getting all the categories
$args = array(
'show_option_all'    => '',
'orderby'            => 'name',
'order'              => 'ASC',
'show_last_update'   => 0,
'style'              => 'list',
'show_count'         => 0,
'hide_empty'         => 1,
'use_desc_for_title' => 0,
'child_of'           => 0,
'feed'               => '',
'feed_type'          => '',
'feed_image'         => '',
'exclude'            => '',
'exclude_tree'       => '',
'include'            => '',
'hierarchical'       => true,
'title_li'           => '',
'number'             => NULL,
'echo'               => 0,
'depth'              => 0,
'current_category'   => 1,
'pad_counts'         => 0,
'taxonomy'           => 'category' );

// Get all categories (in a silly html menu)
$catmenu = wp_list_categories( $args );

// Split the menu to get all categories sepparately
$cats = split('
  • ") === False){ // Arguments for getting the categorie's posts $args = array( 'post_type' => 'post', 'post_status' => 'published', 'numberposts' => -1, 'category' => $id ); // Get the posts $myposts = get_posts($args); // If we got any posts returned if(count($myposts)){ // Start a nice html post list $postlist = "\n".'
      '; // Check east post to see if it's current and add to the html foreach($myposts as $post) { $current_post = ''; if($postid == $post->ID){ $current_post = ' class="current-post"'; $current = ' current-cat'; } $postlist .= "\n".' '.$post->post_title.' '; } $postlist .= "\n".'
    '; // Add the post list to the code of the current category $code = str_replace('', ''.$postlist, $code); } } // Put everything that was split before back together $cat = '
    • '.$catmenu.'
  • ';

    And here is the javascript code I added to my template’s header.php:

    1. // Initialise javascript functions using jquery
    2. jQuery(document).ready(function(){
    3.  
    4.         initMenu();
    5.  
    6. });
    7.  
    8. // Start the menu functionality
    9. function initMenu(){
    10.         // Hide all the submenus by default
    11.         jQuery('#menu ul ul').hide();
    12.  
    13.         // Find the current post and make sure all the categories it's in are also "current", then show their kids.
    14.         jQuery('#menu .current-post').parents('li[id!=menu]').addClass('current-cat').children('ul').show();
    15.  
    16.         // Replace the click event of all category menu items except for "news"
    17.         // In stead make them fold down and up the sub menu
    18.         jQuery('#menu a').click(function(event){
    19.  
    20.                 thisItem = jQuery(this);
    21.  
    22.                 childList = thisItem.siblings('ul');
    23.  
    24.                 if(childList.length){
    25.  
    26.                         if(!(thisItem.html() == 'News' && childList.is(':hidden'))){
    27.  
    28.                                 event.preventDefault();
    29.  
    30.                                 if(childList.is(':hidden')){
    31.  
    32.                                         thisItem.addClass('clicked');
    33.  
    34.                                         childList.slideDown('fast');
    35.  
    36.                                         jQuery('#menu ul:visible').each(function(){
    37.  
    38.                                                 if(!(jQuery('.clicked', this).length || jQuery(this).siblings('.clicked').length)){
    39.                                                         jQuery(this).slideUp('fast');
    40.                                                 }
    41.  
    42.                                         });
    43.  
    44.                                         thisItem.removeClass('clicked');
    45.  
    46.                                 }else{
    47.                                         childList.slideUp('fast');
    48.                                 }
    49.                         }
    50.  
    51.                 }
    52.  
    53.         });
    54. }

    Display clean javascript code for copying

    // Initialise javascript functions using jquery
    jQuery(document).ready(function(){
    
    	initMenu();
    
    });
    
    // Start the menu functionality
    function initMenu(){
    	// Hide all the submenus by default
    	jQuery('#menu ul ul').hide();
    
    	// Find the current post and make sure all the categories it's in are also "current", then show their kids.
    	jQuery('#menu .current-post').parents('li[id!=menu]').addClass('current-cat').children('ul').show();
    
    	// Replace the click event of all category menu items except for "news"
    	// In stead make them fold down and up the sub menu
    	jQuery('#menu a').click(function(event){
    
    		thisItem = jQuery(this);
    
    		childList = thisItem.siblings('ul');
    
    		if(childList.length){
    
    			if(!(thisItem.html() == 'News' && childList.is(':hidden'))){
    
    				event.preventDefault();
    
    				if(childList.is(':hidden')){
    
    					thisItem.addClass('clicked');
    
    					childList.slideDown('fast');
    
    					jQuery('#menu ul:visible').each(function(){
    
    						if(!(jQuery('.clicked', this).length || jQuery(this).siblings('.clicked').length)){
    							jQuery(this).slideUp('fast');
    						}
    
    					});
    
    					thisItem.removeClass('clicked');
    
    				}else{
    					childList.slideUp('fast');
    				}
    			}
    
    		}
    
    	});
    }

    Html symbols

    Wednesday, March 17th, 2010
    Character Entity Decimal Hex Rendering in Your Browser
    Entity Decimal Hex
    Latin small f with hook = function = florin &fnof; &#402; &#x192; ƒ ƒ ƒ
    Greek capital letter alpha &Alpha; &#913; &#x391; Α Α Α
    Greek capital letter beta &Beta; &#914; &#x392; Β Β Β
    Greek capital letter gamma &Gamma; &#915; &#x393; Γ Γ Γ
    Greek capital letter delta &Delta; &#916; &#x394; Δ Δ Δ
    Greek capital letter epsilon &Epsilon; &#917; &#x395; Ε Ε Ε
    Greek capital letter zeta &Zeta; &#918; &#x396; Ζ Ζ Ζ
    Greek capital letter eta &Eta; &#919; &#x397; Η Η Η
    Greek capital letter theta &Theta; &#920; &#x398; Θ Θ Θ
    Greek capital letter iota &Iota; &#921; &#x399; Ι Ι Ι
    Greek capital letter kappa &Kappa; &#922; &#x39A; Κ Κ Κ
    Greek capital letter lambda &Lambda; &#923; &#x39B; Λ Λ Λ
    Greek capital letter mu &Mu; &#924; &#x39C; Μ Μ Μ
    Greek capital letter nu &Nu; &#925; &#x39D; Ν Ν Ν
    Greek capital letter xi &Xi; &#926; &#x39E; Ξ Ξ Ξ
    Greek capital letter omicron &Omicron; &#927; &#x39F; Ο Ο Ο
    Greek capital letter pi &Pi; &#928; &#x3A0; Π Π Π
    Greek capital letter rho &Rho; &#929; &#x3A1; Ρ Ρ Ρ
    Greek capital letter sigma &Sigma; &#931; &#x3A3; Σ Σ Σ
    Greek capital letter tau &Tau; &#932; &#x3A4; Τ Τ Τ
    Greek capital letter upsilon &Upsilon; &#933; &#x3A5; Υ Υ Υ
    Greek capital letter phi &Phi; &#934; &#x3A6; Φ Φ Φ
    Greek capital letter chi &Chi; &#935; &#x3A7; Χ Χ Χ
    Greek capital letter psi &Psi; &#936; &#x3A8; Ψ Ψ Ψ
    Greek capital letter omega &Omega; &#937; &#x3A9; Ω Ω Ω
    Greek small letter alpha &alpha; &#945; &#x3B1; α α α
    Greek small letter beta &beta; &#946; &#x3B2; β β β
    Greek small letter gamma &gamma; &#947; &#x3B3; γ γ γ
    Greek small letter delta &delta; &#948; &#x3B4; δ δ δ
    Greek small letter epsilon &epsilon; &#949; &#x3B5; ε ε ε
    Greek small letter zeta &zeta; &#950; &#x3B6; ζ ζ ζ
    Greek small letter eta &eta; &#951; &#x3B7; η η η
    Greek small letter theta &theta; &#952; &#x3B8; θ θ θ
    Greek small letter iota &iota; &#953; &#x3B9; ι ι ι
    Greek small letter kappa &kappa; &#954; &#x3BA; κ κ κ
    Greek small letter lambda &lambda; &#955; &#x3BB; λ λ λ
    Greek small letter mu &mu; &#956; &#x3BC; μ μ μ
    Greek small letter nu &nu; &#957; &#x3BD; ν ν ν
    Greek small letter xi &xi; &#958; &#x3BE; ξ ξ ξ
    Greek small letter omicron &omicron; &#959; &#x3BF; ο ο ο
    Greek small letter pi &pi; &#960; &#x3C0; π π π
    Greek small letter rho &rho; &#961; &#x3C1; ρ ρ ρ
    Greek small letter final sigma &sigmaf; &#962; &#x3C2; ς ς ς
    Greek small letter sigma &sigma; &#963; &#x3C3; σ σ σ
    Greek small letter tau &tau; &#964; &#x3C4; τ τ τ
    Greek small letter upsilon &upsilon; &#965; &#x3C5; υ υ υ
    Greek small letter phi &phi; &#966; &#x3C6; φ φ φ
    Greek small letter chi &chi; &#967; &#x3C7; χ χ χ
    Greek small letter psi &psi; &#968; &#x3C8; ψ ψ ψ
    Greek small letter omega &omega; &#969; &#x3C9; ω ω ω
    Greek small letter theta symbol &thetasym; &#977; &#x3D1; ϑ ϑ ϑ
    Greek upsilon with hook symbol &upsih; &#978; &#x3D2; ϒ ϒ ϒ
    Greek pi symbol &piv; &#982; &#x3D6; ϖ ϖ ϖ
    bullet = black small circle &bull; &#8226; &#x2022;
    horizontal ellipsis = three dot leader &hellip; &#8230; &#x2026;
    prime = minutes = feet &prime; &#8242; &#x2032;
    double prime = seconds = inches &Prime; &#8243; &#x2033;
    overline = spacing overscore &oline; &#8254; &#x203E;
    fraction slash &frasl; &#8260; &#x2044;
    script capital P = power set = Weierstrass p &weierp; &#8472; &#x2118;
    blackletter capital I = imaginary part &image; &#8465; &#x2111;
    blackletter capital R = real part symbol &real; &#8476; &#x211C;
    trade mark sign &trade; &#8482; &#x2122;
    alef symbol = first transfinite cardinal &alefsym; &#8501; &#x2135;
    leftwards arrow &larr; &#8592; &#x2190;
    upwards arrow &uarr; &#8593; &#x2191;
    rightwards arrow &rarr; &#8594; &#x2192;
    downwards arrow &darr; &#8595; &#x2193;
    left right arrow &harr; &#8596; &#x2194;
    downwards arrow with corner leftwards = carriage return &crarr; &#8629; &#x21B5;
    leftwards double arrow &lArr; &#8656; &#x21D0;
    upwards double arrow &uArr; &#8657; &#x21D1;
    rightwards double arrow &rArr; &#8658; &#x21D2;
    downwards double arrow &dArr; &#8659; &#x21D3;
    left right double arrow &hArr; &#8660; &#x21D4;
    for all &forall; &#8704; &#x2200;
    partial differential &part; &#8706; &#x2202;
    there exists &exist; &#8707; &#x2203;
    empty set = null set = diameter &empty; &#8709; &#x2205;
    nabla = backward difference &nabla; &#8711; &#x2207;
    element of &isin; &#8712; &#x2208;
    not an element of &notin; &#8713; &#x2209;
    contains as member &ni; &#8715; &#x220B;
    n-ary product = product sign &prod; &#8719; &#x220F;
    n-ary sumation &sum; &#8721; &#x2211;
    minus sign &minus; &#8722; &#x2212;
    asterisk operator &lowast; &#8727; &#x2217;
    square root = radical sign &radic; &#8730; &#x221A;
    proportional to &prop; &#8733; &#x221D;
    infinity &infin; &#8734; &#x221E;
    angle &ang; &#8736; &#x2220;
    logical and = wedge &and; &#8743; &#x2227;
    logical or = vee &or; &#8744; &#x2228;
    intersection = cap &cap; &#8745; &#x2229;
    union = cup &cup; &#8746; &#x222A;
    integral &int; &#8747; &#x222B;
    therefore &there4; &#8756; &#x2234;
    tilde operator = varies with = similar to &sim; &#8764; &#x223C;
    approximately equal to &cong; &#8773; &#x2245;
    almost equal to = asymptotic to &asymp; &#8776; &#x2248;
    not equal to &ne; &#8800; &#x2260;
    identical to &equiv; &#8801; &#x2261;
    less-than or equal to &le; &#8804; &#x2264;
    greater-than or equal to &ge; &#8805; &#x2265;
    subset of &sub; &#8834; &#x2282;
    superset of &sup; &#8835; &#x2283;
    not a subset of &nsub; &#8836; &#x2284;
    subset of or equal to &sube; &#8838; &#x2286;
    superset of or equal to &supe; &#8839; &#x2287;
    circled plus = direct sum &oplus; &#8853; &#x2295;
    circled times = vector product &otimes; &#8855; &#x2297;
    up tack = orthogonal to = perpendicular &perp; &#8869; &#x22A5;
    dot operator &sdot; &#8901; &#x22C5;
    left ceiling = APL upstile &lceil; &#8968; &#x2308;
    right ceiling &rceil; &#8969; &#x2309;
    left floor = APL downstile &lfloor; &#8970; &#x230A;
    right floor &rfloor; &#8971; &#x230B;
    left-pointing angle bracket = bra &lang; &#9001; &#x2329;
    right-pointing angle bracket = ket &rang; &#9002; &#x232A;
    lozenge &loz; &#9674; &#x25CA;
    black spade suit &spades; &#9824; &#x2660;
    black club suit = shamrock &clubs; &#9827; &#x2663;
    black heart suit = valentine &hearts; &#9829; &#x2665;
    black diamond suit &diams; &#9830; &#x2666;

    Htaccess

    Wednesday, March 17th, 2010

    This is a very basic .htaccess file as I like to use it. It simply redirects any request to the index.php if it’s not an existing file and not an existing directory.

    1. <IfModule mod_rewrite.c>
    2.  
    3. RewriteEngine On
    4.  
    5. RewriteCond %{REQUEST_FILENAME} !-f
    6. RewriteCond %{REQUEST_FILENAME} !-d
    7. RewriteRule ^(.*)/? /index.php?loc=$1
    8.  
    9. </IfModule>

    Display clean text code for copying

    <IfModule mod_rewrite.c>
    
    RewriteEngine On 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/? /index.php?loc=$1
    
    </IfModule>

    It works as follows:

    checks to see if mod-rewrite is enabled

    You can add Options +FollowSymlinks on the line before RewriteEngine On
    It needs to be enabled for mod rewrite to actually work

    RewriteEngine On
    Start the rewrite engine

    RewriteCond %{REQUEST_FILENAME} !-f
    Is a condition and returns true if the requested location is not an existing file.

    RewriteCond %{REQUEST_FILENAME} !-d
    Is a condition and returns true if the location is not an existing directory.

    RewriteRule ^(.*)/? /index.php?loc=$1
    Does the actual redirection, basicly it takes everything from behind http://www.mysite.domain/ and puts it behind loc=
    So inside your index.php you can get the url as $_GET['loc']
    You can then use PHP to analyse what was requested.

    </IfModule>
    The end of the rewriting.

    There are lots of more complicated things you can do, but this is what I need more often and it does only the thing I need… which is good.

    click here to close

    Help keep these files free,
    and support further development!