Custom Blog Post Info

Hi All!

Can anyone see any problems in this code snippet I have modified from the wix example of a custom blog page?

I have removed all the content related to extra data as all I need in things that are in the current blog posts dataset anyway, however…

Nothing at all seems to be happening! (it’s not updating my elements to match data of the current post)

//custom post data

let currentPost
$w.onReady(function () {
    wixLocation.onChange((location) => {
    initPage()
});

async function initPage() {
    $w("#post1").getPost().then(async post => {
        assignPostDataToUIElements()
});

function assignPostDataToUIElements() {
    $w("#postTitle").text = currentPost.title
    $w("#viewCount").text = currentPost.viewCount
    //$w("#likeCount").text = currentPost.likeCount
    $w("#timeToRead").text = currentPost.timeToRead

}

Hello. In looking at your code, my suspicion is that you are having issues in your promise.

You are using “async” but you never “await” anything.

Take a look at this article on working with promises, if you scroll through you will see various types of examples and places in the code you may use promises

Thanks for your reply! I solved this (somehow!) But don’t ask me how, I just kept rearranging things and copying the code untill it worked haha! :slight_smile:

ha! Amazing. Do you mind sharing the working code snippet here so that anyone else having issues can learn from you?

1 Like


//custom post data
const likesWeight = 0.2
const viewsWeight = 0.8


$w.onReady(function () {
    wixLocation.onChange((res) => {
        freshState()
    })
    freshState()
});

function freshState(parameter) {
    $w("#post1").getPost().then(async (res) => {
        if (res) {
            loadInfo(res)
        
        } 
    })
}



async function loadInfo(postRes) {
 let info = await postRes;


    $w("#postTitle").text = await info.title;
    $w("#timeToRead").text = await (info.timeToRead) ? info.timeToRead.toString() : "2";
    $w("#views").text = await (info.viewCount) ? info.viewCount.toString() : "1";
    $w("#likes").text = await (info.likeCount) ? info.likeCount.toString() : "0";
    $w("#coverImage").src = await (info.coverImage)
}
1 Like

@prestonkelpiek9 Thank you! Happy coding.

1 Like

I ran into a similar problem, and found a number of other posts stating the same. Solved for it with the following code, which should work in the instance of customizing elements (Banners, Images, Titles, Text etc.) on a Blog Post Page:

*Just make sure to update the $w(#ElementID) with the ones you have defined on your page, ie. $w ( #Banner )

import wixLocation from ‘wix-location’ ;

let currentPost

$w . onReady ( function () {

wixLocation . onChange (( location ) => { 
    initPage () 
}); 

initPage () 

})

async function initPage ( ) {

$w ( "#post1" ). getPost (). then ( **async**  post  => { 
    currentPost  =  post ; 
    assignPostDataToUIElements () 
}); 

}

function assignPostDataToUIElements ( ) {
console . log ( currentPost )
$w ( “#Banner” ). background . src = currentPost . coverImage
$w ( “#MainTitle” ). text = currentPost . title
$w ( “#Headline” ). text = currentPost . excerpt
$w ( “#ReadTime” ). text = currentPost . timeToRead
$w ( “#ViewCount” ). text = currentPost . viewCount
}