Joomla module get current article title - php

I'm trying to build a module that should show the article title of the current article viewed.
I've started from this code in my default.php layout that shows the title of the page, but I need to edit it so that it shows the article title and not the page title.
$heading = $document->getTitle();
How should I edit it to get the article title instead of the page title?

If this is in the normal article view, the following should work:
$input = JFactory::getApplication()->input;
$id = $input->getInt('id', 0);
if(
$id > 0
&& $input->getString('option') == 'com_content'
&& $input->getString('view') == 'article'
) {
$c = JTable::getInstance('content');
$c->load($id);
echo $c->title;
}

I've solved this way:
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
$heading = $article->get("title");
}
Not sure it's the correct/best solution, but it seems work at the moment.
Any suggestion, comment about this code or better implementation?

Related

Showing Category Count on Html Links

I have html links on my wordpress website sidebar and I would like to show the number of posts related to that link.
The posts are placed under custom listing category defined by the following function:
Php:
function va_cat_menu_drop_down( $location = 'menu', $taxonomy ) {
global $va_options;
$key = 'categories_' . $location;
$options = $va_options->$key;
$args['menu_cols'] = ( $location == 'menu' ? 3 : 2 );
$args['menu_depth'] = $options['depth'];
$args['menu_sub_num'] = $options['sub_num'];
$args['cat_parent_count'] = $options['count'];
$args['cat_child_count'] = $options['count'];
$args['cat_hide_empty'] = $options['hide_empty'];
$args['cat_nocatstext'] = true;
$args['cat_order'] = 'ASC';
$args['taxonomy'] = $taxonomy;
$terms_args['pad_counts'] = false;
$terms_args['app_pad_counts'] = true;
return va_categories_list($args, $terms_args);
}
How can I show that number of posts in a particular category next to my Html link as the following does not work (I guess as the listing category is probably custom taxonomy?)
<?php echo get_category(17)->count; ?>
There is a very simple method to do this. Use wp_list_categories and enter the argument 'show_count' => 1
http://codex.wordpress.org/Template_Tags/wp_list_categories

Joomla create menu item from component

Here is my current component setup. I have a very dynamic page generation component that syncs with data from a external API to create pages for products without extra data entry.
Right now it works at a simple button click to populate all and update any changes, or just update individual fields. What this leads to is the generation of static "pages" from the api in joomla, and the ability to update it from the api.
The problem comes into the fact that this is used as the "home" menu item so the component itself takes the root directory. What I need is each "page" to take a sub menu of home automatically, though just setting the main menu item as home does not seem to work, it leads to the JRoute class getting confused and using component/ , everything I have read so far takes the assumption it is not the default menu item so I am losing home making it fully automatic.
So my question is, is there a function class to create menu items from components in joomla? adding another row to the joomla menu table for each page while i update them "should" solve the problem, I know I can try to figure out how joomla adds them to the database on my own, but I would prefer to use a joomla class/function if at all possible, any ideas?
here is my current router.php, works fine for directly linking to the page but not when using JRoute. There is some uneeded parts to this as I have been doing some extensive testing though.
<?php
defined('_JEXEC') or die;
function GoFormsBuildRoute($query){
$segments = array();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$params = JComponentHelper::getParams('com_goforms');
$db = JFactory::getDBO();
if (empty($query['Itemid'])) {
$menuItem = $menu->getActive();
$menuItemGiven = false;
}
else {
$menuItem = $menu->getItem($query['Itemid']);
$menuItemGiven = true;
}
//print_r($menuItem);
if(isset($query['option'])){
unset($query['option']);
}
if(isset($query['view'])){
$view = $query['view'];
}else{
return $segments;
}
unset($query['view']);
if(isset($query['id'])){
if ($menuItemGiven && isset($menuItem->query['id'])) {
$mCatid = $menuItem->query['id'];
} else {
$mCatid = 0;
}
//echo 'hi';
if(strpos($query['id'], ':') === false) {
$db = JFactory::getDbo();
$aquery = $db->setQuery($db->getQuery(true)
->select('alias')
->from('#__goforms_list')
->where('id='.(int)$query['id'])
);
$alias = $db->loadResult();
$query['id'] = $alias;
}
$segments[] = $query['id'];
unset($query['id']);
}
print_r($segments);
return $segments;
}
function GoFormsParseRoute($segments){
$vars = array();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item = $menu->getActive();
$params = JComponentHelper::getParams('com_goforms');
$db = JFactory::getDBO();
print_r($item);
$count = count($segments);
if($count == 1){
if(isset($segments[0])){
$vars['view'] = 'region';
$alias = str_replace(':','-',$segments[0]);
//print_r($alias);
//echo '<br>';
$query = 'SELECT alias, id FROM #__goforms_list WHERE alias = "'.$alias.'"';
$db->setQuery($query);
$page = $db->loadObject();
if($page){
$vars['view'] = 'region';
$vars['id'] = (int)$page->id;
return $vars;
}else{
$vars['view'] = 'goforms';
}
}else{
$vars['view'] = 'goforms';
}
}
return $vars;
}
?>
in review:
Joomla 2.5
component is at root menu item of site (home)
items from component need to fall under the first level of menu after home
links work, however JRoute class in joomla does not properly make the link.

How to add breadcrumb?

How can I add breadcrumb to a page in my attendance module.
I have used the following hook, but it changed the breadcrumb for all pages in other modules also.
function attendance_init() {
// set the breadcrumb
$breadcrumb = array();
$breadcrumb[] = l('Home', '<front>');
$breadcrumb[] = l('People', 'group/node/'. arg(2) .'/people');
$breadcrumb[] = l('Attendance', 'group/node/'. arg(2) .'/people/attendance');
drupal_set_breadcrumb($breadcrumb);
}
If you are trying to change the breadcrumb for a specific content type nodes, try to use hook_node_view_alter()
Here's an example:
function attendance_node_view_alter(&$build)
{
$node = $build['#node'];
if($build['#view_mode'] == "full" && $node->type == "attendance")
{
$breadcrumb = array();
$breadcrumb[] = l('Home', '<front>');
$breadcrumb[] = l('People', 'group/node/'. arg(2) .'/people');
$breadcrumb[] = l('Attendance', 'group/node/'. arg(2) .'/people/attendance');
drupal_set_breadcrumb($breadcrumb);
}
}
Hope this works... Muhammad

Make Search and Archives match homepage

I have my homepage http://www.faberunashop.com set up as a directory. When you click on the post image, it takes you over to the artists site. Here is the code that I used to make this happen by adding it to the functions.php:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2>'.$title.'</h2>';
}
Now I want to do the same to my search and archive page. What do I adjust to make them behave the same?
I suppose that you use WordPress.
In that case you can change the layout and the behavior of your search results by creating a file with name search.php into your theme for your search results, and another file for archives that called archives.php.
For more information about Template Hierarchy for WordPress you can find here http://codex.wordpress.org/Template_Hierarchy

WordPress Get the Page ID outside the loop

I want to get the page ID before starting the loop in WordPress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body> tag based on it.
If you're using pretty permalinks, get_query_var('page_id') won't work.
Instead, get the queried object ID from the global $wp_query:
// Since 3.1 - recommended!
$page_object = get_queried_object();
$page_id = get_queried_object_id();
// "Dirty" pre 3.1
global $wp_query;
$page_object = $wp_query->get_queried_object();
$page_id = $wp_query->get_queried_object_id();
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the cases):
<?php
/**
* #uses WP_Query
* #uses get_queried_object()
* #see get_the_ID()
* #return int
*/
function get_the_post_id() {
if (in_the_loop()) {
$post_id = get_the_ID();
} else {
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
}
return $post_id;
} ?>
And simply do:
$page_id = get_the_post_id();
Use this global $post instead:
global $post;
echo $post->ID;
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
if (get_option('show_on_front') == 'page') {
$page_id = get_option('page_for_posts');
echo get_the_title($page_id);
}
(taken from Forrst | Echo WordPress "Posts Page" title - Some code from tammyhart)
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup the post ID:
// get or make permalink
$url = !empty(get_the_permalink()) ? get_the_permalink() : (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$permalink = strtok($url, '?');
// get post_id using url/permalink
$post_id = url_to_postid($url);
// want the post or postmeta? use get_post() or get_post_meta()
$post = get_post($post_id);
$postmeta = get_post_meta($post_id);
It may not catch every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
You can use is_page($page_id) outside the loop to check.
This function get id off a page current.
get_the_ID();
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
This is the correct code.
echo $post->ID;
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will help you :)
$page_id = #$_GET['page_id'];
if (!is_numeric($page_id)) {
// Then the uri must be in friendly format aka /my_domain/category/onepage/
// Try this
//$path = '/www/public_html/index.php/';
///$path = '/my_domain/category/onepage/';
$path = $_SERVER['REQUEST_URI'];
// Clean the uri
//$path = str_replace('/', '', $page);
$path = str_replace('.php', '', $path);
//$path = str_replace('?s=', '', $path);
$path = $path ? $path : 'default';
$path_len = strlen($path);
$last_char = substr($path, $path_len -1);
//echo $last_char;
$has_slash = strpos($last_char, "/");
//echo $has_slash;
if ($has_slash === 0) :
$path = substr($path, 0, $path_len -1);
elseif ($has_slash === null) :
$path = substr($path, 0, $path_len);
endif;
//echo "path: ".$path; // '/www/public_html/index'
$page = substr(strrchr($path, "/"), 1);
echo "page: ".$page; // 'index'
}
$my_page_id = 31;
$my_page = 'mypage';
//echo "page: ".$page;
//echo "page_id ".$page_id;
if($page_id == $my_page_id || $page == $my_page)
{
// your stuff....
}
Enjoy!

Categories