Page 1 of 1

(Resolved) Add Recent Posts to Footer on Home Page

Posted: Thu Jun 11, 2020 4:59 am
by nootkan
I was wondering how I would add a "Latest Posts" widget to my html based homepage on the root website.

Right now I have a html built website with bootstrap and my bludit blog in a sub directory named blog.

In the past with an older version of Bludit I was able to do it with help from this post:
viewtopic.php?t=917

But now the same code doesn't work for some reason. Can I still do this or has the api changed since the original working code?

Code: Select all

<?php

$url = 'https://www.mywebsite.com/blog/api/show/all/pages/f3fd2843709dcc49d6ebf2efe01ec460';

$jsonData = file_get_contents($url);

$data = json_decode($jsonData, true);

$i = 1;

foreach($data as $post) {

        if($i <= 5) {
       
            $postPermalink = $post['permalink'];
            $postTitle = $post['title'];
            echo '<a class="post-link" href="'.$postPermalink.'">'.$postTitle.'</a>';
            echo '<br>';

            $i++;

        }

        else {

        break;

        }
 
}

?>
Here is what I see in my logs:
[11-Jun-2020 04:16:07 UTC] PHP Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/mywebsite/public_html/index.php on line 287
[11-Jun-2020 04:16:07 UTC] PHP Warning: file_get_contents(https://www.mywebsite.com/blog/api/show ... efe01ec460): failed to open stream: no suitable wrapper could be found in /home/mywebsite/public_html/index.php on line 287
[11-Jun-2020 04:16:07 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/mywebsite/public_html/index.php on line 293
Also is allow_url_fopen supposed to be enabled? I was seeing the wrapper disabled error in my logs referencing this so I enabled it against my better judgement due to the serious security concerns.

The disabled wrapper error disappeared but the invalid foreach argument was still there and it didn't seem to make a difference so I disabled it again.

Re: Add Recent Posts to Footer on Home Page

Posted: Fri Jun 12, 2020 4:41 pm
by Edi
nootkan wrote: Thu Jun 11, 2020 4:59 am Also is allow_url_fopen supposed to be enabled? I was seeing the wrapper disabled error in my logs referencing this so I enabled it against my better judgement due to the serious security concerns.
I think it should be enabled.
The disabled wrapper error disappeared but the invalid foreach argument was still there and it didn't seem to make a difference so I disabled it again.
Is there a value for $data if allow_url_fopen is enabled?

Re: Add Recent Posts to Footer on Home Page

Posted: Sat Jun 13, 2020 9:34 pm
by nootkan
Edi wrote: Fri Jun 12, 2020 4:41 pm
Is there a value for $data if allow_url_fopen is enabled?
I'm not sure what you mean. This code was something that you provided to me in the past (see link in first post). When I enabled allow_url_fopen the only error I seen in the logs was the "invalid argument foreach"

Re: Add Recent Posts to Footer on Home Page

Posted: Sat Jun 13, 2020 10:43 pm
by Edi
I think I had this code from Diego. There is nothing about it in the actual documentation. I will ask him.

Re: Add Recent Posts to Footer on Home Page

Posted: Sat Jun 13, 2020 10:58 pm
by diego
Hi guys, I don’t remember that code :P

The API documentation is here https://docs.bludit.com/en/api/request-a-list-of-pages

Re: Add Recent Posts to Footer on Home Page

Posted: Sun Jun 14, 2020 12:52 am
by nootkan
I've been to that page unfortunately for me I am no php coder and was hoping the original code would still work. I did change the reference from post to page but that didn't seem to work.

Re: Add Recent Posts to Footer on Home Page

Posted: Sun Jun 14, 2020 7:57 pm
by Edi
There are some changes for the use of PHP. If you want to print for example the titles of the posts you can use the following:

Code: Select all

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.mywebsite.com/api/pages?token=[number of token]&published=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$jsonData = curl_exec($ch);

curl_close($ch);

$result = json_decode($jsonData, true);

foreach ($result['data'] as $page) {
   echo $page['title'];
}

?>

Re: Add Recent Posts to Footer on Home Page

Posted: Sun Jun 21, 2020 10:20 pm
by nootkan
Hi Edi, sorry for the late reply. Your code worked fine for the titles but when I tried to add the code from before for the urls of the page titles it didn't work. What should I change in order for the page titles to linked to the actual blog posts?

Code: Select all

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.mywebsite.com/blog/api/pages?token=f3fd2843709dcc49d6ebf2efe01ec460&published=true&published=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$jsonData = curl_exec($ch);

curl_close($ch);

$result = json_decode($jsonData, true);

foreach ($result['data'] as $page) {
   if($i <= 5) {
       
            $pagePermalink = $page['permalink'];
            $pageTitle = $page['title'];
            echo '<a class="page-link" href="'.$pagePermalink.'">'.$pageTitle.'</a>';
            echo '<br>';


            $i++;

        }

        else {

        break;
}

?>

Re: Add Recent Posts to Footer on Home Page

Posted: Sun Jun 21, 2020 10:48 pm
by Edi
There is a } missing at the end. ;-)

Re: Add Recent Posts to Footer on Home Page

Posted: Mon Jun 22, 2020 3:49 am
by nootkan
Edi, well I'll be darned. Works great!
Just when you think you're dumb you're actually blind. ;) ;)
Thanks for the awesome support!