• Larsmir

    (@larsmir)


    Hi there!

    I’m running a multisite install for a political party, with subdirectories for each member of the party.

    We have a custom plugin where the admin of each blog can tick a checkbox for “Senator” or “Representative”. This plugin also includes these functions.

    function getBlogInfo($key){
    	global $wpdb, $post, $table_prefix, $blog_id;
    
    	$q = "SELECT <code>value</code> FROM info_extra WHERE idBlog = '{$blog_id}' AND <code>key</code>='{$key}' ";
    	return $wpdb->get_var($q);
    
    }
    
    function getBlogInfoByBlogId($key, $blogid){
    	global $wpdb, $post, $table_prefix;
    
    	$q = "SELECT <code>value</code> FROM info_extra WHERE idBlog = '{$blogid}' AND <code>key</code>='{$key}' ";
    	return $wpdb->get_var($q);
    
    }

    And this is the function that saves the data (Senator/Representative) from the backend, to a table in the database called info_extra:

    function saveData($key, $value){
    	global $wpdb, $post, $table_prefix, $blog_id;
    
    	$q = "SELECT <code>key</code> FROM info_extra WHERE idBlog = '{$blog_id}' AND <code>key</code>='{$key}' ";
    	$exists=$wpdb->get_var($q);
    
    	if($exists):
    		$q = "UPDATE info_extra SET <code>value</code>='{$value}' WHERE <code>key</code>='{$key}' AND idBlog = '{$blog_id}'";
    	else:
    		$q = "INSERT INTO info_extra SET <code>idBlog</code>='{$blog_id}', <code>key</code>='{$key}', <code>value</code>='{$value}'";
    	endif;
    	$post=$wpdb->query($wpdb->prepare($q));

    So, on to the problem: I was asked to add two links to our homepage: one reading ‘Senators’ and one reading ‘Representatives’. When someone clicks on, for instance, Senators, they should be taken to a page (www.example.com/senators) that lists the name of each blog that has ticked the Senator checkbox, along with a link to that blog.

    So far I’ve got this.

    <?php 
    
    $bloglist = wp_get_sites();
    foreach ($bloglist as $blog) {
    	$blogid = $blog[blog_id];
    	getBlogInfoByBlogId('iSenator', $blogid);
    }
    ?>

    But I’m not very good at PHP so I’m stumped. I understand my code should return the id of each blog that has has isSenator=1, but I don’t know how to turn that into a human-readable list of blogs with their links.

    Care to shed some light on this?

Viewing 1 replies (of 1 total)
  • geraintp

    (@geraintp)

    Something like this would generate two, arrays/ lists

    $sen = array();
    $rep = array();
    
    $bloglist = wp_get_sites();
    foreach ($bloglist as $blog) {
    
    	//senitors
    	if ( getBlogInfoByBlogId('isSenator', $blog['blog_id'] ) ) {
    
    		$sen[] = array( 'id' => $blog['blog_id'],
    					'domain' => $blog['domain'],
    					  'path' => $blog['path'],
    					   'url' => 'https://'.$blog['domain'].$blog['path'] );
    
    	// reps
    	}else{
    		$rep[] = array( 'id' => $blog['blog_id'],
    					'domain' => $blog['domain'],
    					  'path' => $blog['path'],
    					   'url' => 'https://'.$blog['domain'].$blog['path'] );
    	}
    
    }

    url making some assumptions based on what wp_get_sites() should return and that you’ve got a sub directory multi site install. obviously none of the code’s been tested.

    you can now iterate through each to come up with a list, but… the list will be in blog id order not alphabetical + you still haven’t got any display info in the arrays like tiles or names extra…

Viewing 1 replies (of 1 total)
  • The topic ‘Linking to each blog on my multisite install’ is closed to new replies.