(Resolved) Add Recent Posts to Footer on Home Page

Post Reply
nootkan
Sr. Bludit
Posts: 43
Joined: Tue Feb 14, 2017 4:59 pm

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.
Last edited by nootkan on Sat Mar 20, 2021 10:21 pm, edited 1 time in total.
User avatar
Edi
Site Admin
Posts: 2345
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

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?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
nootkan
Sr. Bludit
Posts: 43
Joined: Tue Feb 14, 2017 4:59 pm

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"
User avatar
Edi
Site Admin
Posts: 2345
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

I think I had this code from Diego. There is nothing about it in the actual documentation. I will ask him.
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
User avatar
diego
Site Admin
Posts: 755
Joined: Sat May 16, 2015 2:53 pm
Contact:

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
nootkan
Sr. Bludit
Posts: 43
Joined: Tue Feb 14, 2017 4:59 pm

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.
User avatar
Edi
Site Admin
Posts: 2345
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

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'];
}

?>
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
nootkan
Sr. Bludit
Posts: 43
Joined: Tue Feb 14, 2017 4:59 pm

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;
}

?>
User avatar
Edi
Site Admin
Posts: 2345
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

There is a } missing at the end. ;-)
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
nootkan
Sr. Bludit
Posts: 43
Joined: Tue Feb 14, 2017 4:59 pm

Edi, well I'll be darned. Works great!
Just when you think you're dumb you're actually blind. ;) ;)
Thanks for the awesome support!
Post Reply