Page 1 of 2

Calling latest post

Posted: Wed Oct 19, 2005 12:26 am
by Frenchi
I have s9y displayed on a sub-page of my website, and was curious if there was a way to display the title/link of the latest blog entry on the index page of the site. Any help appreciated! :D

Re: Calling latest post

Posted: Wed Oct 19, 2005 10:44 am
by garvinhicking
Yes, you can do that with Serendipity API methods. Just do this in your main website via PHP:

Code: Select all

<?php
$current_path = getcwd();
chdir('/path/to/your/s9y/');
include_once('serendipity_config.inc.php');
$entries = serendipity_fetchEntries(null, true, 1);
$entry_link = serendipity_archiveURL($entries[0]['id'], $entries[0]['title'], 'baseURL', true, $entries[0]);
echo "<a href='$entry_link'>{$entries[0]['title']}</a>";
chdir($current_path);
?>
Best regards,
Garvin

Posted: Wed Oct 19, 2005 7:33 pm
by Frenchi
Awesome! Thanks a ton Garvin! I did have to remove the first / from in front of the directory name in chdir (it wouldnt take '/log/serendipity/', but 'log/serendipity/' worked), in case it helps anyone in the future. Thanks again!

Re: Calling latest post

Posted: Tue Nov 01, 2005 4:56 pm
by Guest
garvinhicking wrote:Yes, you can do that with Serendipity API methods. Just do this in your main website via PHP:

Code: Select all

<?php
$current_path = getcwd();
chdir('/path/to/your/s9y/');
include_once('serendipity_config.inc.php');
$entries = serendipity_fetchEntries(null, true, 1);
$entry_link = serendipity_archiveURL($entries[0]['id'], $entries[0]['title'], 'baseURL', true, $entries[0]);
echo "<a href='$entry_link'>{$entries[0]['title']}</a>";
chdir($current_path);
?>
Best regards,
Garvin
I used your code in front page of my website, and it works fine. thanks a lot! Now I also want to display the date of posting with the title of the entries. I would appreciate it very much if you can guide me to do this. I have looked around this forum and tried myself but without any success.

thanks,
rs

Posted: Wed Nov 02, 2005 3:49 am
by rs1
Frenchi wrote:Awesome! Thanks a ton Garvin! I did have to remove the first / from in front of the directory name in chdir (it wouldnt take '/log/serendipity/', but 'log/serendipity/' worked), in case it helps anyone in the future. Thanks again!
I figured it out myself. Following serves my purpose. Thanks anyways.

Code: Select all

$date = serendipity_formatTime('%D', $entries[0]['timestamp']);
echo "{$date}";
rs

Posted: Sat Nov 05, 2005 9:47 pm
by rs2
When I call 'body'into my frontpage of website using {$entries[0]['body']}, it does not recoginze line breaks and paragraphs. All paragraphs is displayed as one. Is it possible to solve this problem?

thank you very much!

Posted: Sun Nov 06, 2005 2:01 pm
by garvinhicking
The formating of entries is depending on many markup plugins of Serendipity affecting the input.

So in your custom PHP code you need to also call up those markup plugins. Here's a forum post which should help you:

http://www.s9y.org/forums/viewtopic.php ... +hookevent

It's in german, but if you dont' understand it, the code examples on the bottom of the page should help you.

Basically you'd make it like this:

Code: Select all

<?php
$current_path = getcwd();
chdir('/path/to/your/s9y/');
include_once('serendipity_config.inc.php');
$entries = serendipity_fetchEntries(null, true, 1);
foreach($entries AS $i => $entry) {
  $entries[$i]['display_dat'] = '';
  serendipity_plugin_api::hook_event('frontend_display', $entries[$i]);
  serendipity_plugin_api::hook_event('frontend_display:html:per_entry', $entries[$i]);
  $entries[$i]['plugin_display_dat'] =& $entries[$i]['display_dat']; 
}
$entry_link = serendipity_archiveURL($entries[0]['id'], $entries[0]['title'], 'baseURL', true, $entries[0]);
echo "<a href='$entry_link'>{$entries[0]['title']}</a>";
echo $entries[0]['body'];
chdir($current_path);
?> 

Posted: Sun Nov 06, 2005 2:21 pm
by rs4
thank you so much garvin! it worked!
und ich verstehe einbisen deutsch :)

Posted: Mon Nov 07, 2005 4:34 am
by judebert
Is it possible to do this from index.tpl or entries.tpl? What I'm really interested in doing is displaying a "rants and news" box on the frontpage. Whether I make a plugin or a template, it needs to:

1) Know whether this is the frontpage or not
2) Retrieve one entry (the active "rant")
3) Retrieve a category's entries (the current "news")

I've been looking at the static page plugin for ways to figure out if I'm on the frontpage, but if there's a way to do it in the templates, I'm happy.

Posted: Mon Nov 07, 2005 10:06 am
by garvinhicking
Hm, it is a bit harder to detect whether you're on the frontpage. I think you'll best use a plugin for that.

The staticpage plugin detects that on the 'genpage' hook:

Code: Select all

if ((empty($args) || trim($args) == $serendipity['indexFile']) && empty($serendipity['GET']['subpage'])) {
So this detects if the plain URL was called without any GET parameters or so. You can also put this logic in a templates' config.inc.php file and assign a smarty variable which you check in the entry, then you can get past the plugin approach.

Retrieving an entry also requires the config.inc.php PHP function call hack.

HTH,
Garvin

Posted: Fri Jan 13, 2006 1:17 am
by Guest
garvinhicking wrote:The formating of entries is depending on many markup plugins of Serendipity affecting the input.

So in your custom PHP code you need to also call up those markup plugins. Here's a forum post which should help you:

http://www.s9y.org/forums/viewtopic.php ... +hookevent

It's in german, but if you dont' understand it, the code examples on the bottom of the page should help you.

Basically you'd make it like this:

Code: Select all

<?php
$current_path = getcwd();
chdir('/path/to/your/s9y/');
include_once('serendipity_config.inc.php');
$entries = serendipity_fetchEntries(null, true, 1);
foreach($entries AS $i => $entry) {
  $entries[$i]['display_dat'] = '';
  serendipity_plugin_api::hook_event('frontend_display', $entries[$i]);
  serendipity_plugin_api::hook_event('frontend_display:html:per_entry', $entries[$i]);
  $entries[$i]['plugin_display_dat'] =& $entries[$i]['display_dat']; 
}
$entry_link = serendipity_archiveURL($entries[0]['id'], $entries[0]['title'], 'baseURL', true, $entries[0]);
echo "<a href='$entry_link'>{$entries[0]['title']}</a>";
echo $entries[0]['body'];
chdir($current_path);
?> 
garvin,
is it possible that I only display first paragraph of the 'body'. And add "read more" link?
I am really new to Serendipity. Any help will be appreciated.

thanks.

Posted: Fri Jan 13, 2006 5:39 am
by judebert
This capability is built-in to Serendipity. Put the first paragraph of the body in the "entry body" of the entry; put the rest in the "extended body". Serendipity will display the "entry body" on overview pages, with a "Continue reading <whatever>" link. The individual page will include the "extended body", too.

Posted: Fri Jan 13, 2006 1:56 pm
by Guest
Thanks Judebert.
What should I put here in this line of the code then instead of the 'body'?

Code: Select all

echo $entries[0]['body']; 
Thanks again. I helped.

Posted: Fri Jan 13, 2006 2:08 pm
by garvinhicking
Guest - uhm, you are talking about the PHP code API integration. Are you sure you need this, and you don'T want to use the usual serendipity fetching functions?

You should only do it the "manual" way, if you are coding your own CMS/Framework. In this case you should get yourself enough PHP knowledge to echo/do whatever you want with the result array.

So, why are you not simply using the usual Serendipity Smarty templating to display your entries? There the use of 'extended' and normal 'body' are implemented completely.

Regards,
Garvin

Posted: Thu Feb 09, 2006 9:37 pm
by carl_galloway
Jude, Garvin,

I'm particulary interested in using smarty within one of the template files to call the latest entry only, and I don't want to use a plugin because this is for a new theme so must be able to be used by any user. I can already detect which page I'm on using smarty (ie startpage/overview/detail) but now wish to only include the single latest entry. Can I use a standard variable to select the latest entry, Or would I need to write some code into the template config file and then call a newly created variable. If so, can you give me any hints? I'd actually like to write this myself to test my smarty abilities, but can't figure out where to begin.

Cheers

Carl