How To Display Recent Posts From All Sites
The following snippet will display recent posts from all sites.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
function recent_mu_posts( $howMany = 10 ) { global $wpdb; global $table_prefix; // get an array of the table names that our posts will be in // we do this by first getting all of our blog ids and then forming the name of the // table and putting it into an array $rows = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs WHERE public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0';" ); if ( $rows ) : $blogPostTableNames = array(); foreach ( $rows as $row ) : $blogPostTableNames[$row->blog_id] = $wpdb->get_blog_prefix( $row->blog_id ) . 'posts'; endforeach; # print_r($blogPostTableNames); # debugging code // now we need to do a query to get all the posts from all our blogs // with limits applied if ( count( $blogPostTableNames ) > 0 ) : $query = ''; $i = 0; foreach ( $blogPostTableNames as $blogId => $tableName ) : if ( $i > 0 ) : $query.= ' UNION '; endif; $query.= " (SELECT ID, post_date, $blogId as `blog_id` FROM $tableName WHERE post_status = 'publish' AND post_type = 'post')"; $i++; endforeach; $query.= " ORDER BY post_date DESC LIMIT 0,$howMany;"; # echo $query; # debugging code $rows = $wpdb->get_results( $query ); // now we need to get each of our posts into an array and return them if ( $rows ) : $posts = array(); foreach ( $rows as $row ) : $posts[] = get_blog_post( $row->blog_id, $row->ID ); endforeach; # echo "<pre>"; print_r($posts); echo "</pre>"; exit; # debugging code return $posts; else: return "Error: No Posts found"; endif; else: return "Error: Could not find blogs in the database"; endif; else: return "Error: Could not find blogs"; endif; } |
Snippet Source/Credit: Snipplr
How To Display Twitter Feed In WordPress
With an age of social media, it is very much important to display twitter feed in your WordPress website. Moreover, it is not only just for getting updates but also to encourage people more often with the tweets. To display twitter feed in WordPress, using the following snippet will let you achieve this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php include_once(ABSPATH . WPINC . '/feed.php'); $rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=smashingmag'); $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items(0, $maxitems); ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else // Loop through each feed item and display each item as a hyperlink. foreach ( $rss_items as $item ) : ?> <li> <a href='<?php echo $item->get_permalink(); ?>'> <?php echo $item->get_title(); ?> </a> </li> <?php endforeach; ?> </ul> |
Snippet Source/Credit: Smashing Magazine
How To Execute Shortcode From Custom Field
The snippet will let you add shortcode to a WordPress custom field while by default; the option is not available by WordPress.
1 |
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'your_custom_field_here', true)); ?> |
Snippet Source/Credit: Leeboyce.co.uk
How To Generate Your Own Portfolio With Filtering Based On Taxonomy
Using the following snippet will let you generate your own portfolio with the help of filtering based on taxonomy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
<?php // get portfolio $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'asc' ); $loop = new WP_Query( $args ); $port = array(); while ( $loop->have_posts() ) : $loop->the_post(); $idx = get_the_ID(); $year_completed = get_post_meta($idx, 'year_completed', true); $website_addr = get_post_meta($idx, 'website_address', true); $thumb = get_the_post_thumbnail($idx, 'thumbnail'); //250x200 - featured image $title = get_the_title(); $excerpt = get_the_excerpt(); $content = get_the_content(); //get 'Solutions' terms $terms = get_the_terms($idx, 'Solutions'); $terms_string = ''; //build up comma delimited string foreach($terms as $t){ $terms_string .= $t->slug . ' '; } $port[] = array( 'id' => $idx, 'year_completed' => $year_completed, 'website' => $website_addr, 'thumb' => $thumb, 'title' => $title, 'content' => $content, 'excerpt' => $excerpt, 'terms' => $terms, 'terms_string' =>$terms_string, //classifications (comma delimited slugs) 'permalink' => get_permalink(), ); endwhile; $terms = get_terms('Solutions'); $filters = '<section id="options" class="clearfix"> <ul id="filters" class="option-set floated clearfix"> <li><a href="#filter" data-filter="*" class="selected">show all</a></li>'; foreach($terms as $t){ $filters .= '<li><a href="#filter" data-filter=".' . $t->slug . '">' . $t->name . '</a></li>';// $t->count } $filters .= '</ul></section>'; ?> <!-- add this inside entry-content --> <?php echo $filters; ?> <div id="portfolio"> <!-- isotope --> <ul class="thumbnails isotope"> <?php foreach($port as $p){ ?> <li class="span3 element <?php echo $p['terms_string']; ?>"> <div class="thumbnail"> <?php echo $p['thumb']; ?> <div class="caption"> <h5><a href="<?php echo $p['permalink']; ?>"><?php echo $p['title']; ?></a></h5> </div><!-- end caption --> </div><!-- end thumbnail --> </li> <? } //end foreach ?> </ul> </div><!-- end #portfolio --> |
Snippet Source/Credit: ToddWilson
How To Get An RSS Feed And Display Items With A Loop
Use of the following snippet will let you get an RSS feed and display items with a loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php include_once(ABSPATH . WPINC . '/rss.php'); $feed = 'http://dangayle.com/feed/'; $rss = fetch_feed($feed); if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items(0, $maxitems); if ($rss_items): echo "<ul>\n"; foreach ( $rss_items as $item ) : //instead of a bunch of string concatenation or echoes, I prefer the terseness of printf //(http://php.net/manual/en/function.printf.php) printf('<li><a href="%s">%s</a><p>%s</p></li>',$item->get_permalink(),$item->get_title(),$item->get_description() ); endforeach; echo "</ul>\n"; endif; endif; ?> |
Snippet Source/Credit: DanGayle.com
How To Get Attachments For Post Or Page
Using the following snippet below will let you get attachments for posts or pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID ); $images = get_posts( $args ); foreach($images as $image): echo wp_get_attachment_image($image->ID, 'medium'); endforeach; |
Snippet Source/Credit: Stack Overflow