Support » Plugins » custom tree menu into sidebar

  • hi all,

    I am just trying to build my own customized wordpress tree menu, starting from the post where the user is and recursively querying the db via $wpdb to obtain a full category tree, until it reaches the top-level categories.

    The question is this: it appears that every turn that i do in the cycle, the variable $treeContent ( or in the previous version, the array $theTree, appears to empty himself, despite I declare it out of the cycling function. Can somone explain me how I can solve this problem?

    I had posted the code here It is thrown directly on the top of the sidebar.php file into my theme folder. Later I will build the complete plugin. Please have a look.
    Many thanks,

    Luca

    ——- START CODE EXCERPT ——–

    <?php
    		$theTree = array();
    		$treeContent = '';
    
    		function printContentNode($inputId) {
    			global $wpdb;
    
    			//$layerArray = array(); //creo array per il layer -- da appendere a $theTree;
    			$rowList = '';
    			//echo "elenco quelli sullo stesso livello<br />";
    			$rowList .=  "<!--inizio giro: " . $inputId . "-->";
    
    			//query di selezione stesso livello -- stesso parent ($parentId);
    			$rows = $wpdb->get_results("
    			SELECT name AS catName,
    			term_taxonomy_id AS catId,
    			parent AS catParent
    			FROM wp_terms LEFT JOIN wp_term_taxonomy
    			ON wp_terms.term_id = wp_term_taxonomy.term_id
    			WHERE wp_term_taxonomy.parent = " . $inputId . "
    			;");
    			$subLayerArray = array();
    			foreach ($rows as $row) {
    				// doppio giro per estrarre gli item dagli oggetti
    				//che il metodo di wpdb crea di default;
    				foreach ($row as $rowKey => $rowItem) {
    					//echo " // rowItem (" . $rowKey . ") " . $rowItem;
    					$subLayerArray[$rowKey] = $rowItem;
    				}
    				/*
    				echo "<br /><br />subLayer: ";
    				print_r($subLayerArray);
    				echo "<br /><br />";
    				*/
    				$layerArray[] = $subLayerArray; // same as array_push();
    				$rowList .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?cat=' . $subLayerArray['catId'] . '">' . $subLayerArray['catName'] . '</a></li>';
    			}
    
    			//echo "<br />";
    			$rowList = '<ul>' . $rowList . '</ul>';
    			return $rowList;
    		}
    
    		function printParentNode($inputId) {
    			global $wpdb;
    
    			//echo "estraggo il parente<br />";
    			$theParent = $wpdb->get_results("
    			SELECT name AS catName,
    			term_taxonomy_id AS catId,
    			parent AS catParent
    			FROM wp_terms LEFT JOIN wp_term_taxonomy
    			ON wp_terms.term_id = wp_term_taxonomy.term_id
    			WHERE wp_terms.term_id = " . $inputId . "
    			;");
    			/*
    			print_r($theParent);
    			echo "<br /><br />";
    			*/
    			//$newParentId = $theParent[0]->catParent;
    			//$layerArray["mainParent"] = $theParent[0]->catName;
    			/*
    			echo "<br /><br />layer(" . $parentId . "): ";
    			print_r($layerArray);
    			echo "<br /><br />";
    
    			array_unshift($theTree, $layerArray);
    			*/
    			$subArray = array();
    			foreach ($theParent[0] as $theKey => $theItem) {
    				$subArray[$theKey] = $theItem;
    			}
    			return $subArray;
    		}
    
    		function printLayer($parentId) {
    
    			global $wpdb; // fix al problema dell'oggetto non trovato
    			//vedi link -> http://wordpress.org/support/topic/240236
    
    			$levelOutput = printContentNode($parentId);
    
    			echo "pre: " . $treeContent . "<br />";
    			$treeContent = $levelOutput . $treeContent;
    			echo "all: " . $treeContent;
    			//echo '<ul>' . $rowList . '</ul>';
    			//echo "<!--fine giro: " . $parentId . "-->";
    			if ($parentId != 0) { // ricorsiva;
    
    				$levelParent = printParentNode($parentId);
    				print_r($levelParent);
    				echo "theP: " . $levelParent['catParent'].'<br />';
    				printLayer($levelParent['catParent']);
    			} else { // arrivato a livello zero;
    
    				/*
    				echo "<br /><br />layer(" . $parentId . "): ";
    				print_r($layerArray);
    				echo "<br /><br />";
    				array_unshift($theTree, $layerArray);
    				*/
    			}
    		}
    		if (is_category() || is_single()) {
    			echo "cat caso 1<br />";
    			$myCats = get_the_category();
    		} else {
    			echo "cat caso 2<br />";
    			$myCats = get_the_category(0);
    		}
    		if (count($myCats) == 1) {
    			$curCat = $myCats[0];
    			echo "categoria parente singola (" . $curCat->name . " :: " . $curCat->parent . ")<br />";
    			print_r($curCat);
    			echo "<br />";
    			//echo "<br /> / " . $curCat->parent . " / <br />";
    
    			printLayer($curCat->parent);//primo giro di lancio, poi ricorsiva;
    			//$result .= '<ul class ="custom_menu">';
    			//$result .= $treeContent;
    			//$result .= '</ul>';
    		} else {
    			echo "errore! questo post ha piu parenti";
    		}
    		?>

    ——– END CODE EXCERPT ——–

  • The topic ‘custom tree menu into sidebar’ is closed to new replies.