Kirby CMS - add an image modified timestamp - php

I'm learning Kirby CMS.
I have a page displaying a series of images, I'm unsure how to get the date the image was uploaded.
I think it is something to do with the $file variable?:
$file->modified($format=false)
the last modified timestamp
here is my php loop:
<?php if($page->hasImages()): ?>
<ul class="gallery">
<?php foreach($page->images() as $image): ?>
<div class="gallerySegment">
<h3 class="guidelineHead"><?php echo $image->title() ?></h3>
<p><?php echo $image->caption() ?></p>
<li><img src="<?php echo $image->url() ?>" width="<?php echo $image->width() ?>" height="<?php echo $image->height() ?>" alt="<?php echo $image->title() ?>" /></li>
</div>
<?php endforeach ?>
</ul>
<?php endif ?>

I think this isn't possible by default in kirby 1. You could use the php-function filetime().
In case of Kirby 2 you can write $file->modified($format = false) as in your proposal.

You should be able to simply do something like this within your loop.
<p>Uploaded at: <?php echo $image->modified('d. F Y'); ?></p>
This is available in Kirby 1 as well as in Kirby 2 as a single "image object" you have in your $image variable is always an extended "file object". That means you can use all file object methods on image objects as well.
If you need any information about how set the $format ('d. F Y'), you should have a look at PHPs date() function explanation

Related

Advanced Custom Fields - no Image selected

How could I put in a conditional to show a default img if an image is NOT selected in the ACF field $cta_fields['image'] ?
I'd like the default img to be "websiteurl.com/wp-content/uploads/img.jpg"
<?php
$root = get_template_directory_uri();
?>
<section class="k-ctabanner k-cta-banner--main">
<div class="k-inner k-inner--xl">
<div class="k-ctabanner--bgimg" data-src="<?php echo $cta_fields['image']['url']; ?>" alt="<?php echo $cta_fields['image']['alt']; ?>"></div>
<div class="k-inner k-inner--md">
<div class="k-ctabanner--content k-block k-block--md">
<div class="k-preheading k-upcase"><?php echo $cta_fields['preheading']; ?></div>
<h3 class="k-headline k-headline--sm"><?php echo $cta_fields['heading']; ?></h3>
<?php echo $cta_fields['link']['title']; ?> →
</div>
</div>
</div>
</section>
<?php echo $cta_fields['image']['url'] ?: 'wp-content/uploads/img.jpg'; ?>
Assuming $cta_fields['image'] would be null or otherwise undefined in that scenario, then something like
data-src="<?php echo (!empty($cta_fields['image']) ?: 'websiteurl.com/wp-content/uploads/img.jpg'); ?>"
should do the trick, I think.
This uses a (shorthand) ternary operator which you can think of as being a bit like a condensed if/else block.
You'd probably want to modify the "alt=" bit in a similar way too, by the looks of it.

unable to fetch image from the database

I am trying to display images on my web page, the content is getting fetched from my database, but the issue I'm facing is in displaying the image. please, can anyone guide me how should I display the image?
I mean to say the path what I should give
here 'image' is my column name and this is my view
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/');$image?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
Hope this will help you :
Use this : <img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
The whole code should be like this :
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
If $image is the column name(which contains image name) then Replace
<img src="<?php echo base_url('uploads/');$image?>" alt="">
with
<img src="<?php echo base_url();?>uploads/<?php echo $row->image;?>" alt="">
If $image has image path then remove ';' and add '.' on echo base_url('uploads/');$image?> line
You issue here is the following code;
<?php echo base_url('uploads/');$image?>
This is because you are not concatenating the string, rather, just saying it's variable name, so, use of the following will provide the output;
<?php echo base_url('uploads/') . $image' ?>
This replaces the semi-colon part way through for a period (.) which is the PHP concatenation operator
Obviously, there is the issue you are not setting $image, which would require (before usage) of;
$image = $row['image_column_name'];
// Or
$image = $row->img_column_name

Magento store View - Incorrect Size (Attribute) displayed

I have been fixing this store, based on magento. The main problem of this is that Some of the products display on the store view has incorrect size (which is attribute)
If you have a look at the picture, on the grid view (list.phtml) the size is US 16/ UK 18 / EUR 48 while on the single view page (view.phtml) it is US 6/ UK 8/ EUR 38.
Have a look at this link
http://www.sequinqueen.com/shop/in-stock.html
I have gone through the code in the view.phtml and appear that the code request the attribute value correctly according to the code below
<?php
$categories =$_product->getCategoryCollection();
foreach($categories as $_category) {
$cat_arr[] = $_category['entity_id'];
}
if($currentCategoryId==102){
// CATEGORY ID = 102 is IN-STOCK PRODUCT CATEGORY
$sizeStock=$_product->getAttributeText('size');
$day="Ready To Ship";
$searchfabric=$_product->getSearchfabric();
$searchcolor=$_product->getSearchcolor();
$make=$_product->getHandmake();
$handmake="HandMake";
$topname=$_product->getTopname();
<?php $i=$j+1;?>
<?php $j=$j+1;?>
<?php if($j<5){?>
<?php if ($i%$_columnCount==1): ?>
<ul class="products-grid">
<?php endif ?>
<?php if($j==1):?>
<li class="item first" style="height:434px; background:#FFF;">
<?php echo $this->getChildHtml("catalog"); ?>
</li>
<li class="item">
<?php echo $topname; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(162,324); ?>" width="162" height="324" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<div style="width:100%; border-bottom:none; height:80px;">
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($make==1){ echo "<p>".$handmake."</p>";}else{}?>
<?php if($currentCategoryId==102){?>
<!-- display size -->
<p><?php echo $sizeStock; ?></p>
<?php } ?>
<p> <?php echo $day; ?></p>
</div>
</li>
so it came to my thought that it might be something in the store backend, somewhere around attribute modification.
Here is how the previous developer sorted and arranged the position of the size values I am not sure if this is the cause of problem.
Anybody is familiar with this problem?
Any answer is appreciated.
thank you in advance.
Did you try to reindex product data from "System"->"Index Management" ?
Please use this
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);

PHP variable array + loop

I should say first of all I'm not a PHP guy, so if anyone can help with this, I'll do my best to understand any suggestions.
I have the following code that accesses an API and outputs some of the data, via PHP wrapper:
$idMovie=11;
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
This works fine, it shows the data for one item. What I need to do though is show the data for many more items. So far I've just repeated the block and changed the $idMovie variable - but this is of course, is not the way to do it.
I think I need to do this:
Set up an array to hold each variable, so $idMovie[12,34,56,78]
Create a loop to go through each variable, and output the data using
my code block above.
If anyone can point me in the right right direction, that would be most helpful.
Thanks
Dave
There's one very useful construct in PHP - foreach:
<?php foreach($idMovies as $idMovie):
$pelinfo = $tmdb_V3->movieDetail($idMovie); ?>
<h1><?php echo $pelinfo['original_title']; ?></h1>
<h2><?php echo $pelinfo['release_date']; ?></h2>
<img src="<?php echo $pelinfo['poster_path']; ?>">
<p><?php echo $pelinfo['overview']; ?></p>
<?php endforeach; ?>
Here I've used so-called 'alternative syntax', useful when PHP snippets are included in HTML template.
Yet, there's more than one way to iterate through this array. For example:
<?php
$idMovies = array(11, 22, 33, 42);
$pelHTMLs = array_map(function($id) use ($tmdv_V3) {
$pelInfo = $tmdv_V3->movieDetail($id);
// perhaps you should check the result here, no?
return <<<HTML
<h1>$pelInfo[original_title]</h1>
<h2>$pelInfo[release_date]</h2>
<img src="$pelInfo[poster_path]" />
<p>$pelInfo[overview]</p>
HTML;
}, $idMovies);
echo implode("\n", $pelHTMLs);
?>
Here I used array_map function to create an array $pelHTMLs, each element of which is some HTML representation of a movie data, related to an id taken from $idMovies array. Then all these parts are just 'joined' into a single string with 'implode' function - and echoed out.
This form is quite often used in PHP 5.3+ environments (when you can supply an anonymous function into array_map and similar list comprehension functions). But it actually can be done in PHP 5.2 too - you just need to extract this part into a separate function (or class method), then give its name (or array with two params - class name and method name) as 'callback' argument.
$idMovie = array(12,34,56,78);
foreach($idMovie as $id){
$pelinfo = $tmdb_V3->movieDetail($id);
echo "<h1> $pelinfo[original_title] </h1>
<h2> $pelinfo[release_date] </h2>
<img src='$pelinfo[poster_path]'>
<p>$pelinfo[overview]</p>";
}
Try using
while($data = $pelinfo){<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>}
This should go through all of it.
From here you should be able to work out your girst question.
Its as simple as your pseudo-code. Here as an implementation:
<?php
$ids = array('12','34','56','78') //array of movie ids
$foreach($ids as $id) : //I'm a huge fan of foreach vs for
$pelinfo = $tmdb_V3->movieDetail($id);
?>
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php endforeach; ?>
$idMovieArr=array(11,22,35,...);
foreach ($idMovieArr as $key) {
$idMovie=$idMovieArr[$key];
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php } ?>
something like this

Joomla intro image as read more link

I want to make the joomla articles intro image to behave like the read more, and the title link. So the user clicks the image, and the article loads.
I'm not an PHP expert but maybe this is the readmore links code:
<a href="<?php echo $this->item->readmore_link; ?>" class="button<?php echo $this->item->params->get('pageclass_sfx'); ?>">
<?php if ($this->item->readmore_register) :
echo JText::_('Register to read more...');
elseif ($readmore = $this->item->params->get('readmore')) :
echo $readmore;
else :
echo JText::_("Read Article");
endif; ?></a>
This is what i want to do with every intro image on my joomla site.
Thanks !
Just resolved it!
your way of thinking helped me. Thank you!
here's my code:
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>">
<?php
$images = json_decode($item->images);
if (isset($images->image_intro) and !empty($images->image_intro)) {
$imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro;
$class = (htmlspecialchars($imgfloat) != 'none') ? ' class="size-auto align-'.htmlspecialchars($imgfloat).'"' : ' class="size-auto"';
$title = ($images->image_intro_caption) ? ' title="'.htmlspecialchars($images->image_intro_caption).'"' : '';
echo '<img'.$class.$title.' src="'.htmlspecialchars($images->image_intro).'" alt="'.htmlspecialchars($images->image_intro_alt).'" />';
}
echo $this->item->introtext;
?>
</a>
for Joomla 2.5:
in your override for _item.php (Location: yourtemplate\html\mod_articles_news\item.php)
place the following line:
<?php if ($params->get('image')) : ?>
<?php $images = json_decode($item->images); ?>
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<img src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
<?php endif; ?>
<?php endif; ?>
Place it there where you would like it to show up
For example after:
<?php echo $item->beforeDisplayContent; ?>
Your intro image has become a link now.
the isset part, makes sure that if a viewer uses Internet Explorer, it doesn't show up a small red cross box.
Just for the information:
in blog_item.php you can find an example code how it's shown up in an article. Here you can also find the code for imagefloat etc.
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<?php $imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro; ?>
<div class="img-intro-<?php echo htmlspecialchars($imgfloat); ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</div>
<?php endif; ?>
So let me start by explaining what the code that you've posted above does. The entire block of code generates one link: there are a bunch of if statements that are determined based off some settings. For example, if you have set that people need to register in order to read more, the link will say "Register to read more..."
The part that we're interested in here, however, since we want to turn images into links, is the URL that we want the images to link to. This is right in the first line:
<a href="<?php echo $this->item->readmore_link; ?>"
so we know that the URL is provided dynamically thanks to $item->item->readmore_link and all this code is doing is echoing it into the HTML.
All that's left is to edit your Joomla template of the page on which you have your images (probably the same file you took this code from). It looks like this should be part of a greater PHP loop, which loops through all the posts. Somewhere above where you found this code, should be code for the intro image that goes along with that post.
I'm not sure what it'll look like, it could be a <img src="<? stuff here; ?> /> or it could be dynamically generated. Keep reading. If you're still not sure where to find it at the end, edit your post with the full code of the template where you got the above snipping from. Regardless of what it looks like, it is referred to as <WHATEVER IMAGE CODE YOU FOUND ABOVE> in the following step:
You have to wrap that image with "a" tags so that it looks like the following:
<WHATEVER IMAGE CODE YOU FOUND ABOVE>
That should do it. Let me know if you have any trouble, I'll be more than happy to make my post more specific if you can provide more detailed information, but I've tried to explain it well enough that you should be able to figure it out with a couple tries.
As you stated you are not a PHP expert, it sounds like your best bet will be to use a Joomla extension that has similar functionality to what you want.
I believe mod_minifrontpage will work for what you need. It allows you to display a list of articles, and it generates thumbnails for those articles based on the first image to be referenced.
There are article intro images in J, since 1.7.5 and now in latest 2.5.3
what you need is change the defaults for component_content,
you can do it 2 ways, editing views in yourinstall/components/com_content/views/
or use template overrides , you first need to know if your template IS using overrides otherwise if you edit component views in the component itself you will not see changes.
to verify this , go to
site_name/templates/template_name/html folder and check if there is folder name
com_content ,
if that is the case than your template is using overrides and any edits should be done trough there not through component
now to the actual code
this is in
components\com_content\views\featured\tmpl\default_item.php ( THIS I DEFAULT FRONTPAGE ARTICLE VIEW)
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<?php $imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro; ?>
<div class="img-intro-<?php echo htmlspecialchars($imgfloat); ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</div>
<?php endif; ?>
all you would need to do is wrap a element around IMG tag with readmore link like this
<a href="<?php echo $this->item->readmore_link; ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</a>
DO NOT forget that if there is template override for com_content you wold need to edit the featured/default_item.php inside it
In Joomla 3.1, the intro_image layout has been moved to the layouts/joomla/content folder. In my situation, it is called from com_content/views/category/tmpl/blog_item.php like so:
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
I moved that file to my template/html/com_content/category/blog_item.php and then wrapped the call to JLayoutHelper like so:
<?php $link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>
<a href="<?php echo $link; ?>">
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
</a>
If you have Gantry installed on Joomla 3.1, the overrides are in a different location. You will want to navigate to plugins/system/gantry/overrides/3.0/2.5/com_content/category/blog_item.php and wrap the intro image with the read more link code.
<?php $link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>
<a href="<?php echo $link; ?>"><img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/></a>

Categories