In a wordpress site, i have defined a default NOT_FOUND_IMAGE in timthumb config file:
// "http://example.com/timthumb-config.php".
<?php
if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', 'http://example.com/img/default.jpg');
Is there any way to force resizing of this image depending on timthumb request parameters. For instance:
// 404 occurs
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2F404-image.jpg&h=180&w=120
// get resized (cropped) default image
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2Fdefault.jpg&h=180&w=120
Try this code:
if (!defined('NOT_FOUND_IMAGE'))
define ('NOT_FOUND_IMAGE','images/ingredient.jpg');
I don't rate timbthumb, and I think it has security issues. Install and use the ThumbGen plugin instead.
You can then use it really easily with a normal editable conditional php statement.. (Pseudo)
<?php if you have a thumbnail {
Output the img with thumbgen resized
} else {
Output the no image, you can even use thumbgen if you want
}
?>
Real world example:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<img src='<?php thumbGen($image_url,175,175, "crop=1"); ?>' />
<?php
}
else
{
?>
<img src="<?php bloginfo('template_url');?>/images/no-photo.png" alt="No Photo!">
<?php
}
?>
Related
Here is the my code, which is not working.
global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );
but this is not working.
thank you in advance.
For parent page id
$post->post_parent;
For current page title
$post->post_title;
For parent page id
<?php
echo wp_get_post_parent_id(get_the_ID());
?>
In Gutenberg:
wp.data.select('core/editor').getEditedPostAttribute('parent')
Hope will be helpful to someone
If you want i.e: create a link to the post parent:
<a href="<?= get_permalink($post->post_parent) ?>">
<?= get_the_title($post->post_parent) ?>
</a>
→ <?= the_title() ?>
which will result in i.e:
Latest News → Some news title
For Astra theme and for page template look.php I did this:
$post->post_parent; will nbot work cause in my case function is out of the loop. I run it via functions.php. $post->post_parent works perfectly when inserting it in page template, but not when editing theme function ;)
function add_script_before_header() {
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
echo get_the_title($grandparent);
}
$after = $parent;
if ( is_page_template( 'look.php' ) ) {
echo $after . ' - ';
}
}
i am using dynamic featured image and adding multiple product images to a single custom post type name as products for a single product but and i am trying to get those images in my template but the array only return me only two sizes [thumb] [full] but i need medium as well below is my code
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
As you guys can see in the anchor tag $featured_image['medium'] this is how i want to echo this anchor tag but unfortunately it don't return me the medium size and i need help in getting the medium size as well. below is the array that i get where you can clearly see only [thumb] and [full]. please help
Array
(
[thumb] => http://www.example.com/wp-content/uploads/2014/07/product-1-120x90.jpg
[full] => http://www.example.com/wp-content/uploads/2014/07/product-1.jpg
[attachment_id] => 254
)
You need to get medium sized image by calling get_image_url function. Try this:
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
$mediumSizedImage = $dynamic_featured_image->get_image_url($featured_image['attachment_id'], 'medium');
echo "<img src = '" . $mediumSizedImage . "' />";
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
All available functions are documented here.
PS: I am author of the plugin.
To grab and resize thumbnail, I use aqua resizer from this link:
https://github.com/sy4mil/Aqua-Resizer
Call the thumbnail to show with this code on the loop:
<?php $thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' );
$image = aq_resize( $img_url, 150, 700, true );
?>
<img src="<?php echo $image ?>" width="150" height="700" alt="<?php the_title(); ?>"/>
It is working well. But only for feature image.
I want to set the caller not for feature image, but also for first post image.
So when I forget to set feature image on my post, the first image will show up as the thumbnail.
I know the code flow should be like this:
if(has_post_thumbnail()) {
// resize post thumbnail here e.g. $img_url = aq_resize...
} elseif($first_img) {
// resize the first img here, $img_url = aq_resize($first_img, ...
} else {
// $img_url = ''; //empty
}
But I'm new on php. Can anybody help?
Thanks in advance
You could put this function in your functions.php, and then call it from anywhere. It will return the source attribute of the first image tag it finds in your post, or a blank string if it doesn't find anything.
function get_first_image_src()
{
$content = get_the_content();
$image_regex = "/<img [^>]*src=[\"']([^\"^']*)[\"']/";
preg_match($image_regex, $content, $match);
if (empty($match))
return "";
return $match[1];
}
I'm trying to have different pictures on every one of my pages built on wordpress.
So I have the following in my index.php file, archive.php file, page.php file, etc:
<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic; ?>" alt="page1" id="mainPageImg" />
Now, in my page.php file, I have the following:
<?php
// TOP PICTURE DEFINITIONS
if ( is_home() ) {
$toppic == 'page1.png';
}
if ( is_page('articles') ) {
$toppic == 'page2.png';
}
?>
How come this does not work? I tried it with one equal (=) sign...
EDIT: If I define $toppic at the top, for example, in the index.php file as follows:
<?php $toppic = 'page1.png'; ?>
Then it works. So therefore, it must be something that has to do with the conditional if is_page/is_home statements. Any ideas?
Thanks!
Amit
Okay, I found the answer.
This is what needs to be done. For the articles (blog) page, at the top section you need to place the following:
<?php // TOP PICTURE DEFINITION FOR ARTICLES PAGE
if ( is_home() ) {
$toppic = 'page1.png';
}
?>
Then, in your page.php file, you can control the picture at the top for all other pages (except 404, where you'd need to put a is_404() in your 404.php. So this is what it looks like:
<?php
// TOP PICTURE DEFINITIONS
if ( is_page('english') ) {
$toppic = 'page1.png';
}
if ( is_page('aboutus') ) {
$toppic = 'page1.png';
}
if ( is_page('newspaper') ) {
$toppic = 'page1.png';
}
else {
$toppic = 'page1.png';
}
?>
And finally, in order to implement this, use the following HTML/php syntax:
<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic ?>" alt="page1" id="mainPageImg" />
That's all. Phew. Finally got it to work :) Had to do it for a client, too!
The slug for your Articles page has to be defined as articles. This is set in the edit page interface, see these directions.
I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>
Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>