- Creatively Nino
- Sep 19, 2020
- 2 min read
Updated: Sep 20, 2020
Here is the helpful code to get you going! Let me know if you need any more assistance in the comments on YouTube or contact me at the bottom today!
📚 WHAT WAS COVERED:
0:00 Awesome Wix Intro
3:40 Categories Function for Wix Sidebar
6:35 Category Repeater Item Ready Function
10:35 Get Popular Post Function
14:40 Popular Post Repeater Item Ready Function
17:50 Get Tags Function
23:15 Preview Custom Blog Sidebar
Code Snippet to follow along with the video:
Page Code
This is a continuation of the previous article: https://ninotutorials.wixsite.com/creativelynino/post/customizing-your-wix-blog-post-page-using-corvid-wix-code-tutorial-wix-tutorial-2020
This imports the multiple API's
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
This loads the page code
const likesWeight = 0.2
const viewsWeight = 0.8
const trendingPostsToShow = 3
const trendingCategoriesToShow = 5
$w.onReady(function () {
// TODO: write your page related code here...
wixLocation.onChange((res) => {
$w("#input5").value = null;
freshState()
})
freshState()
});
Get the post items
function freshState(parameter) {
$w("#post1").getPost().then(async (res) => {
if (res) {
loadInfo(res)
categories()
popularposts(res._id)
tags(res._id)
$w("#post1").expand()
$w("#statebox9").expand()
} else {
$w("#noshowStrip").expand()
$w("#statebox9, #columnStrip1, #post1").collapse()
}
})
}
Then find the popularpost based on the parameters above.
async function popularposts(parameter) {
await wixData.query("Blog/Posts").ne("_id", parameter).find()
.then((res) => {
if (res.items.length > 0) {
let trendingPosts = res.items.map(post => {
post.trendingStrength = (post.viewCount * viewsWeight) + (post.likeCount * likesWeight)
return post
})
trendingPosts.sort((a, b) => {
return b.trendingStrength - a.trendingStrength
})
trendingPosts = trendingPosts.slice(0, trendingPostsToShow)
$w("#popularPostRepeater").data = trendingPosts //results.items.slice(0, trendingPostsToShow) //
$w("#popularBox").expand()
} else {
$w("#popularBox").collapse()
}
})
}
Then put the information into the repeater to display by using an on ready function.
export async function popularPostRepeater_itemReady($item, itemData, index) {
// Add your code for this event here:
let defaultImage = $w("#popularImage").src;
$item("#popularImage").src = await (itemData.coverImage) ? itemData.coverImage : defaultImage;
$item("#popularImage").alt = await (itemData.title.length > 50) ? itemData.title.slice(0, 47) + "..." : itemData.title;
$item("#popularImage").tooltip = await (itemData.title.length > 50) ? itemData.title.slice(0, 47) + "..." : itemData.title;
$item("#popularTitle").text = await (itemData.title.length > 50) ? itemData.title.slice(0, 47) + "..." : itemData.title;
$item("#popularDateLikes").text = await (itemData.lastPublishedDate) ? (await timeAgo(itemData.lastPublishedDate, true).toString() + ((itemData.likeCount) ? " / " + itemData.likeCount + " Likes" : "")) : ((itemData.likeCount) ? itemData.likeCount : "");
$item("#container2").onClick((event) => {
wixLocation.to(itemData["postPageUrl"])
})
}
Get the tags information via the tags function. Then place them into the selection tags element.
async function tags(parameter) {
await wixData.query("Blog/Posts").eq("_id", parameter).find()
.then(async results => {
//console.log(results)
if (results.items.length > 0) {
let tagList = results.items.map(tager => {
//console.log(tager)
tager.postTags = getTags(tager)
return tager
})
$w("#postTags").options = await tagList[0]["postTags"];
$w("#tagBox").expand()
} else {
$w("#tagBox").collapse()
}
})
}
Use the getTags to return all of the tags information into a format that can be put into the selection tags options field.
function getTags(tager) {
let postTags = []
//console.log(tager)
for (var i = 0; i < tager.hashtags.length; i++) {
postTags.push({ "label": tager.hashtags[i], "value": tager.hashtags[i] })
}
return postTags
}
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
📺 MY PLAYLISTS :
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
💻 WEBSITE
Check out more of the website. If you have a question, let's chat it up below! More information coming soon so stay tuned!
Comments