The MU forums have moved to WordPress.org

List of blogs/sites and all categories (5 posts)

  1. zanfardinom
    Member
    Posted 14 years ago #

    Hi all,

    How would I list all blogs/sites and categories without having to connect to the database and use select statements. Blogs/sites would be one drop down box and categories would be another.

    My MU site structor is:
    Admin - http://name.domain.tld
    New user - http://username.name.domain.tld

    I have the code working for the most part, but not returning what I need to display.

    The first part list all blogs and list them in a drop down with the blog name and site URL. That's great, but I would like to list the display name and site URL. (I can do this by connecting to the database, but thought there might be a better way without opening a connection.). Code below was from an example - can't find the source.

    <?php
    $current_site = get_current_site()->id;
    $blog_list = get_blog_list( 0, 'all' );
    $blogs=array();
    foreach ($blog_list AS $blog) {
    	$blogid = $blog['blog_id'];
    	//if ($blogid != $current_site) {
    		$name = get_blog_details($blogid)->blogname;
    		$url = get_blog_details($blogid)->siteurl;
    		$blogs[] = "$name, $url";
    	//}
    }
    sort($blogs);
    echo '<h2>Choose your site by Name</h2>'; ?>
    <select name="instructor_list" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    	<option value=""><?php echo attribute_escape(__('--- Select Instructor ---')); ?></option>
            <?php foreach ($blogs as $blog) {
              	list($name, $url) = split(",", $blog);
                    echo '<option value="' .$url. '"><a href="'.$url.'" title="' .$name. '">' .$name. '</a></option>';
            } ?>
    </select>

    The second part list all categories (or it should). The only thing it list are the categories for the admin blog and not all blogs. Again, I can do this with SQL, but thought there was a way (just like the code below) to list all categories for each user with the exception of a few. The code below was pulled from http://codex.wordpress.org/Function_Reference/get_categories

    <?php echo '<h2>Choose your site by Category</h2>'; ?>
            <select name="course_list" onchange='document.location.href=this.options[this.selectedIndex].value;'>
            	<option value=""><?php echo attribute_escape(__('--- Select Course ---')); ?></option>
                <?php $categories = get_categories();
                foreach ($categories as $cat) {
                	$option = '<option value="/category/'.$cat->category_nicename.'">';
                    $option .= $cat->cat_name;
                    $option .= ' ('.$cat->category_count.')';
                    $option .= '</option>';
                    echo $option;
                } ?>
            </select>
  2. STDestiny
    Member
    Posted 14 years ago #

    sql is not my forte, but i do know that any list of blogs/urls requires a database query. The database is the only information that is stored (unless you write something to periodically input that information to a text file.)

    As for sitewide categories, do a search for "sitewide tags" on the forums. You should be able to do something with that plugin.

  3. zanfardinom
    Member
    Posted 14 years ago #

    Thanks, STDestiny! I appreciate the quick response. I would love to use the second code snipet...if I could. But, it only shows categories for the current blog (admin). I would like for it to show every category for all blogs with some exceptions. Like...don't show Blogroll, uncategorized and empty categories. Now, I can do this by writing SQL and pull from every table, but the code above is better (i can use the built in function get_categories. Is there way I can pass in a list of blog IDs? Also, is there a function for the first code where I can pull every blog with domain for url and the display name?

    Thanks in advance.

    mz

  4. fre391
    Member
    Posted 14 years ago #

    Hi,

    I ended up with something like:

    $blogs = get_blog_list();
    foreach($blogs as $blog) {
    	$blog_info = get_blog_details($blog['blog_id']);
    	switch_to_blog($blog['blog_id']);
    	$categories = get_categories( );
    	foreach($categories as $category) {
    		echo ('['.$blog_info->blog_id.' - '.$category->cat_ID.'] ');
    		echo ($blog_info->blogname.' - '.$category->name.'<br />');
    	}
    }

    Hope that helps?
    There might be more clever ways, but -however- for me it does the trick :-)

    Greetings

    Markus

  5. wojtekor
    Member
    Posted 14 years ago #

    Could you please tell me how to use this code? I am looking for a way to have a page with all blogs listed...

About this Topic

  • Started 14 years ago by zanfardinom
  • Latest reply from wojtekor