Normaly in Drupal 7 we have node.tpl.php:
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
It is taking $node_url and putting it on a Title in every Node.
I do have 5 nodes (pages) displayed:
First
Second
Third etc
I have created images First.gif, Second.gif and I want to load that images instead of Title.
I did check various implementation, but didn't find any resolution for me.
[Update] I did try to edit template.php file and to add functions for replacing title with image-if images exists. I need this in Drupal 7 - please see here - http://drupal.org/node/221854
Is there any help? Thanks..
Since each node has it's own id then what I'd suggest to you, is to have in your specific files folder images such as node-12, node-13 etc.
In your node.tpl.php
<?php
// path to our file depending on node id
$file = "path/to/file/node-{$node->nid}.gif";
// check if file exists
if (file_exists($file)) {
// theme $title as image with theme_image()
$title = theme('image', array('path' => $file));
}
?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
However I'm sure this is not the best way of development but it work for your question.
Related
I would like to make a custom module with a list of tags. When a tag is clicked the visitor would be navigated to a category page that would show articles with that tag.
I am not a joomla expert, I was thinking about a solution like a hyperlink like this that I would add to the tags inside the module:
href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places"
Is this possible? Or how could I achieve this result?
Thanks!
This is a bit more complicated than just a query string in the URL as you also need to tweak a template.
If you want to keep it as simple as possible, I'd would recommend creating a new K2 template using template overrides and editing the category template so that it would read the query string parameters and show only articles already filtered by the category and furthermore by the tag via a query string.
That's just a brief how-to, now with a lil bit more details:
1) Create a new K2 template using template overrides.
In your template, if it doesn't exist already, create a folder structure /templates/your_template/html/com_k2/templates/default. The "default" can be replaced with any name if you want to have more K2 templates, but you have to set the new template to each category you have manually.
Now take the content from "/components/com_k2/templates/default" and copy it to the new folder in your template. Now, K2 is using the templates from your /templates/your_template/html/com_k2/ folder. Feel free to google more details if you don't understand template overrides, it's pretty important thing when customizing a template.
2) Edit your category view file to accommodate the list to your query strings
The file that interests you now is in /templates/your_template/html/com_k2/templates/default/category.php. Open this file and try to understand what's important there:
Line 141
<?php foreach($this->leading as $key=>$item): ?>
Line 169
<?php foreach($this->primary as $key=>$item): ?>
Line 197
<?php foreach($this->secondary as $key=>$item): ?>
Line 226
<?php foreach($this->links as $key=>$item): ?>
This is what matters. In these four foreach loops, there are all the items. Then, you can wrap the content of each of these loops into an if-condition to check whether it has the desired tag that is specified in the URL.
To show you an example, this is the code for <div id="itemListPrimary">. You can replace this whole div in the category.php file with the following code and it will work flawlessly. I've just written and tested it.
<div id="itemListPrimary">
<?php foreach ($this->primary as $key=>$item): ?>
<?php
# Get the value of the "tag" query string
$jInput = JFactory::getApplication()->input;
$myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
//$myTag = JRequest::getVar('tag'); // for Joomla 1.5
# If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
if (empty($myTag)) {
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
# Otherwise the tag is set so we'll filter the articles by the tag
} else {
# Get an array of all the tags that the current article in the loop has
$articleTags = array();
foreach ($item->tags as $tag) {
$articleTags[] = $tag->name;
}
# Check if the article has the tag specified in the URL as a query string
if (in_array($myTag, $articleTags)) {
# Now the default content of the foreach loop comes as written in the default K2 category.php template
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
}
} ?>
<?php endforeach; ?>
</div>
3) Understand how the URLs will work
My typical category URL is:
http://mywebsite.com/category-name
To show only articles with a specified tag, use:
http://mywebsite.com/category-name?tag=your-tag
For instance, if you want to show only articles with the "Tokio City" tag, use:
http://mywebsite.com/category-name?tag=Tokio City
Done.
That's the basics of what you needs. It's all you need if you use primary articles only (no leading and secondary or links). Of course there are plenty more things you might want to take care of:
a notice if there is no article with the specified tag
no redundant code, I've written it like this for the sake of simplicity and readability
SEO - spaces and special characters in URLs
making sure no empty div will be printed
But that would be way more code and I wanted to keep it simple & readable for you. I think I gave you more than enough for a start, so go ahead and get it done, good luck :)
I'm pretty new in PHP and what I would like to do is for PHP to check if a certain image file with a specific name exists in a specific directory, then echo the name of the file, but if it doesn't exist, then just show XXX.png.
I currently have a page (http://powerplantv2.jehzlau.net/ppm-deals) that echoes all product names from a certain attribute in Magento.
This page calls all images based on the attribute name. For example in my page there's an attribute name called "cool haan". So it automatically calls the image named "coolhaan.png". If there's an attribute name called "levis" then it will show an image named levis.png.
But I don't know how to add a condition if levis.png doesn't exist in the directory, I just want to call XXX.png.
How can I let PHP check first if the image exists that matches the certain attribute in the directory, then show attributename.png, if now, XXX.png.
Currently, below is my code:
<?php
$name ='deals';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem(); $attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); $ppmtenants =
$attribute ->getSource()->getAllOptions(false); ?>
<?php foreach ($ppmtenants as $ppmtenant): ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php endforeach; ?>
The code above gets all the options from a certain attribute then displays all the images with the attribute name. What I want to do is to check for the image name first before showing it.
To simplify my question, I just want to add a placeholder image with the name XXX.png for attributes with no images yet. :D
You can use file_exists() http://php.net/manual/en/function.file-exists.php to check if the file exists and then display the image, otherwise output the placeholder image.
<?php if(file_exists(path_to_your_file)) {
// Image does exist, fetch the image
} else {
// Image doesn't exist, output xxx.png
}
?>
Both the answers posted so far are correct, but to make things easier for you, you can do something like this:
<?php foreach ($ppmtenants as $ppmtenant):
if(file_exists($this->getUrl()."media/wysiwyg/Deals/".strtolower($ppmtenantclean).".png"))
{ ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php
}
else
{
echo '<img src="xxx.png" alt="No image" />';
}
?>
<?php endforeach; ?>
<?php
if (!file_exists("PATH_TO_IMAGE") {
//display xxx.png
} else {
//load the image
}
I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>
I wonder if its possible to change the description of one category to be just after the products.
Example here:
http://www.theprinterdepo.com/refurbished-printers/monochrome-laser-refurbished-printers
I suppose there is one phtml file somewhere that I could change easily for this
I found the file, and the code is as follow
<?php if($_description=$this->getCurrentCategory()->getDescription()): ?>
<div class="category-description std">
<?php
//$currentPage = $this->getCollection()->getCurPage();
echo $_helper->categoryAttribute($_category, $_description, 'description')
?>
</div>
<?php endif; ?>
Howver the getCurPage gives an error
// get category id
$category = $category->load($category_id);
// get collection
$category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');
// print out the description
echo $category->getDescription();
// get current page
$currentPage = $this->getCollection()->getCurPage();
if($currentPage = 1) {
// show Description Here
echo $category->getDescription();
} else {
// Show category name instead of description
echo $category->getName();
-- EDIT --
try this instead of above
$currentPage->getSelect()->getCurPage();
the file you are looking for is located in theme folder under catelog/category/view.phtml. so if have any custom theme installed locate the file first in your theme, if the file is not there then you can copy the file from the base folder and put in your theme folder, be sure to put the file in the correct folder otherwise it will not going to work. Like
if file is located in
app/design/frontend/base/default/template/catalog/category/view.phtmlput the file in
app/design/frontend/default/YOUR_THEME/template/catalog/category/view.phtml.This way if you upgrade the magento to newer version, the changes you made will not be over written by the system
If you want to put Category description after product listing, you just need to move elements around.
The template you need to modify is this:
app/design/frontend/{package}/{theme}/template/catalog/category/view.phtml.
Take this code:
<?php if($_description=$this->getCurrentCategory()->getDescription()): ?>
<div class="category-description std">
<?php echo $_helper->categoryAttribute($_category, $_description, 'description') ?>
</div>
<?php endif; ?>
and move it at the bottom of the file. That's it.
What I'm trying to do is to add some HTML tags to my Joomla! module titles. I will need something like this
Some <b>Title</b>
but when I save !Joomla trims the titles and remove all HTML tags.
I've check the administrator/com_content, which I think should be responsible for inserting the database data, but I couldn't find the answer there.
Can anyone help me with this headache?
Check out ../templates/system/html/modules.php
You can style your module structure in HTML.
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then call your styled module in index.php:
<jdoc:include type="modules" name="right" style="myCustomModule" />
So I found the solutions. It includes both of the previous answers, so I'm putting a new one here with the correct code.
First of all I need to say, that this solution works only for a fixed amount of words (last one, two, etc.) I need only to have the last one, so I will post an example code with one word.
First as SMacFadyen sad I needed to create a new module structure in my template html folder: /templates/system/html/modules.php file.
Note: If you don't want to add this new module styling to all templates, but just on one of them you need to put the module.php in your template's html folder.
The provided by SMacFadyen looks like this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then expired by the comments of Hanny I've added some php code to match the last word of the title and to store it in a new varibale.The code looks like this:
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
Note: the $wrap_tag variable stores the tag you want. You can put b, em, u and etc. to have different result.
The last thing was to replace the displayed title, so I've replaced this code:
<h1><?php echo $module->title; ?></h1>
with this one:
<h1><?php echo $html_title; ?></h1>
The final result was this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $html_title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Thanks to everybody for the help.
The Gantry framework can help you accomplish what you want (1st word styled one way, 2nd word styled another) - but it's a lot of overhead just accomplish that one task you're looking for. Ultimately you'll have to create a template override for your template, and then do some creative editing with php in order to get it to display that way.
There's no quick and easy way to get that done. You'll have to do some php coding on the backend and edit the template (use an override so you don't hack core files). Ultimately you'll probably have to code the php to pull apart the title, and apply formatting to each pulled apart word (or string of words as the case may be) using CSS.
Hope that helps.