display text based on category in wordpress - php

how can i solve the following problem. Basically I want to show different sub headings based on the wordpress category being used.
this is the following code:
$categ = the_category(' ');
if($categ == 'News'){
$second_header = " secondry header displayed here";
}else{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Right now this is not working, instead of checking against the text 'news' is there any way to check against the category id?
thanks in advance.

You can use the following to store the current category id:
<?php $catID = the_category_ID($echo=false);?>
The echo false stops any echo on the page and stores the variable. You can then do the following:
<?php
$categ = the_category_ID($echo=false);
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Alternatively to this you could also use the following (as I believe the_category_ID is now deprecated http://codex.wordpress.org/Function_Reference/the_category_ID):
$categories = get_the_category();
$categ = $categories[0]->cat_ID;
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>

Different Text on some Category Pages:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
if you need category id you will need to first get category through <?php get_the_category_by_ID( $cat_ID ); ?>
Because the_category_ID is deprecated, then you can do the same with some modification.
More here - http://codex.wordpress.org/Category_Templates

Related

using and multiple else if statement- output unexpected end of file

i have been trying to echo the codes and it not working properly. after reading up ifelse statement, i still cant figure what wrong with this statement in php outputting unexpected end of file.
<?php if($current_type){?>
<?php echo " Manage Product Type ";
echo "Menu Name:". $current_type["product_type"]."<br>";?>
Edit Subject;
<?php}elseif($current_category){?>
<h1> Manage Product category </h1>
<?php echo "Menu Name:". $current_category["category_name"];?>
<?php}elseif ($current_product){?>
<h1> Manage Products </h1>
<?php echo "Menu Name:". $current_product["product_names"];?>
<?php }else{?>
Please select a Product, category, or type
<?php};?>
This is a huge improvement on the readability of your code. It uses PHP alternate syntax for control structures.
<?php if($current_type): ?>
<h1>Manage Product Type</h1>
Menu Name: <?php echo $current_type["product_type"] ?><br>
Edit Subject
<?php elseif ($current_category): ?>
<h1>Manage Product category</h1>
Menu Name: <?php echo $current_category["category_name"] ?>
<?php elseif ($current_product): ?>
<h1>Manage Products</h1>
Menu Name <?php echo $current_product["product_names"] ?>
<?php else: ?>
Please select a Product, category, or type
<?php endif ?>
try this,
<?php if($current_type){ ?>
<?php echo " Manage Product Type ";
echo "Menu Name:". $current_type["product_type"]."<br>";?>
Edit Subject;
<?php } else if($current_category){ ?>
<h1> Manage Product category </h1>
<?php echo "Menu Name:". $current_category["category_name"];?>
<?php } else if ($current_product){?>
<h1> Manage Products </h1>
<?php echo "Menu Name:". $current_product["product_names"];?>
<?php } else {?>
Please select a Product, category, or type
<?php }?>
Use this rationale
<?php if(1 == 1) { ?>
<?php echo "xxxxx"; ?>
<?php } elseif(1 == 2) { ?>
<?php echo "xxxxx"; ?>
<?php } elseif (1 == 3){ ?>
<?php echo "xxxxx"?>
<?php } else { ?>
<?php echo "xxxxx"?>
<?php } ?>

PHP if function joomla no work

Hello everyone and thank you in advance that I will answer. I have this problem, I want to see the second page where I find a link that takes me home page blog if in other categories.
I developed this little code:
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php $link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53" ?>
<?php $homeblog = "Blog" ?>
<?php $doc = JFactory::getDocument(); ?>
<?php $page_title = $doc->getTitle(); ?>
<?php echo $page_title; ?>
<?php if ($page_title != $homeblog) ?>
<?php echo "<a href='".$link_address."'>Home Blog</a>"; ?>
<?php endif; ?>
<?php echo $this->escape($this->params->get('page_title')); ?>
Unfortunately although not error the result is the same, or as if what I wrote was invisible. In homepage obviously sees the home page link (and that is the rule I)
Your code is missing a colon at the end of if statement.
Change
if ($page_title != $homeblog)
To
if ($page_title != $homeblog):
Also dont use <?php and ?> every line. You can have the code like this
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php
$link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53";
$homeblog = "Blog";
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
echo $page_title;
if ($page_title != $homeblog):
echo "<a href='".$link_address."'>Home Blog</a>";
endif;
echo $this->escape($this->params->get('page_title'));
?>

Joomla 3 - category link breaking if category title contains an ampersand &

I have Joomla v3.x.x template override that creates an isotope layout with some category filters.
The problem I have is that if an ampersand (&) is put into the title of the category, that particular category filter will not show.
Is the a way to escape or allow the ampersand (&) to show and not break the filter for that particular category?
Code:
<?php
defined('_JEXEC') or die;
JHtml::_('bootstrap.tooltip');
if (count($this->children[$this->category->id]) > 0 && $this->maxLevel != 0) : ?>
<?php foreach ($this->children[$this->category->id] as $id => $child) : ?>
<?php if ($this->params->get('show_empty_categories') || $child->numitems || count($child->getChildren())) : ?>
<?php $data_name = $this->escape($child->title); ?>
<?php $data_option = str_replace(' ', '', $data_name); ?>
<li class="btn btn-primary"><?php echo $this->escape($child->title); ?></li>
<?php if (count($child->getChildren()) > 0) : ?>
<?php
$this->children[$child->id] = $child->getChildren();
$this->category = $child;
$this->maxLevel--;
if ($this->maxLevel != 0) :
echo $this->loadTemplate('children');
endif;
$this->category = $child->getParent();
$this->maxLevel++;
?>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif;
This is the particular line that create the filters from the category titles:
<li class="btn btn-primary"><?php echo $this->escape($child->title); ?></li>
The issue is most likely tied to the rendering of the filter select. Try changing the following line:
<?php $data_name = $this->escape($child->title); ?>
to:
<?php $data_name = htmlspecialchars($child->title); ?>
And see if that resolves the issue.

Magento static Blocks is not displaying properly.

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Echoing post meta in divs

Argh, this code is not pulling through my custom meta.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (!empty($post_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
But the one below works, the only reason I am not using it is because it still shows the speach marks and dash when the fields are left empty in the admin panel
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
echo "<div class='client-testimonial'>". "'".$my_meta['testimonial']."'". "</div>";
echo "<div class='client-name'>". "-" .$my_meta['name']."</div>";
?>
Please help me on why the first code is not echoing the info. I am at the end of my tether!
You're checking if $post_meta is not empty, you don't have a variable named $post_meta
Change:
if (!empty($post_meta))
to
if (!empty($my_meta))
i think u have checked wrong variable.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (isset($my_meta) && !empty($my_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>

Categories