Code snippet: "Anynews"

Code snippet to display news from the news module at any page you want

@author:
Christian Sommer
@copyright:
Christian Sommer © 2007-2009
@maintained:
Bianka Martinovic (Webbird)
@license:
GNU General Public license 3.0
@version:
1.14 (stable)
@requires:
PHP 4.3 or higher, WB 2.7 or higher, WB news module

A: Requirements and Installation of "Anynews"

Note: Since Anynews v0.90 the legacy support for Website Baker versions 2.6.x and below was removed!!!. This means Anynews will only work with Website Baker 2.7 or higher!!!

  1. Download anynews.zip from the addons repository and install it via the Website Baker backend > Add-ons > Modules
  2. invoke the new function displayNewsItems() from a page/section of type code or the template index.php file

B: Using "Anynews"

Invoke the "Anynews" function displayNewsItems() from a code page:

Copy the code below to a page or section of type code.

if (function_exists('displayNewsItems')) {
	displayNewsItems();
}

Invoke the "Anynews" function displayNewsItems() from the template:

Add the following code to the index.php file of your template

<?php
if (function_exists('displayNewsItems')) {
	displayNewsItems();
}
?>

C: The "Anynews" function parameters: displayNewsItems()

You can customize the output of "Anynews" via several optional parameters to the function displayNewsItems(). If you invoke Anynews with displayNewsItems, the following default values are used.

<?php displayNewsItems(
   $group_id = 0,
   $max_news_items = 10,
   $max_news_lenght = -1,
   $display_mode = 1,
   $lang_id = 'auto',
   $strip_tags = true,
   $allowed_tags = '<p><a><img>',
   $custom_placeholder = false,
   $sort_by = 1,
   $sort_order = 1,
   $not_older_than = 0
   ); 
?>
Optional Parameters:
  group_id...        group to show news from (default:= 0 all groups, X:= group X, for multiple groups: array(2,4,5) )
  max_news_items...  maximal number of news shown (default:= 10, min:=1, max:= 999)
  max_news_length... maximal length of the short news text shown (default:=-1 for full news length)
  display_mode...    1:=details (default); 2:=unsorted list; 3:=coda slider; 4-99 (custom template: custom_output_display_mode_X.htt)
  lang_id...         module language file ID (default:= 'auto', examples: 'auto', 'DE', 'EN')

  strip_tags...      true:=remove all tags not allowed via variable $allowed_tags (default:=true); false:=donīt strip tags
  allowed_tags...    allowed tags (default:='<p><a><img>')
  custom_placeholder false:= none (default), array('MY_VAR_1' => '%TAG%', ... 'MY_VAR_N' => '#regex_N#' ...)

  sort_by...         1:=position (default), 2:=posted_when, 3:=published_when, 4:= random order, 5:= number of comments
  sort_order...      1:=descending (default), 2:=ascending
  not_older_than..   0:=disabled (default), 0-999 (only show news `published_when` date <=x days; 12 hours:=0.5)

To find the right "group_id", go to the page of type "News" in the Website Baker backend (section Pages). Move your mouse pointer over the group name you are interested in. Have a look at the browser status bar (Firefox at the bottom) and search for &group_id=xx. If nothing is shown in the status bar, simply click on the group name and extract the group_id from the URL displayed in your browser.

Using the Coda Slider Output ($display_mode = 3):

When using $display_mode = 3 (coda slider), please keep in mind to add the following code line to the head section of the index.php file of your template. Coda Slider requires Javascript enabled to show itīs full potential. However, this effect will also work with Javascript disabled (but less fancy of course).

<?php include_once(WB_PATH . '/modules/anynews/coda-slider.inc.php'); ?>

You can adapt the look and feel of the Coda Slider Output by modifying the two files: coda-slider.css and /htt/coda_slider_output.htt located in the Anynews module folder.

Example for a customised call:
<?php displayNewsItems(
   $group_id = array(3,4),
   $max_news_items = 5,
   $max_news_lenght = 50,
   $display_mode = 2,
   $lang_id = 'auto',
   $strip_tags = true,
   $allowed_tags = '<p><a><img>',
   $custom_placeholder = array('IMG_LINK' => '%img%', 'MY_VAR' => '#(test)#i'),
   $sort_by = 4,
   $sort_order = 1,
   $not_older_than = 0.5
   ); 
?>

The function call above displays news from the groups three and four (array(3,4)). Maximum five news entries (5) are shown at once. The short text is automatically truncated after fifty characters (50). As output format the list style is used (2). Language specific outputs e.g. for the read more text are automatically detected (auto) by searching the URL for a supported language key (e.g. /EN/ --> yourdomain.com/pages/en/page.php). If no language key was found in the URL, the Website Baker constant LANGUAGE is used.

Tags are removed (true) except paragraph, anchor and image tags ('<p><a><img>'). Additional placeholders are added if the regular expression matches, which can then be used in the template files (array('IMG_LINK' => '%img', 'MY_VAR' => '#(test)#i'). Placeholders are named as follows: 'MY_VAR' => 'PREFIX_MY_VAR_NUMBER' (PREFIX:= SHORT/LONG for short/long news text, NUMBER:=counter starting with 1).

The short hand regular expressions (%TAG%) can be used when dealing with inputs embedded in tags. For images, the full image tag is returned, for other tags, only the text between opening and closing pairs (<TAG>...</TAG>) is returned. You can also provide your own regular expression. With '#(test)#i', all matches of the word test (case insensitiv) are returned. Only one capturing group () can be used. The regular expression needs to be enclosed #.

News are outputed randomly (4) in descending order (1). Only news not older than (field `published_when`) 12 hours (0.5 days) will be displayed (0.5).

D: Customizing the output

You can change the output of the module via the HTML template files located in "anynews/htt/" and by applying CSS rules. There is no need to change the PHP code itself.

The HTML template files (anynews/htt/*.htt)

View the example /htt/custom_output_display_mode_4.htt to get a list of all placeholders which can be used in the template. Alternatively you can set $display_mode:=4 to get the output on the screen. The most important placeholders are shown below:

NEWS INFO: {TITLE}, {POSTED_WHEN}, {PUBLISHED_WHEN}, {CONTENT_SHORT}, {CONTENT_LONG}, {LINK} ...
LANGUAGE FILES: {TXT_HEADER}, {TXT_READMORE}, {TXT_NO_NEWS}, {TXT_NEWS} ...

Note: The text stored in {TXT_NO_NEWS} is automatically added if no news are available.

Within the template files, you can add or remove HTML elements like you want, as long as you DO NOT TOUCH THE TWO HTML COMMENT LINES. These lines mark the beginn and the end of a news block. The stuff between these two lines will be outputed as many times as news items exist.

<!-- BEGIN news_block -->
 Custom HTML code and placeholder for news items
<!-- END news_block -->

You can use a custom template output file via the parameter $display_mode. If you set $display_mode to 3, the custom template file /htt/custom_output_display_mode_3.htt is used if it exists in the /htt folder.

Note: You can use the Admin tool: "Addon File Editor" from the Website Baker Add-ons repository to edit the template files of anynews via the backend of Website Baker.

Q: Missing "read more" link?
The link is shown only if the news entry has some "long text". It "disappears" if no long text is found.
If you wish to appear the "read more" link in any case, remove the <!-- BEGIN readmore_link_block --> and <!-- END readmore_link_block --> block markers from the template.

Custom CSS

Using CSS definitions, one can customize the look and feel of Anynews to fit your needs. Per default, the output of Anynews is embedded in a Div container <div class="anynews"> ... </div>. The Div container can be used to style the HTML elements embedded in it without changing the CSS definitions of default elements or other modules.

The best place for your Anynews CSS definitions is the "frontend.css" file located in the Anynews module folder. Please keep in mind, that you have to manually link the Anynews "frontend.css" file to the head section in the "index.php" file of your template to use it. This can be achieved by the following code:

<link rel="stylesheet" type="text/css" href="<?php echo WB_URL;?>/modules/anynews/frontend.css" media="screen"/>

Note: You can use the Admin tool: "Addon File Editor" from the Website Baker Add-ons repository to edit the "frontend.css" file of Anynews via the backend of Website Baker.

The examples below show how you can use CSS definitions to style the Anynews layout to fit your needs.

Q: The font size of the news text is too big?
div.anynews p { font-size:80%; }

Q: You want the news title to show up in brown?
div.anynews strong { color:brown; }

Q: The news header should have a left brown border?
div.anynews h2 { border:1px dotted brown; margin:5px; padding:0; }

Q: You donīt like the bullets in the unsorted list
div.anynews ul{ list-style-type:none; }

Please note: I will not answer questions related to CSS formating as all this information can be found in the WWW. Good starting point is: http://www.css4you.de/ or http://glish.com/css/.

Have fun.
Regards Christian (Doc)