Article headlines

13 posts by 2 authors in: Forums > CMS Builder
Last Post: August 6, 2012   (RSS)

By Jesus - July 15, 2012

Hi everyone,

I'm starting my switch from AM2 to CMS builder and need some advice, so any help will be much appreciate it.

I need to include headlines from all my categories on my index page based on date. Example:

Jul 12 2012, Article name on Category name 1
Jul 10 2012, Article name 2 on Category name 3
Jul 9 2012, Article name 3 on Category name 1
Jul 4 2012, Article name 4 on Category name 2
Jul 1 2012, Article name 5 on Category name 1
etc.

I'll like to have the lastest 5 or 10 articles on my index page. Can someone point me to the right direction on how can I do this?

Thanks in advance,

Jesus

Re: [Jesus] Article headlines

By Jason - July 16, 2012

Hi,

Exactly how you do this will depend on exactly how you have your article section set up. In this example, we'll make the following assumptions:
1) your section is called "articles"
2)you consider the date the record was created as the date of your article
3)your article name is stored in a field called "title"
4)your category field is a dropdown list.

With that in mind, here is how we can get the 5 newest articles:

list($articleRecords, $articleMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => "createdDate DESC"
));


You can then output your records like this:

<?php foreach ($articleRecords as $article): ?>
<p><?php echo date("M j, Y", strtotime($article['createdDate']));?>, <?php echo $article['title'];?> on <?php echo $article['category:label'];?></p>
<?php endforeach ?>


Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Article headlines

By Jesus - July 16, 2012

ok, now I understand it much better.

As I'm moving my categories from my old AM2 to my new CMS I've this question to adjust my categories the best possible way.

Top Level Categories & Subcategories

If I set up my top level categories and subcategories like this:

Sports
Baseball
Soccer
Football
Travel & leisure
Places
Hotels
Socials
Weddings

I'll like the latest headlines on my main page so then it should be something like this?


list($articleRecords, $articleMetaData) = getRecords(array(
'tableName' => 'sports,baseball,soccer,football,travel,places,hotels,socials,weddings',
'allowSearch' => false,
'limit' => 5,
'orderBy' => "createdDate DESC"
));


and then use this code where I need to display the headlines?

<?php foreach ($articleRecords as $article): ?>
<p><?php echo date("M j, Y", strtotime($article['createdDate']));?>, <?php echo $article['title'];?> on <?php echo $article['category:label'];?></p>
<?php endforeach ?>


Thanks for the clarification.

Jesus

Re: [Jesus] Article headlines

By Jason - July 16, 2012

Hi,

the 'tableName' option of getRecords() refers to the section of CMS Builder where your records are stored. In this case, "articles".

If you want to retrieve the 5 latest records from any category, then the first block of code I gave you would work. If you wanted the 5 latest records "baseball", you could use something like this:

list($articleRecords, $articleMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'where' => "category = 'baseball' ",
'orderBy' => "createdDate DESC",
));


Note that this assumes that the value of your category field will store the name of the category, and not the num.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Article headlines

By Jesus - July 16, 2012

Awesome, will try it tomorrow!

Thanks!

Re: [Jesus] Article headlines

By Jesus - July 17, 2012

Jason,

As I'm also trying to display on my right side column the latest 10 articles on each category, I'm using this code:

<?php
list($articleRecords, $articleMetaData) = getRecords(array(
'tableName' => 'accesando',
'allowSearch' => false,
'limit' => 10,
'orderBy' => "createdDate DESC"
));
?>

<?php foreach ($articleRecords as $article): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
<?php endforeach ?>


But the article its not incrementing, right now I've 3 articles on my category and on my sidebar column the system its displaying 3 times the same article (1st one).

Thanks for your help!

Jesus

Re: [Jesus] Article headlines

By Jason - July 18, 2012

Hi Jesus,

There is a small bug in your foreach loop where you are using the variable $record instead of the variable $article.

Try this:

<?php foreach ($articleRecords as $article): ?>
<li><a href="<?php echo $article['_link'] ?>"><?php echo htmlencode($article['title']) ?></a></li>
<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Article headlines

By Jesus - July 18, 2012

Thanks!

This worked perfectly!

Re: [Jesus] Article headlines

By Jason - August 6, 2012

Hi Jesus,

You can filter by a field in the backend by using an advanced search.

Go to the section editor for your articles section. Click on the "Searching" tab. In the box type in:

Category|category|match

*Note, this assumes you named your drop down list "category".

Click "Save Details".

Now when you go to your article list view, you should see a link for "Advanced Search". clicking this will show you a drop down with all your categories. Selecting 1 and clicking "Search" will filter by that value.

This will probably be the best option for you.

You could put each category in a separate section, but you'll then need multiple list and detail pages. You could pull in articles from multiple sections, but you'll need a database call for each category.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/