Skip to content

05.2 Content Functions

Peter Ivanov edited this page Aug 20, 2013 · 1 revision

Content Functions

Those functions will help you work with the items from the content db table.

function: get_content($params)

Get array of content items (posts,pages,etc) from the content DB table

//you can pass params as string
$data = get_content('is_active=y');

//or pass params as as array 
$params = array();
$params['is_active'] = 'y'; //get only active content
$params['parent'] = 2; //get by parent id
$params['created_by'] = 1; //get by author id
$params['content_type'] = 'post'; //get by content type
$params['subtype'] = 'product'; //get by subtype
$params['title'] = 'my title'; //get by title
$data = get_content($params);

//Order by position
$data = get_content('content_type=post&is_active=y&order_by=position desc');

//Order by date
$data = get_content('content_type=post&is_active=y&order_by=updated_on desc');
 
//Order by title
$data = $get_content('content_type=post&is_active=y&order_by=title asc'); 

//Get content from last week
$data = get_content('created_on=[mt]-1 week&is_active=y&order_by=title asc');
 

Parameters

parameter description usage
id the id of the content
is_active published or unpublished "y" or "n"
parent get content with parent any id or 0
created_by get by author id any user id
created_on the date of creation strtotime compatible date
updated_on the date of last edit strtotime compatible date
content_type the type of the content "page" or "post", anything custom
subtype subtype of the content "static","dynamic","post","product", anything custom
url the link to the content
title Title of the content
content The html content saved in the database
description Description used for the content list
position The order position
active_site_template Current template for the content
layout_file Current layout from the template directory
is_deleted flag for deleted content "n" or "y"
is_home flag for homepage "n" or "y"
is_shop flag for shop page "n" or "y"

function: get_content_by_id($id)

Does what it says - get content by id from the content db table

$single_content = get_content_by_id($id=5);

function: content_link($id)

Return the url for a page or a post

$link = content_link($id=5);
print $link;

function: content_parents($id)

Returns array of parents ids

$link = content_parents($id=5);
print $link;

function: pages_tree($params)

Prints nested tree of pages and sub-pages

// Example Usage:
$pt_opts = array();
$pt_opts['link'] = "<a href='{link}'>{title}</a>";
$pt_opts['list_tag'] = "ol";
$pt_opts['list_item_tag'] = "li";
pages_tree($pt_opts);

// Example Usage to make <select> with <option>:
$pt_opts = array();
$pt_opts['link'] = "{title}";
$pt_opts['list_tag'] = " ";
$pt_opts['list_item_tag'] = "option";
$pt_opts['active_ids'] = 5; //those items will have the selected attribute
$pt_opts['active_code_tag'] = '   selected="selected"  ';
$pt_opts['ul_class'] = 'nav';
$pt_opts['li_class'] = 'nav-item';
print '<select>';
pages_tree($pt_opts);
print '</select>';

// Other options
$pt_opts['parent'] = "8";
$pt_opts['include_first'] =  true; //includes the parent in the tree
$pt_opts['id_prefix'] = 'my_id';
$pt_opts['max_level'] =  2; //the max nesting level of the tree
$pt_opts['include_categories'] =  true; //includes the categories in the tree
$pt_opts['active_class'] =  'active'; // set your own class name of the active item


// Placeholders you can use
// {id}, {title}, {link}, {tn}, {active_class}, 
// {active_parent_class}, {exteded_classes}, {nest_level}