Calling latest post

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Frenchi
Posts: 2
Joined: Wed Oct 19, 2005 12:19 am
Location: Port St. Lucie, FL
Contact:

Calling latest post

Post 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
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." -- Mark Twain
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Calling latest post

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Frenchi
Posts: 2
Joined: Wed Oct 19, 2005 12:19 am
Location: Port St. Lucie, FL
Contact:

Post 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!
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." -- Mark Twain
Guest

Re: Calling latest post

Post 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
rs1

Post 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
rs2

Post 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!
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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);
?> 
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
rs4

Post by rs4 »

thank you so much garvin! it worked!
und ich verstehe einbisen deutsch :)
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Guest

Post 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.
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post 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.
Judebert
---
Website | Wishlist | PayPal
Guest

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post 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
Post Reply