php variable inside href inside if statement - php

I am trying to make a thumbnail image clickable so that when clicked, it shows the full size image, but I keep getting a PHP error and I'm not sure why. This is a Wordpress site. I suspect it has something to do with the variable that's inside the URL, but I need some guidance. I've searched stack for almost 8 hours now...Here is my code:
if ( has_post_thumbnail() ) {
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
the_post_thumbnail('cb-thumb-600-crop');
echo '</a>';
} else {

I think what's wrong on your code is this:
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
should be:
echo '<a href="' . get_the_post_thumbnail_url($post_id, 'full') . '">';

Try This:
if ( has_post_thumbnail() ) {
$anything=get_the_post_thumbnail_url($post_id, 'full');
$anythings=the_post_thumbnail('cb-thumb-600-crop');
echo ''.$anythings.'';
} else {

The problem is on this line:
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
You forgot the dots between the single quotes near "get_" and ");"
It should look something like this:
echo '<a href="' . get_the_post_thumbnail_url($post_id, 'full') . '">';
Good luck and keep coding! :)

Related

Is there some way though PHP which will display a picture detail on click out of list of a pictures?

I'm doing a PHP assignment having a very basic knowledge of PHP.
I'm looping through a list of pictures from S3, but after looping through I want a selected picture to be opened on click. I'm clueless on how it will proceed further.
I have a product.php page which has to redirect to single .php.
PHP is server side language that send html tags to render in browser side, so you can use client side languages (exp: javascript) to handle it.
You can do it with HTML: Anchors aka Links:
$images = [
'a.jpg',
'b.jpg'
];
echo '<ul>';
foreach($images as $image) {
echo '<li>';
echo '<a href="single.php?image=' . $image . '">';
// small preview-images could be prefixed with 'thumb_' . $image
echo '<img src="' . $image . '" width='100'/>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
In single.php you will get the clicked Image with $_GET['image']

Fixing WordPress Header

I was provided this bit of code to add to my functions.php file by the support team of the company from whom I bought the theme.
I wanted my logo and site name side by side and they gave me this code:
function igthemes_site_logo() {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
$url = home_url( '/' );
echo '<a href="' . esc_url( $url ) .'">';
echo "<img class='logo' src= " . $image[0] . " style='float:left; padding:15px;'>";
echo '<div style="float:left; padding:15px 0;">' . get_bloginfo( 'title' ) . '</div>';
echo '</a>';
}
However, that made it look like this.Two logos and titles
What do I change to just show one logo and one title?
it seems like that php function (igthemes_site_logo()) is called twice. That would be in the PHP code, not in the code you posted which only contains the function itself.

How to echo Wordpress default thumbnail via php inside php if else

Hey guys i have this code
<?php $jthumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ($jthumb> "0")
{
echo $jthumb;
}
else
{
echo "http://placehold.it/350x250&text= Jedcore";
}
?>);background-attachment:fixed;">
What i wanted to do is to replace the
http://placehold.it/350x250&text= Jedcore with an image inside my wordpress theme like
<?php bloginfo('template_url'); ?>/images/theimage.png
But i just cant replace it liek that i will prompt an error,
I'm no expert with php :)
try this:
echo '<img src="'. bloginfo('template_url') . '/images/img.jpg" alt="">';
try after this
change
echo "http://placehold.it/350x250&text= Jedcore";
to
echo get_bloginfo('template_url').'images/theimage.png';

How to return a certain html tag in php

Sorry if this is a really stupid question. I am just starting to learn PHP and am probably jumping the gun a bit.
I am writing a very 'simple' wordpress plugin which has a custom post type and takes the content from it and returns it on the homepage with a shortcode. Below is the part of the code that handles the shortcode.
add_shortcode("new-tub", "new_tub_short");
function new_tub_short() {
$post_id = 87;
return '<a class="new-tub" href="' . home_url( '/test' , __FILE__ ) . '">' . get_post_field('post_content', $post_id) . '</a>';
}
So currently it wraps a link around the content of the post. All that is in the post will be an image, however, I would like to make it fool proof so it doesnt include another link and paragraph tag.
My question is, is it possible to search for the img tag within that post and return that only?
Thanks in advance,
Alex
You can do this by using strip_tags. Try this,
add_shortcode("new-tub", "new_tub_short");
function new_tub_short() {
$post_id = 87;
return '<a class="new-tub" href="' . home_url( '/test' , __FILE__ ) . '">' . strip_tags(get_post_field('post_content', $post_id), '<img>') . '</a>';
}
http://php.net/manual/en/function.strip-tags.php

PHP If & Else Statement in Wordpress

I'm trying to figure out how to do a if else statement in Wordpress. I want it to state if $data[0]['media_upload'] is empty then show the blog title. Else (if there is something there) show the logo in Wordpress. I'm wondering if I have the wrong syntax.
Right now I have the following which is showing the blog title and no errors but I do have a image uploaded and its not showing for some reason. Any ideas would be great!
<?php
if (empty($data[0]['media_upload'])) {
echo'<h1 class="site-title"><a href="';
esc_url( home_url( '/' ) );
echo'" title="';
esc_attr( get_bloginfo( 'name', 'display' ) );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></h1>';
}
else{
echo '<a href="';
bloginfo('siteurl');
echo '">';
echo'<img src="';
global $data;
$data['media_upload'];
echo'" /></a>';
}
?>
Try changing:
$data['media_upload'];
in your else to: $data[0]['media_upload'];
like you have in your if statement (or the other way around). We don't really know what $data is so it's hard to help you.
Edit: Since I can't comment yet, I'll comment here.
Try using the rubber duck technique; explain to us what is happening with the $data object in your code. Maybe you'll figure it out yourself.
it should be
echo'<img src="';
global $data;
$data[0]['media_upload'];
^ // you forgot 0th index
echo'" /></a>';

Categories