Display category name in template - joomla

I am trying to display a category name in a module position.
I tried:
<?php echo $listing['Category']['title'];?>
It did not work.
I followed this link, but it shows the article title and I need the category one.
I'm working on Joomla 1.7.

Much more simple answer:
<?php echo $this->escape($this->item->category_title);?>

As per the posters comment in the OP:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult();
echo $category;
?>

Travega is really close, his code works on pages, but not on category pages.
When you use $id = JRequest::getString('id'); on a category page (such as a category blog or list page) the id of the category is returned. This means we need more context of the id variable, in this case the 'view'.
Here is my modified version of travega's code:
function getCategoryName() {
//Modified from: http://stackoverflow.com/questions/8928967/joomla-display-catagory-name-in-template
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$view = JRequest::getString('view');
if ($view == 'category') {
$sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
} else {
$sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
}
$db->setQuery($sql);
$category = $db->loadResult();
return $category;
}
Other relevant info:
I've only tested this on Joomla 2.5.3 on cat blog and cat list pages. I've not tested it on anything other than com_content component. This means it probably won't work on weblink, contact, etc pages as you may again loose the context.

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

magento - How to get product meta title?

I use following code to get the current product meta title, but it doesn't work.
$curr_prod = Mage::registry('current_product');
$meta_title = $curr_prod->getMetaTitle();
echo $meta_title;
I can get the correct $curr_prod object, but I don't know which method I should use to get the meta title of current product, anyone can help on this?
Thanks!
$title = $product->getMetaTitle();
if ($title) {
$headBlock->setTitle($title.' - '. $product->getFinalPrice());
}
try the code it might work
Set Meta information(Title, Keyword, Description) on Controller Action. Fetch meta information from product data and set the values for layout head block.
$_product = Mage::getModel('catalog/product')->load($id);
if($_product){
$title = $_product->getMetaTitle();
$keyword = $_product->getMetaKeyword();
$description = $_product->getMetaDescription();
}
$this->loadlayout();
$title = $title!=""?$title:$this->getLayout()->getBlock('head')->getTitle();
$keyword = $keyword!=""?$keyword:$this->getLayout()->getBlock('head')->getKeywords();
$description = $description!=""?$description:$this->getLayout()->getBlock('head')->getDescription();
$this->getLayout()->getBlock('head')->setTitle($title )
->setKeywords($keyword)
->setDescription($description);
$this->renderlayout();

Getting title of a category that has id 80

$db = JFactory::getDBO();
$db->setQuery('SELECT title FROM #__categories WHERE id = 80');
$category = $db->loadResult();
echo $category;
Can anyone tell me why this does not return the title of category with id 80?
and/or is there a better way of doing this? I have an item that shows the id but not the name/title
Try using the following which uses Joomla 2.5 coding standards:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')
->from('#__categories')
->where('id = 80');
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
As nibra mentioned, you can also check to see if it exists like so:
if($result){
echo $result;
}
else {
echo "title with this ID was not found";
}
Your code works, if there is a category with the id 80. So
if $category is null, there is no such category;
if $category is something else, that is the title of your category.
Beide that, the better way to access the database in Joomla! is as Lodder pointed out.

Code to display articles from category ID?

What would be the code to display articles from category (specified by ID)?
In Wordpress this is fairly easy by doing:
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_title();
endwhile;
?>
I'm looking for similar code for Joomla.
$db = JFactory::getDbo();
$id = 50;//example
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');
$db->setQuery((string)$query);
$res = $db->loadObjectList();
foreach($res as $r){
echo '<h3>'.$r->title.'</h3>';
echo $r->introtext;
}
You can use next code template:
$categoryId = $[category id];
require_once("components/com_content/models/category.php");
$category = new ContentModelCategory();
$category->hit($categoryId);
$articles = $category->getItems();
print_r($articles);
There is no direct code for getting this in joomla like word press.
If you want to check the code you can check the code for achieving this by following path.
components/com_content/view/category/view.html.php
and
components/com_content/view/category/tmpl/blog.php
According to my Guess your requirement is to display the articles from same category.
then in joomla you dont need to edit in any code.
for achieving this you can simply create a menu.
and menu layout type should be category blog and Choose your category from the right side category options.
This will get the complete article from that category.
If you want to manage it in your style you can add style based on blog.php file .
Hope this will help you..
Try this:
$articles = JFactory::getDBO()->setQuery("SELECT * FROM #__content WHERE catid = '21'")->loadObjectList();
Of course replace '21' with id of your category.
This is quite easy in Joomla. You can find the code in /components/com_content/models/category.php under getItems()
Below is an example
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState('params', JFactory::getApplication()->getParams());
$model->setState('filter.category_id', $category_id);
$articles = $model->getItems();

Pulling only the title and body of an article (Joomla)

I have inserted the following line into my Joomla template:
<jdoc:include type="component" />
This will bring across the whole post, including the title of the article. How can I bring the featured article title across seperate to it's body/content for styling purposes?
Thank you.
I just tested this code and it seems to work on Joomla 2.5.3:
$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);
echo $article->get("title");
echo $article->get("introtext"); // and/or fulltext
}
$catId = 80;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
$db = &JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article){
$article_id=$article->id;
$article_title=$article->title;
$article_content=$article->get("introtext");

Resources