Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Blog Module /

Discuss the Blog Module.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Pulling SS Blog from another pages


Go to End


14 Posts   5874 Views

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

29 March 2011 at 2:56am

Hi there!
I've installed Blog module succesfully, and customized it, etc etc...
What i need to know is how to pull the blog feed's latest post (let's say, the last five posts) from another no-children page in the site.

To be clear, i have

home (page)
blog>blog posts

i need to pull the feed with latests posts from the homepage of my site, wich is no blog dependant...
I believe there is a widget that allows me to pull feeds from other blogs, but i don't know if this is valid for this request.

Any help will be much apreciated

thanks in advance

Eduardo

Avatar
Willr

Forum Moderator, 5523 Posts

29 March 2011 at 11:56pm

Tutorial 2 has an example of how to pull pages onto another page type (http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site#showing-the-latest-news-on-the-homepage) though if you were to use that example instead of 'ArticlePage' you want to use the blog pages 'BlogHolder' and 'BlogEntry'

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

13 April 2011 at 5:28am

Willr, Thanks for your reply!

I've tried various recipes i've found here on the blog forum, but it seems like i'm misplacing the code or something, because in 2 tests i don't have any content on the page, and in other one (wich have the code@Pastie http://pastie.org/446929) the template triggers an error!

I have the blog installed under
/blog folder, but for what i've readed here, there is no problem with the path where the blog is, since what we are trying to do is a db query through the CMS.

i'll paste here the code of my actual (working) splash page, wich is the page where i want to pull the content, because i think that i'm placing in the wrong place the new function to pull the posts:

mysite/code/splash.php

<?php
class Splash extends Page
{
static $has_many = array ('Randomizados' => 'Randomizer');

public function getCMSFields()
	{
		$f = parent::getCMSFields();
		$f->addFieldToTab("Root.Content.Randomizados", new DataObjectManager(
		$this,
		'Randomizados',
		'Randomizer',
		array('Descripcion' => 'Descripcion', 'Retratista' => 'Retratista'),
		new FieldSet(
		new HTMLEditorField('Descripcion'),
		new ImageField('Retratista')
		)
		));
		//ADDED LINE
		return $f;
	}

	
}

class Splash_Controller extends ContentController {

}

?>

thanks in advance!

Avatar
Bruce B

Community Member, 164 Posts

13 April 2011 at 6:22pm

The code in your pastie example was close to what I use. This is my version that definitely works for me:

function LatestBlogPosts($num=5) {
$blogs = DataObject::get_one("BlogHolder");
return ($blogs) ? DataObject::get("BlogEntry", "ParentID = $blogs->ID", "Date DESC", "", $num) : false;
}

On the relevant .ss page I have:

<% if LatestBlogPosts(4) %>
<% control LatestBlogPosts(4) %>
<% include BlogFront %>
<% end_control %>
<% else %>
<h3><% _t('NOENTRIES', 'There are no recent news entries. ') %></h3>
<% end_if %>

BlogFront is an include file with all the formatting of the blog post.

Hope this helps.

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

14 April 2011 at 2:23am

Bruce,

Thanks a lot for your repply.
I've tried to implement this, but no luck again, i think i'm still misplacing the code, here i attach the Splash.php file as it looks right now:

Splash.php

<?php
class Splash extends Page
{
static $has_many = array ('Randomizados' => 'Randomizer');

public function getCMSFields()
	{
		$f = parent::getCMSFields();
		$f->addFieldToTab("Root.Content.Randomizados", new DataObjectManager(
		$this,
		'Randomizados',
		'Randomizer',
		array('Descripcion' => 'Descripcion', 'Retratista' => 'Retratista'),
		new FieldSet(
		new HTMLEditorField('Descripcion'),
		new ImageField('Retratista')
		)
		));
		//ADDED LINE
		return $f;
	}

}

class Splash_Controller extends ContentController {

function LatestBlogPosts($num=5) {
$blogs = DataObject::get_one("BlogHolder");
return ($blogs) ? DataObject::get("BlogEntry", "ParentID = $blogs->ID", "Date DESC", "", $num) : false;
} 
	

}

?>

In my template file Splash.ss i have:

......
		<% control Menu(1) %>		
		<div class="columna $URLSegment">
			<h1><a href="$Link">$Title.XML</a></h1>
			<% if URLSegment=blog %>
			<p>Novedades, historias, an&eacute;cdotas y vivencias sobre las diferentes actividades que realizo. Noticias frescas y posts interesantes para fomentar el contacto con los que disfruten el sitio.</p>

   <% if LatestBlogPosts(2) %>
      <% control LatestBlogPosts(2) %>
            <% include BlogFront %>
      <% end_control %>
   <% else %>
      <h3><% _t('NOENTRIES', 'There are no recent news entries. ') %></h3>
   <% end_if %> 		

			<% end_if %>
			$Descripcion
			<% if Children %>
				<ul>
				<% control Children %>
					<li><a href="$Parent.URLSegment#$URLSegment">$Title</a></li>
					<% control Children %>
						<% if Destacado %>
						<li>$ImagenPhotoSet.SetWidth(150)</li>
						<% end_if %>
					<% end_control %>	
				<% end_control %>
				</ul>
			<% end_if %>	
			<% if Children %>
				<% include Mosaicos %>
			<% end_if %>			
		</div>
		<% end_control %>
.......

Always i'm getting the "no entries" message. If i place the code in another place, i don't have any output.
Am i missing something? some <% control %> sentence before pasting the code on the template?
It is right placed the funcion code on the Splash.php file?

Thanks a lot for your support

Regards

Eduardo

Avatar
Bruce B

Community Member, 164 Posts

14 April 2011 at 10:22am

Eduardo,
Try moving the function code into the top section of the splash.php code - just below the getCMSFields function. I put the function in my page.php file so I could call it from anywhere in the site but if you only need it on one page, leaving it on the splash page is fine.

That should give you a blank result if you didn't also create an include file with the blog formatting.

In mysite/templates/Includes I created BlogFront.ss with the following code:

<div class="blogSummary">
<h2><a href="$Link" title="<% _t('VIEWFULL', 'View full post titled -') %> '$Title'">$MenuTitle</a></h2>
$ParagraphSummary
<p class="authorDate"><% _t('POSTEDBY', 'Posted by') %> $Author.XML <% _t('POSTEDON', 'on') %> $Date.Long | <a href="$Link#PageComments_holder" title="View Comments Posted">$Comments.Count <% _t('COMMENTS', 'Comments') %></a></p>
</div>

cheers
Bruce

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

20 April 2011 at 4:59pm

Bruce,

I wanted to thank you for your replies, it finally worked!
what i've did is to place the function on the Page.php file. Maybe i've had some syntax error somewhere on the Splash.php, and now it looks great!
if you want to take a look, you'll see it at www.eduardocesario.com

Thanks again for your help!

Ed

Avatar
Bstarr

Community Member, 25 Posts

2 July 2011 at 7:33pm

I am having the same issue, no matter what I do I'm getting "There are no recent news entries." Can either of you help? I tried the tutorial but couldn't get that to work either.

located in mysite/code/Page.ss

<?php
class Page extends SiteTree {

	public static $db = array(
	);

	public static $has_one = array(
	);

}


class Page_Controller extends ContentController {

	/**
	 * An array of actions that can be accessed via a request. Each array element should be an action name, and the
	 * permissions or conditions required to allow the user to access it.
	 *
	 * <code>
	 * array (
	 *     'action', // anyone can access this action
	 *     'action' => true, // same as above
	 *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
	 *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
	 * );
	 * </code>
	 *
	 * @var array
	 */
	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		// Note: you should use SS template require tags inside your templates 
		// instead of putting Requirements calls here.  However these are 
		// included so that our older themes still work
		Requirements::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}
}

function LatestNews($num=3) {
$news = DataObject::get_one("BlogHolder");
return ($news) ? DataObject::get("BlogEntry", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}

Located in themes/mytheme/Layout/HomePage.ss

<td width="300px">
<h2>Features</h2>
 <% if LatestBlogPosts(4) %>
      <% control LatestBlogPosts(4) %>
            <% include BlogFront %>
      <% end_control %>
   <% else %>
      <h3><% _t('NOENTRIES', 'There are no recent news entries. ') %></h3>
   <% end_if %> 
</td>

And I created the BlogFront.ss and put it in a folder at mysite/template/Includes/BlogFront.ss
(I also tried putting this in with the template includes)

<div class="blogSummary">
   <h2><a href="$Link" title="<% _t('VIEWFULL', 'View full post titled -') %> '$Title'">$MenuTitle</a></h2>
   $ParagraphSummary
   <p class="authorDate"><% _t('POSTEDBY', 'Posted by') %> $Author.XML <% _t('POSTEDON', 'on') %> $Date.Long | <a 

href="$Link#PageComments_holder" title="View Comments Posted">$Comments.Count <% _t('COMMENTS', 'Comments') %></a></p>
</div> 

Go to Top