PHP concatenation for wordpress homepage img src - php

I've tried my best to looking for other questions similar to mine, but couldn't find any.
I have a php code to show for my homepage only but am having a hard time concatenating this correctly so that the img src php can work properly inside the php tags. I'm still new to php and this is confusing me a bit. Could anyone show me the correct way?
<?php
if ( is_front_page() ) {
echo '
<div id="demo">
<img alt="background" src="<?php echo get_template_directory_uri(); ?>/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="<?php echo get_template_directory_uri(); ?>/images/img2.png"/>
</div>'; }
?>

You don't need PHP tags inside anymore since its already inside your PHP script. Just concatenate that function you're calling.
if ( is_front_page() ) {
echo '
<div id="demo">
<img alt="background" src="' . get_template_directory_uri() .'/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="' .get_template_directory_uri() . '/images/img2.png"/>
</div>
';
}

If you want to use separate tags than try like this.
<?php if ( is_front_page() ) { ?>
<div id="demo">
<img alt="background" src="<?php echo get_template_directory_uri(); ?>/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="<?php echo get_template_directory_uri(); ?>/images/img2.png"/>
</div>
<?php } ?>

You don't need a php tag anymore.
<?php
if (is_front_page()){
echo "<div id='demo'> <img alt='background' src='".get_template_directory_uri()."/images/img1.png'/> <div id='block' ></div> <img alt='logo' src='".get_template_directory_uri()."/images/img2.png'/> </div>";
}
?>

Related

append <div> tag inside dynamically created <li> using PHP

This is for a WordPress site I am working with for practice.
i am using Feedzy rss plugin to dynamically create rss feed items on different pages.
so..if the html structure was already there i could have just added it in the .php file directly inside.
But it is created when the site loads.
That is why i need a php function to find the <li> with class="item" and append the
<div class="mainbox" id="mainbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
</div>
The rss plugin creates this html structure -
<ul>
<li class="item">
<div class="image" ></div>
<span class="title"></span>
<div class="content" ></div>
</li>
</ul>
And I need to create it like this:
<li class="item">
<div class="image" ></div>
<span class="title"></span>
<div class="content" ></div>
<div class="mainbox" id="mainbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
<img src="" alt="" class="imgbox">
</div>
</li>
Please help fix the issue.
This is what i tried and it didnt do anythhing.
function wrap_anchor_text_with_span( $content ) {
if ( !is_admin() && preg_match( '~class=\"(.*?)\"~', $content ) ) {
$content = preg_replace_callback( '~class=\"(.*?)\"~', '_add_span', $content );
}
return $content;
}
add_filter('the_content', 'wrap_anchor_text_with_span', 10);
function _add_span( $matches ) {
// $matches[1] will contain class and other properties of the wrapper tag. Using a preg_match - assign $class to be the css class from the parent function.
preg_match('~class=\"(.*?)\"~', $matches[1], $class);
if ( $class[1] !== 'item' ) { // If the class from wrap_anchor_text function isn't item. Return the original content
// return the original content
return $matches[0];
} else {
// do what you want here.
return '<li class="' . $class[1] . '"><div class="mainbox"></div></li>';
}
}

image slider when empty image

i use this template for my image slider "https://www.jssor.com/demos/image-gallery.slider"
i try to change a image if image1 is empty(NULL) to image2, there is my sample code i have try but doesnt work.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:980px;height:550px;overflow:hidden;">
<?php
if (!empty ($upload_dir.$data['image1'])){
?>
<div>
<img data-u="image" src="<?php echo $upload_dir.$data['image1']; ?>" />
<img data-u="thumb" src="<?php echo $upload_dir.$data['image1']; ?>" />
</div>
<?php
}else{
?>
<div>
<img data-u="image" src="<?php echo $upload_dir.$data['image2']; ?>" />
<img data-u="thumb" src="<?php echo $upload_dir.$data['image2']; ?>" />
</div>
<?php
}
?>
this code is work when image1 is able on databases, but when it null the image is not change as image2
It's because your $upload_dir is not empty so $upload_dir.$data['image1'] would not be empty.
Try changing the condition to:
if (!empty($data['image1'])) {
...
}

CakePHP: Showing images in view.ctp

Goal:
I am trying to show an image in the view.ctp. I am trying to grab the image that was saved with the rest of the information. I am pretty sure the code is 100% correct in the ProductsController, if not that'll be posted next.
Here is the code in the view.ctp for products:
<h1>Viewing Product</h1>
<div><b>Name:</b> <?php echo $Product['Product']['name']; ?></div>
<div>
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
</div>
<div><b>Description:</b> <?php echo $Product['Product']['description']; ?></div>
<div><b>Price:</b> <?php echo $Product['Product']['price']; ?></div>
Read down a bit to see the actual solution.
Shoudln't
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
be:
<img src="<?php echo $path .'/'. $Product['Product']['filename']; ?> " width="300px" />
?
Since the original poster actually answered their own question I thought I'd elaborate.
The Solution:
Replacing:
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
with:
<?php echo $this->Html->image('$Product['Product']['filename']') ?>

PHP else/if statements

I've written the following PHP statement but everytime i try to combine it into an else/if, it breaks.
Can someone please advise? I'm new to PHP and am getting a tad stuck.
Thanks :)
<?php if (is_page( 19 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php }?>
<?php if (is_page( 23 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /></div>
}
<?php }?>
Use this syntax :
<?php if (condtion):?>
// html goes here,
<?php elseif(condtion): ?>
// html goes here,
<?php endif; ?>
<?php if (is_page( 19 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php } elseif (is_page( 23 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /></div>
<?php }?>
Here is an sample if/else using your code
<?php if (is_page(19)) { ?>
//Insert html
<? { elseif (is_page(23)) } ?>
//Insert other html
<? } ?>

changing the style inside "if" statement

I was trying to change the style of only a part of php. This is my codes;
if($fetch_array)
{
$foto_destination = $fetch_array['foto'];
echo "<img src = '$foto_destination' height='150px' width='150px'>";
}
else
{
?>
<div style= "position:absolute; left:350px; top:70px;">
<?php
echo "<img src = 'images/avatar_default.png' height='150px' width='150px'>";
?>
</div>
But, this php part is inside if. This is why i could not change it? I want to display the image where i define it inside the div tag if the statement of "if" is true. How can i do this? Where am i missing?
Thanks
If I understand you correctly, it should be:
<?php
if($fetch_array){
?>
<div style= "position:absolute; left:350px; top:70px;">
<?php
$foto_destination = $fetch_array['foto'];
print " <img src = '$foto_destination' height='150px' width='150px'>";
}else{
?>
<div style= "position:absolute; left:350px; top:70px;">
<img src = 'images/avatar_default.png' height='150px' width='150px'>
<?php
}
?>
</div>
It shows the $foto_destination, if there is one.
HTH
Did you mean like this?
<?php
if($fetch_array) {
$photo = $fetch_array['foto'];
$styles = 'position:absolute; left:350px; top:70px;';
} else {
$photo = 'images/avatar_default.png';
$styles = 'position:absolute; left:350px; top:70px;';
}
?>
<div style="<?php echo $styles; ?>">
<img src="<?php echo $photo; ?>" height="150" width="150" />
</div>
That is correct or you can do an isset()
if (isset($fetch_array) {
...
The only advantage being that it will not error if the variable is undefined
Here's a more compact version, shorttags must be enabled.
<div style="position:absolute; left:350px; top:70px;">
<img src="<?= isset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>" height="150px" width="150px" />
</div>
otherwise:
<div style="position:absolute; left:350px; top:70px;">
<img src="<?php echo isset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>" height="150px" width="150px" />
</div>

Categories