I want to display random images from a folder on my website
example: ../images/
I already thought of using a foreach construction only I don't know how to implement it in the code.
<ul>
{foreach from=../images/ item=imagelink}
<li><img src="{$imagelink}" width="100" height="100" alt=""/></li>
{foreach}
</ul>
I hope someone has a solution I already searched a lot but couldn't find a solution.
I also want to show lateron all the images that are in that folder.
You'll probably want to create your list of random images in PHP, then assign the array to a Smarty variable. Then you'll be able to iterate over the array with a foreach and create your list elements.
<?php
// create array of random images into $images
$smarty->assign(images, $images);
?>
{* template *}
<ul>
{foreach from=$images item=image}
<li><img src="{$image}"/></li>
{/foreach}
</ul>
#paul-delre working properly, so I take the url of all the images in that directory use
$files = glob("img/*.*");
and
$smarty->assign(images, $files);
Thanks for your help :-)
Related
I can't seem to print render an image on my page.tpl.php.
This is the code I use:
<?php print render($content['field_image']); ?>
I can get the title to show up though: <?php print $title; ?>
So what am I doing wrong?
Because you're in a page.tpl not a node.tpl, field_image isn't in $content. The only thing in $content should be regions, which contain blocks.
Use this instead: https://www.drupal.org/project/fieldblock
Here you can use fields as a block. Very easy :)
I need to create a single string in SMARTY from two as below
{$value.b64id} = ?type=1&id=aWQ9
this is what I have got so far, cant seem to get anywhere. I think the quote marks are messing me up!!
{assign var='controls' value='<a style="color: red;" href="http://example.com'|cat:{$value.b64id}|cat:'">click Me</a>'}
so what I want from the end of it
{$controls} = <a style="color: red;" href="http://example.com?type=1&id=aWQ9">click Me</a>
Many Thanks
First of all, you only need to use delimiters for variables if you want to display them in the template. When you're using variables inside a smarty function that's not necessary, so
|cat:{$value.b64id}
should be
|cat:$value.b64id
However, if you need to compose a string to reuse it several times, it's probably better to use {capture}
{capture "controls"}
<a style="color: red;" href="http://example.com{$value.b64id}">click Me</a>
{/capture}
and then just use {$controls}
I am a new guy in smarty.This piece of code will loop all the list,but I just want 10 items, is there anyway to solve this problem in smarty? Thanks!
<ul>
{section name=list loop=$list.list}
<li><span>{$list.list[list].co_title|truncate:20:"...":true}</span></li>
{/section}
</ul>
Set the max attribute:
<ul>
{section name=list loop=$list.list max=10}
<li><span>{$list.list[list].co_title|truncate:20:"...":true}</span></li>
{/section}
</ul>
This and other Smarty section attribute settings can be found in the Smarty Section Docs
I have an array that have duplicated values, I want to print the value only once no matter how many time it exist on the array.
This is the code that I have:
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{$tagitem}
{/foreach}
{/foreach}
This is what it prints right:
kim000kardashian
kim000kardashian
miley000cyrus
miley000cyrus
kim000kardashian
irina000shayk
and this is what I am looking to print
kim000kardashian
miley000cyrus
irina000shayk
Is there a way to achieve this only using Smarty Templates? or any way that I can use on the .tpl files?
Thanks in advance
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{if !$done.$tagitem}
{$done.$tagitem = 1}
{$tagitem}
{/if}
{/foreach}
{/foreach}
I am not sure it works with all the versions.
Maybe it would be a bit cleaner to call an array_unique() in the php.
I need a help. I need to get the main url of an image uploaded in wprdpress from the url of the re sized one's url. If possible the link of the attachment. Experts please help. Thanks in advanced.
Example-
re sized url-
1. http://www.example.com/wp-content/uploads/2013/05/image-1-90x120.jpg
2. http://www.example.com/wp-content/uploads/sites/9/2013/01/image-1-1040x320.png
3. http://www.example.com/wp-content/uploads/custom_upload_folder/image-1-430x210.jpg
what i'm looking for-
1. http://www.example.com/wp-content/uploads/2013/05/image-1.jpg or http://www.example.com/?attactment_id=12
2. http://www.example.com/wp-content/uploads/sites/9/2013/01/image-1.png or http://www.example.com/?attactment_id=12
3. http://www.example.com/wp-content/uploads/custom_upload_folder/image-1.jpg or ttp://www.example.com/?attactment_id=12
I can't use regex as i'm novice at this. again i can't use substr as image sizes of the resized ones are different.
attactment_id is the attachment id if the image is uploaded through the wordpress media uploader.
Thanks a lot.
You may not do it with substr, but some other string operations may help. This will work but I wouldn't recommend using it as it's not very efficient (too many var assignments) and should just give you an idea. However you may still use it if you're not going to parse these urls with some heavy production code.
$url = 'http://www.example.com/wp-content/uploads/2013/05/image-1-90x120.jpg';
list($first, $second, $third, $forth) = explode("-", $url);
echo $first.'-'.$second.'-'.$third.'.jpg';
There are plenty of examples of how to work with strings, especially with explode and implode functions, in order to make it efficient and more elegant.
On the other hand, I don't believe that Wordpress can anyhow retrieve back the original url out from the ones you have provided.
Using this code get main upload image url:
<?php
while ( have_posts() ) : the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'full');
?>
<?php if ( has_post_thumbnail() ) { ?>
<img class="img_slide" src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
<?php
endwhile;
wp_reset_query();
?>