1

Topic: Howto display only the last article

There is a place on my website that I need to display only the last article.  I have been trying to use this:

<?php foreach ($page_article->children(array('limit' => 1, 'offset' => 0, 'order' => 'page.created_on DESC')) as $article): ?>

But it does not work.  Can someone help me with this?

Nai

Last edited by nai (2010-09-03 16:00)

Thumbs up

2

Re: Howto display only the last article

Hi nai - this is what I use:

<?php 
$page_article = $this->find('/news/');
$last_article = $page_article->children(array('limit'=>1, 'order'=>'page.created_on DESC'));
?>

If you don't use "news" (in the 2nd line) for where you want this latest article to come from, substitute the slug of what you want (e.g. "blog", "articles" or whatever). And then use it like this:

<h3><?php echo $last_article->link(); ?></h3>

...and so on. Does that work for you? Do let us know!

3

Re: Howto display only the last article

Hi, David.  That works. Thank you.

Thumbs up

Re: Howto display only the last article

David wrote:

Hi nai - this is what I use:

<?php 
$page_article = $this->find('/news/');
$last_article = $page_article->children(array('limit'=>1, 'order'=>'page.created_on DESC'));
?>

David, that actually doesn't works for me.
Only if I do 'limit' => 2, or a greater number it is acceptable, else for limit => 1 it returns

Fatal error: Call to a member function link() on a non-object in
D:\wamp\www\cms\wolf\app\models\Page.php(489) : eval()'d code on line 49

I had to digg deeper so I opened up wolf/app/models/Page.php file to see what children() function is doing in the back.

Everything seemed okay, except for this two lines.

  if ($limit == 1)
     return isset($pages[0]) ? $pages[0]: false;

which I honestly wonder what is the purpose of those lines, that is basically what is causing the fatal error, as we already pass the value of $limit to $limit_string

$limit_string = $limit > 0 ? "LIMIT $offset, $limit" : '';

and as you see there we also filter it by checking if the value of $limit is greater than 0, and defacto 1 is also greater than zero.

plus after we execute the SQL statement, $limit parameter at the end of the function does no good but harm to the children() function itself, it is of no value.

I believe that is a bug, that might have slipped from Martijn's awareness, but fortunately the error is gone once you comment out those two lines, or either remove them for good.

Now I am doing this

<?php $last_article = $this->find('/articles/'); ?>
<?php foreach ($last_article->children(array('limit' => 1, 'order' => 'created_on DESC')) as $article): ?>
  <h2 class="post_title"><?php echo $article->link(); ?></h2>
<?php echo $article->content('excerpt'); ?>
<?php endforeach; ?>

and it returns exactly what I want, and what it should return in first place.

Let me know if I'm mistaking.

P.s. I forgot to tell, I'm talking about Wolf 0.7.0 Nightly. Cheers.

Last edited by tokolo (2010-09-09 19:41)

Re: Howto display only the last article

Please report bugs/issues/patches in the issue tracker.

axjezzy wrote:

Everything seemed okay, except for this two lines.

  if ($limit == 1)
     return isset($pages[0]) ? $pages[0]: false;

which I honestly wonder what is the purpose of those lines, that is basically what is causing the fatal error, as we already pass the value of $limit to $limit_string

How come you feel this is a problem?

This line makes sure that if the limit parameter is set to "1" we return a single Object of type Page instead of an Array with a single entry (the Page object).

Works fine for me on the nightly by the way. smile

axjezzy wrote:
$limit_string = $limit > 0 ? "LIMIT $offset, $limit" : '';

and as you see there we also filter it by checking if the value of $limit is greater than 0, and defacto 1 is also greater than zero.

I don't see how this comment is relevant. Sure we filter it to make sure its larger than 0. But that's not doing the same thing as the code in the first part of my reply. smile

axjezzy wrote:

plus after we execute the SQL statement, $limit parameter at the end of the function does no good but harm to the children() function itself, it is of no value.

I believe that is a bug, that might have slipped from Martijn's awareness, but fortunately the error is gone once you comment out those two lines, or either remove them for good.

The use of the $limit parameter is not a bug. It does no harm (the opposite in fact) and it is most definitely of value.

The $limit parameter is there to make sure the SQL is usuable not only by MySQL but also SQLite. (and in the near future PostgreSQL)

Removing it would break(!) SQLite compatibility.

axjezzy wrote:

Now I am doing this <snip/> and it returns exactly what I want, and what it should return in first place.

Let me know if I'm mistaking.

P.s. I forgot to tell, I'm talking about Wolf 0.7.0 Nightly. Cheers.

Does it return an array with only one entry? I bet it does. In which case, that's not the intended behavior. When setting $limit to "1", we want a single Page object to be returned, not an array.

Anyway, please report bugs/issues/patches in the issue tracker.

As I said, works fine on the nightly.

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution.

Re: Howto display only the last article

mvdkleijn wrote:

Please report bugs/issues/patches in the issue tracker.

How come you feel this is a problem?

As I said, works fine on the nightly.

Martijn, hell of a response you did. I'm impressed with the concise and detailed explanation.

Couple of words though, I wasn't reporting an issue, because initially I wasn't quite sure if I am the one who's doing it wrong, hence my question for correction at the end of my previous post.

Secondly, something that doesn't work, to me resembles to a problem, like the issue in word that's why I so mouthful say that this is a problem.

Thirdly, of course it does return an array and not an object, but if that's not the intended behavior could you please post your working part to echoing $obj->link(); with a LIMIT of 1 since I am certain that it does not work either way, because the if conditional with the $limit parameter at the end of the function tries to take over, something that has been already executed differently.

and lastly, I don't see how removing the last if condition would break SQLite compatibility, I am familiar with most of the differences between MySQL and SQLite, but I must admit I'm very eager to learn it.

Please, be so kind again and give me a thorough explanation. My acknowledgements to you.

P.S. I tried it even on 0.6.0a with the default installation code, on the default layout and everything, just changed limit 5 to 1 and you guessed it, the non-object fatal error shows miraculously.

Update: It is working now.

Martijn, I see the problem was at my side, but I was misguided by the documentation in the Wiki here, so I can't take all the blame upon my shoulders, by this line above to be more precise.

The array produced by children() requires a foreach loop to present usable information.

No it doesn't. That is only valid when we set 'limit' to 2 or greater. With 'limit' set to 1 the error is produced from within a foreach loop of course.

So for clarification to other users as they don't deserve to go through the whole CMS framework files

When we want to echo a single result, or the last post in the articles case, don't use a foreach loop just set 'limit' => 1,
this is valid and works

<?php 
  $page_article = $this->find('/articles/');
  $last_article = $page_article->children(array('limit'=>1, 'order'=>'page.created_on DESC')); ?>
    <h2 class="post_title"><?php echo $last_article->link(); ?></h2>
    <?php echo $last_article->content(); ?>
    <?php if ($last_article->hasContent('extended')) echo $last_article->link('Continue Reading&#8230;'); ?>

P.S. I am submitting the change to the Wiki btw. Have a great day.

Last edited by tokolo (2010-09-10 16:56)