នៅលើទំព័រមុខ យើងអាចធ្វើការរចនានូវអ្វីៗបានគ្រប់យ៉ាងទាំងអស់ មានដូចជាការបង្កើតផ្នែកនៃការផ្សាយចុងក្រោយនៅផ្នែកខាងលើគេជាដើម។ លើសពីនេះទៀត ក្រៅពីភាសា PHP យើងក៏អាចប្រើប្រាស់ភាសា JavaScript ក្នុងគោលបំណងបង្កើតផ្នែកនៃការផ្សាយចុងក្រោយគេនេះបានដែរ។
ដើម្បីបង្កើតផ្នែកនៃការផ្សាយចុងក្រោយមួយ ដោយប្រើប្រាស់ភាសា JavaScript យើងចាំត្រូវបង្កើតឯកសារ js មួយ មានឈ្មោះជាឧទាហរណ៍ដូចជា script.js ជាដើម ដែលនៅក្នុងនោះ យើងត្រូវសរសេរកូដមួយចំនួនដូចខាងក្រោមនេះ៖
function __KBclass__(){
this.today = this.srvTime();
this.playIcon = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR2UeE7iPZFtkNE7wC38Febfs2JMyy-0C-vrOGKlMmjNw-oo-1io9yGChDBg1UzBy-2NXsajoBrSmiA5m1cRKqZcDuWD7ToRJd85Woes7h5x4xMr4wvJ_nptXtv1J-O0hD6DyYn7w1BQA/s1600/play.png";
this.noPost = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhblCOrPueEj9giHUNqD6KrzqdCTRuyMIxJbKaEwiEH2-o_fBcQ2RIt0jM6MBEJCzlAeh88WLsVfSN2ODT-nPkkca5oSz4pgR9mEnJ7DtNrrHGJ_GiU0JmDxjAtaT9L0LtGkb1tgqBArAQ/s1600/no_post.png";
}
__KBclass__.prototype.toKhNum = function (number) {
this.khNum = {'0':'០', '1':'១', '2':'២', '3':'៣', '4':'៤', '5':'៥', '6':'៦', '7':'៧', '8':'៨', '9':'៩'};
var stringNum = number.toString();
var khString = '';
for(var i in stringNum){
var char = stringNum.charAt(i);
khString += this.khNum[char];
}
return khString;
}
__KBclass__.prototype.srvTime = function(){
var xmlHttp;
try {
//FF, Opera, Safari, Chrome
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//IE
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send();
return xmlHttp.getResponseHeader("Date");
}
__KBclass__.prototype.elapTime = function(rawDate){
var seconds = 1000;
var minutes = seconds * 60;
var hours = minutes * 60;
var days = hours * 24;
var weeks = days * 7;
var months = days * 30.5;
var years = days * 365;
var todayDate = new Date(this.today);
var t = todayDate.getTime();
var publishedDate = new Date(rawDate);
var pt = publishedDate.getTime();
var time = t - pt;
if((time / years)>1){
var ptime = Math.round(time / years);
ptime = __KBobject__.toKhNum(ptime)+' '+'ឆ្នាំមុន';
}else if((time / months)>1){
var ptime = Math.round(time / months);
ptime = __KBobject__.toKhNum(ptime)+' '+'ខែមុន';
}else if((time / weeks)>1){
var ptime = Math.round(time / weeks);
ptime = __KBobject__.toKhNum(ptime)+' '+'សប្តាហ៍មុន';
}else if((time / days)>1){
var ptime = Math.round(time / days);
ptime = __KBobject__.toKhNum(ptime)+' '+'ថ្ងៃមុន';
}else if((time / hours)>1){
var ptime = Math.round(time / hours);
ptime = __KBobject__.toKhNum(ptime)+' '+'ម៉ោងមុន';
}else if((time / minutes)>1){
var ptime = Math.round(time / minutes);
ptime = __KBobject__.toKhNum(ptime)+' '+'នាទីមុន';
}else{
var ptime = Math.round(time / seconds);
ptime = __KBobject__.toKhNum(ptime)+' '+'វិនាទីមុន';
}
return ptime;
}
__KBclass__.prototype.createThumb = function (postContent){
var div = document.createElement( 'div' );
div.innerHTML = postContent;
var img = div.getElementsByTagName("img");
if(img.length>=1) {
return img[0].src;
}else{
return (this.noPost);
}
}
__KBclass__.prototype.getPost = function (json){
this.postUrl = [];
this.postTitle = [];
this.postThumb = [];
this.postDate = [];
this.postIdList = [];
this.postData = [];
var postList = json;
this.postList = postList;
for(var i =0; i<postList.length; i++){
this.postUrl.push(postList[i].link);
var postContent = postList[i].content.rendered;
this.postData.push(postContent);
this.postIdList.push(this.postList[i].id);
this.postTitle.push(postList[i].title.rendered);
this.postThumb.push(this.createThumb(postContent));
this.postDate.push(postList[i].date);
}
}
__KBclass__.prototype.setPostCategory = function(){
var html = '';
for (var i=0; i<this.postData.length; i++){
var str = this.postData[i];
if(str.indexOf('__video-id__') != -1){
html += '';
html += '
';
html += '
';
html += ''+__KBobject__.elapTime(this.postDate[i])+'';
html += ''+this.postTitle[i]+'';
html += '';
}else{
html += '';
html += '
';
html += ''+__KBobject__.elapTime(this.postDate[i])+'';
html += ''+this.postTitle[i]+'';
html += '';
}
}
html += '';
$('.post-category').html(html);
if(!this.categoryHeight){
var width = $('.post-category .img-frame').css('width');
this.categoryHeight = parseInt(width)/16*9;
}
}
__KBclass__.prototype.adjustFrames = function(){
$('.post-category .img-frame').css('height',this.categoryHeight);
}
__KBclass__.prototype.getPostsByCategory = function(options){
var rootDir = options.rootDir || __rootDir__;
var category = options.category || '';
var posts = options.posts || 4;
var page = options.page || 1;
$.ajax({url: rootDir+"/wp-json/wp/v2/posts?per_page="+posts+"&categories="+category+"&page="+page, success: function(json){
__KBobject__.getPost(json);
__KBobject__.setPostCategory();
__KBobject__.adjustFrames();
__KBobject__.currentPage = page;
}});
}
var __KBobject__ = new __KBclass__();
បន្ទាប់មកទៀត ដើម្បីអាចយកកូដ JavaScript នៅក្នុងឯកសារ script.js ខាងលើមកប្រើនៅក្នុងគេហទំព័ររបស់យើងបាន យើងត្រូវត្រូវសសេរបន្ថែមនៅក្នុងឯកសារ header.php ដូចខាងក្រោមនេះ៖
<head> <?php wp_head(); ?> </head>
បន្ទាប់មកទៀត នៅក្នុងឯកសារ home.php យើងត្រូវសរសេរបន្ថែមនូវកូដដូចខាងក្រោមនេះ៖
<?php
/*
*នេះជាទំព័រមុខរបស់គំរូគេហទំព័រ Lotus Blue
*/
get_header();
?>
<?php
if(!wp_is_mobile()){
get_template_part( 'template-parts/home', 'latest' );
}
?>
<?php
get_sidebar();
get_footer();
បន្ទាប់មកទៀត នៅក្នុងថត template-parts យើងត្រូវបង្កើតឯកសារមួយមានឈ្មោះថា home-latest.php ដែលនៅក្នុងនោះ យើងត្រូវសរសេរកូដដូចខាងក្រោមនេះ៖













