Last active
March 27, 2016 06:35
-
-
Save tvdsluijs/5e543011b1259e2bc6d4 to your computer and use it in GitHub Desktop.
So you want a list of links on your site from posts from another wordpress blog? Use this little script!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function showPostsOtherSite($atts) { | |
//this information is found in the config.php of your other wordpress blog | |
$db_username = 'XXXXXXXXXX'; | |
$db_password = 'XXXXXXXXXX'; | |
$db_database = 'XXXXXXXXXX'; | |
$db_host = 'localhost'; //leave localhost or type IP from different server! | |
$db_prefix = 'XX_'; //something like wp_ (don't forget the _ sign) | |
//###########################################################################// | |
//LIKE MY SCRIPT? PLEASE BUY ME A CUP OF COFFEE, DONATE WITH PAYPAL! // | |
//PLEASE DONATE 1 EURO TO => http://snurl.eu/donate-one-euro // | |
//THANK YOU SO MUCH!!!! // | |
// // | |
//NO MORE EDITING FROM HERE // | |
//###########################################################################// | |
global $wpdb; | |
extract( shortcode_atts( array('site' => 'site', | |
'sitename' => 'sitename', | |
'limit' => 'limit'), $atts)); | |
$limit = (int) $limit; | |
if(!isset($limit) || $limit <= 0){ | |
$limit = '5'; // only used when there's no limit in the attributes | |
} | |
$newdb = new wpdb( $db_username , $db_password , $db_database , $db_host ); | |
$table_name = $db_prefix . "posts"; | |
$sql = "SELECT * FROM {$table_name} WHERE post_type='post' AND post_status='publish' | |
ORDER BY id DESC LIMIT {$limit}"; | |
$rows = (array) $newdb->get_results( $sql, ARRAY_A ); | |
$str = ""; | |
$str .= "<h4 class='widget-title'>Posts from {$sitename}</h4>"; | |
if(!isset($rows) || count($rows)<=0){ | |
$str .= "Sorry, no posts to show!"; | |
}else{ | |
$str .= "<ul>"; | |
foreach($rows as $row){ | |
$str .= "<li>"; | |
$str .= "<a href='{$row['guid']}' target='_blank'>"; | |
$str .= $row['post_title']; | |
$str .= "</a>"; | |
$str .= "</li>"; | |
} | |
//DID YOU DONATE? THANK YOU, YOU CAN REMOVE THE FOLLOWING LINES | |
$str .= "<li> | |
<a href='http://vandersluijs.nl' target='_blank'> | |
Display recent Wordpress Posts links on another WordPress Blog | |
</a> | |
</li>"; | |
//STOP HERE | |
$str .= "</ul>"; | |
} | |
return $str; | |
} | |
add_shortcode('show-postsothersite', 'showPostsOtherSite'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment