News Modules

edited October 2014 in Modules
Hi,

I was modified the news module http://docs.getfuelcms.com/general/pages-variables#news and want to plug into my first page but I got some questions:

What I modified:
1/ Added a "language" field in Database
2/ Modified the news view file style
<?php $slug = uri_segment(2); if (!empty($slug)) { $news_item = fuel_model('news', array('find' => 'one', 'where' => array('slug' => $slug))); if (empty($news_item)) show_404(); } else { $news = fuel_model('news'); } ?> <?php if (!empty($news_item)) : ?> <?php fuel_set_var('page_title', $news_item->headline); ?> <blockquote class="blockquote-red"> <p><i class="glyphicon glyphicon-calendar"></i> <?=$news_item->release_date_formatted?></p> <p><?=$news_item->content_formatted?></p> </blockquote> <?php else: ?> <?php foreach($news as $item) : ?> <blockquote class="blockquote-red"> <p><i class="glyphicon glyphicon-calendar"></i> <?=$item->release_date_formatted?></p> <p><?=$item->get_excerpt_formatted(200, 'Read more »')?></p> </blockquote> <?php endforeach; ?> <?php endif; ?>


Questions:
1/ I plugged the following code in my CMS "home" Pages body, is any other way to do it as I want to view the news in different language.
<h4 class="heading-red">Latest News</h4> {$load->view('news')}
2/ How can I view the database by different language?
3/ How can I limit the foreach to 2 latest news?

Many thanks

Comments

  • edited 3:14PM
    1. You can use the Fuel_language class to detect the language of a page. I'll usually add the following to the fuel/application/views/_variables/global.php file so the $language variable is available on every page$varrs['language'] = $CI->fuel->language->detect();
    I'd recommend reading the following documentations about the Language class and how you can
    http://docs.getfuelcms.com/general/localization
    http://docs.getfuelcms.com/libraries/fuel_language
    2. You'll need to add it as a where parameter in your query (see #3)
    3. $news = fuel_model('news', array('limit' => 2, 'language' => $language));
  • edited 3:14PM
    @admin

    Let me study and try!

    Many thanks.
  • edited 3:14PM
    Thanks! It work perfectly. Here is my code for reference.

    1/ Added the following to the fuel/application/views/_variables/global.php file.
    $vars['language'] = $CI->fuel->language->detect();

    View file:
    <?php $slug = uri_segment(2); if (!empty($slug)) { $news_item = fuel_model('news', array('find' => 'one', 'where' => array('slug' => $slug, 'language' => $language))); if (empty($news_item)) show_404(); } else { $news = fuel_model('news', array('limit' => 2, 'where' => array ('language' => $language), 'order' => 'release_date desc')); } ?> <?php if (!empty($news_item)) : ?> <?php fuel_set_var('page_title', $news_item->headline); ?> <blockquote class="blockquote-red"> <p><i class="glyphicon glyphicon-calendar"></i> <?=$news_item->release_date_formatted?></p> <p><?=$news_item->content_formatted?></p> </blockquote> <?php else: ?> <?php foreach($news as $item) : ?> <blockquote class="blockquote-red"> <p><i class="glyphicon glyphicon-calendar"></i> <?=$item->release_date_formatted?></p> <p><?=$item->get_excerpt_formatted(200, 'Read more »')?></p> </blockquote> <?php endforeach; ?> <?php endif; ?>
Sign In or Register to comment.