Cara Membuat Auto Generate Content di Single Post,setelah kemarin ngepost tentang cara mudah membuat halaman auto generate content di halaman search,sekarang lanjut membuat auto generate content di single php.
langsung saja tak perlu basa basi
step 1 .
buka single.php di theme editor kemudian pasang kode berikut ini di bagian paling atas / di atas sebelum kode <?php get_header();?>
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 | <?php
if($_GET['s']!=''){
$ganti = array('+',' '); //tanda plus dan spasi
$urlredirect = get_settings('home') . '/search/' . str_replace($ganti, '-' ,$_GET['s']). '.html'; //tanda plus dan spasi jadi minus
header("HTTP/1.1 301 Moved Permanently");
header( "Location: $urlredirect" );
}?>
<?php define('BING_API_KEY', '');
function pete_curl_get($url, $params){$post_params = array();
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$fullurl = $url."?".$post_string;
$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_URL, $fullurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608'); //kamu bisa pake user agent yang lain, lihat listnya di sini www.user-agents.org
$result = curl_exec($ch);curl_close($ch);
return $result;
}function perform_bing_web_search($termstring){$searchurl = 'http://api.bing.net/json.aspx?';
$searchurl .= 'AppId='.'ABCDEFG'; //ganti ABCDEFG dengan kode api BING
$searchurl .= '&Query='.urlencode($termstring);
$searchurl .= '&Sources=Web';
$searchurl .= '&Web.Count=10'; //jumlah list situs yang dihasilkan
$searchurl .= '&Web.Offset=0';
$searchurl .= '&Web.Options=DisableHostCollapsing+DisableQueryAlterations';
$searchurl .= '&JsonType=raw';
$response = pete_curl_get($searchurl, array());
$responseobject = json_decode($response, true);if ($responseobject['SearchResponse']['Web']['Total']==0)return array();
$allresponseresults = $responseobject['SearchResponse']['Web']['Results'];
$result = array();
foreach ($allresponseresults as $responseresult){$result[] = array('url' => $responseresult['Url'],'title' => $responseresult['Title'],'abstract' => $responseresult['Description'],);
}return $result;
}if (isset($_REQUEST['s'])) {
$termstring = urldecode($_REQUEST['s']);
} else {
$termstring = '';}?>
<?php
function ambiljudul($title){
$title = get_the_title();
$title = trim($title);
return ($title);
}?> |
step 2.
kemudian kopi dan paste kode berikut ini di mana mau di tampilkan,sebagai contoh kalau mau di tampilkan setelah post maka pasang kode setelah kode <?php the_content (‘read more »’;); ?>
berikut ini kodenya
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 | <?php function CleanFileNameBan($result){
$bannedkey = array("porn","adult","sex"); //masukkan kata kunci satu persatu untuk menghindari kata-kata yang tidak diinginkan.
$result = str_replace($bannedkey, '',$result);
$result = trim($result);
return $result;
}
function hilangkan_spesial_karakter($result) { //fungsi hilangkan semua spesial karakter pada Title
$result = strip_tags($result);
$result = preg_replace('/&.+?;/', '', $result);
$result = preg_replace('/s+/', ' ', $result);
$result = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', ' ', $result);
$result = preg_replace('|-+|', ' ', $result);
$result = preg_replace('/&#?[a-z0-9]+;/i','',$result);
$result = preg_replace('/[^%A-Za-z0-9 _-]/', ' ', $result);
$result = trim($result, ' ');
return $result;
}
function ubah_tanda($result) { //fungsi ubah spasi jadi minus pada permalink title
$result = strtolower($result);
$result = preg_replace('/&.+?;/', '', $result);
$result = preg_replace('/s+/', '-', $result);
$result = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '-', $result);
$result = preg_replace('|-+|', '-', $result);
$result = preg_replace('/&#?[a-z0-9]+;/i','',$result);
$result = preg_replace('/[^%A-Za-z0-9 _-]/', '-', $result);
$result = trim($result, '-');
return $result;
}
?>
<?php $termstring = ambiljudul($title) ?>
<h3>related on another site</h3><?php if (ambiljudul($title)!='') {
$bingresults = perform_bing_web_search($termstring);
//Kamu bisa ganti kode <h1> serta test yang ada sesuai dengan kode html dan text keinginan kamu begitu juga dengan yang lainnya
foreach ($bingresults as $result) {
print '<div>';
print '<div>';
echo ('<hr/>');
print '<a href="'.$result['url'].'" rel="nofollow"><img align="left" width="50px" height="50px"style="margin:5px 5px 5px 0px; border:1px solid #000;" src="http://open.thumbshots.org/image.aspx?url='.$result['url'].'" title="'.CleanFileNameBan(hilangkan_spesial_karakter($result['title'])).'"/></a>';
print '<h3 ><a href="'. get_settings('home').'/search/'.ubah_tanda(CleanFileNameBan(hilangkan_spesial_karakter($result['title']))).'.html">'.CleanFileNameBan(hilangkan_spesial_karakter($result['title'])).'</a></h3>';
print ''.CleanFileNameBan(strip_tags($result['abstract'])).'';
print '<div>Related Tags:<a href="'. get_settings('home').'/search/'.ubah_tanda(CleanFileNameBan(hilangkan_spesial_karakter($result['title']))).'.html">'.CleanFileNameBan(hilangkan_spesial_karakter($result['title'])).'</a> ';
print '</div>';
print '</div>';
print '</div>';
print '<div style="clear:both;"></div>';
}
}
?> |
klik save kemudian test hasilnya.


