Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created August 8, 2011 14:31
Show Gist options
  • Save sapegin/1131851 to your computer and use it in GitHub Desktop.
Save sapegin/1131851 to your computer and use it in GitHub Desktop.
All categories page for Wordpress
<?php
// wp-content/plugins/birdwatcher/birdwatcher.php
/*
* Страница со списком категорий
*/
add_shortcode( 'all_tags', 'bw_all_tags' );
function bw_all_tags()
{
ob_start();
require ABSPATH . 'wp-content/plugins/birdwatcher/cats.php';
return '<div class="allTags">' . ob_get_clean() . '</div>';
}
?>
<?php
// wp-content/plugins/birdwatcher/cats.php
/*
All Tags List
*/
if( !function_exists( 'get_option' ) )
{
require_once( '../../../wp-config.php');
}
$cats = get_categories(array(
'type' => 'post',
'orderby' => 'name',
'exclude' => '8', // hieroblogs
));
$slugs = array();
$parents = array();
// Родительские категории
foreach ( $cats as $id => $params )
{
$params = get_object_vars( $params );
if ( 0 == $params[ 'parent' ] )
{
$parents[ $params[ 'term_id' ] ] = array (
'main' => "<li><a href=\"/tag/{$params['slug']}\" title=\"{$params['description']}\">{$params['name']}</a><span>{$params['count']}</span>",
'sub' => '',
);
}
$slugs[ $params[ 'term_id' ] ] = $params[ 'slug' ];
}
// Вложенные категории
foreach ( $cats as $id => $params )
{
$params = get_object_vars( $params );
if ( $params[ 'parent' ] != 0 )
{
if ( $parents[ $params[ 'parent' ] ] )
{
$parents[ $params[ 'parent' ] ][ 'sub' ] .= "<li><a href=\"/tag/{$slugs[$params['parent']]}/{$params['slug']}\" title=\"{$params['description']}\">{$params['name']}</a><span>{$params['count']}</span></li>";
}
}
$slugs[ $params[ 'term_id' ] ] = $params[ 'slug' ];
}
?><table><tr><?php
$flat = '';
foreach ( $parents as $parentId => $data )
{
if ( $data[ 'sub' ] )
{
echo "<td><ul>{$data['main']}<ul class=\"sub\">{$data['sub']}</ul></li></ul></td>";
}
else
{
$flat .= $data[ 'main' ] . '</li>';
}
}
echo "<td><ul>$flat</ul></td>";
?></tr></table>
<div class="pages">
<b>Дополнительно:</b>
<ul>
<li><a href="/about">Об авторе</a></li>
<li><a href="/books/photography">Книги о фотографии</a></li>
<li><a href="/books/photoshop">Книги о Фотошопе и обработке фотографий</a></li>
<li><a href="/quotes">Цитаты</a></li>
<li><a href="/wishlist">Вишлист</a></li>
</ul>
</div>
<!-- wp-content/themes/birdwatcher/header.php -->
<a class="fake" href="/tags" onclick="toggleTags(this); return false;">Все разделы</a>
<div id="allTags" class="allTags" style="display:none"></div>
<script>
function toggleTags( link )
{
var container = document.getElementById( "allTags" );
var style = container.style;
if ( "" == style.display )
{
link.parentNode.parentNode.className = "all";
style.display = "none";
}
else
{
link.parentNode.parentNode.className = "allClicked";
if ( 0 == container.childNodes.length )
{
container.innerHTML = '<div class="ajaxLoader"><div>Загрузка списка разделов&hellip;</div></div>';
getUrl( "/wp-content/plugins/birdwatcher/cats.php", createTags );
}
style.display = "";
}
return false;
}
function createTags( html ) {
setTimeout( function () {
document.getElementById( "allTags" ).innerHTML = html;
}, 1 );
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment