PHP code within HTML style attribute (or Confused by quotes) - php

I am completely confused about the way I should use PHP code in this line:
<div class="post-img" style="backgroung-image:url(' <?php echo $thumbnail[0] ?> ')"></div>
It seems to me that it is read as a text in my example.
I also have tried another approach which also didn't work for the same reason, I believe...
<?php echo '<div class="post-img" style="backgroung-image:url(' . $thumbnail[0] . ')"></div>' ?>
$thumbnail variable contains a link to the main image of a Wordpress post:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ) );
Please, give me an advice, how can I apply a dynamic link to a Wordpress post image?

If your div has no content then you have to add height in CSS like this:
<div class="post-img" style="background:url('<?php echo $thumbnail[0]; ?>')no-repeat; height:200px;">

Try this
<div class="post-img" style="backgroung-
image:url('<?php
wp_get_attachment_image_src(
get_post_thumbnail_id($post_id )[0] ); ?>')">
</div>
Some WordPress function directly display the string. No need to echo them.

generall, the results will be like this,
array{
[0] => url,
[1] => width</em>
[2] => height</em>
[4] => is_intermediate
}
you can do something like this
<?php echo '<div class="post-img" style="backgroung-image:url(' . $thumbnail[0] . ') . $thumbnail[2] ."></div>' ?>

ADyson and Nobita gave me the solution in comments to my question, which is:
echo '<div class="post-img" style="background-image:url(\'' . $thumbnail[0] . '\')"></div>';

Related

How can I put this php into a image background url?

I would be very very grateful for help with this:
I have a bootstrap carousel that I want to include pictures from a database. I want it to appear as background image like this:
<div class="carousel-item" style="background-image: url('link to image')">
This is the PHP-snippet that I want to put in place for the above 'link to image':
<?php
$img_url = "images/programpics/"; {
echo '<img src="' . $img_url . $row_firstrow[ 'show_tix_phone' ] . '"id="pic1" alt="Something here" />';
}
?>
'Link to image' is a part of style, not allow html tags like 'img' and angle brackets. Example syntax:
background-image: url('images/bg.jpg')
With this in mind you code must be like this:
<div class="carousel-item" style="background-image: url('<?php
$img_url = "images/programpics/";
{
echo $img_url . $row_firstrow[ 'show_tix_phone' ];
}
?>')" id="pic1" alt="Something here" />';
Hope help:
<div class="carousel-item" style="background-image: url(<?php echo $img_url . $row_firstrow[ 'show_tix_phone' ] ?>)">

Wordpress & PHP to check if custom field exists, and if not replace with a random string

I'm trying to create a function that checks for the existence of a post_thumbnail() for a post in Wordpress and sets an element's background image appropriately. Here's what I've tried:
<div class="header-cover-image" style="background-image: url(
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_template_directory_uri() . '/assets/img/article-overlay-1.jpg' }?>
">
</div>
This currently crashes/causes the page to render nothing at all. I'm so confused about when and where to use quotation marks when using PHP within HTML markup!
Any ideas how I can make this work?
Try bloginfo ;
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_bloginfo('stylesheet_directory') . '/assets/img/article-overlay-1.jpg'; }?>
Cheers!
There was a problem with trying to echo the Wordpress function and append the string simultaneously. Fixed it by doing:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { $uri = get_template_directory_uri(); echo $uri . '/assets/img/article-overlay-1.jpg'; }?>
">

Href working on one page but not on another?

This href I have on a page is included in a cycle 2 slider so a caption under an image. BUT it doesn't work when clicking and it opening a new tab, it only works when you right click and then click "go to ....". Does anyone know why?
The plugin on wordpress: http://www.advancedcustomfields.com/
Code:
<a class="alt-caption" href="http://<?php the_field('url_site'); ?>"><?php the_field('url_site'); ?></a>
<div class="paginawrap no_overflow">
<div class="wraptest">
<div class="cycle-slideshow"
data-cycle-fx="carousel"
data-cycle-speed="500"
data-cycle-delay=5000
data-cycle-next=" > img"
data-cycle-caption=".alt-caption"
data-cycle-caption-template="{{alt}}"
data-cycle-carousel-fluid=true
data-allow-wrap=false
>
<?php $args = array(
'post_type' => 'project',
'posts_per_page' => 10
);
$query = new WP_Query( $args );
if ( $query->have_posts()) :
while ( $query->have_posts()) : $query->the_post();
the_post_thumbnail('home');
endwhile; wp_reset_postdata();
endif; ?>
<div class="alt-caption"></div>
</div>
<div class="archief1">
</div>
</div>
</div>
Your href is empty. Let's have a look at your source code:
<a class="alt-caption external" href="http://">www.aimassociates.nl</a>
The href attribute is looking at "http://".
Maybe you must add an echo?
<?php echo the_field('url_site'); ?>
It's weird that the same PHP code returns different result, have you copy/paste the code just as is in your files?
Why don't you try this?
<?php $site_url = the_field('url_site'); ?>
<a class="alt-caption" href="http://<?php echo $site_url; ?>"><?php echo $site_url; ?></a>
You use php to load the link. Why not use php the entire way for that line of code? Something like this:
echo('<a class="alt-caption" href="' . $the_field . '" target="_blank"><' . $the_field . '></a>');
Also make sure that your homepage correctly reads the php data.

Wordpress category page not returning array correctly

I am editing my category page. Using some custom fields I am defining an image. For each post within a category I want to add this custom image to an array which I am turning into a gallery of images. I'm using the below code, but for some reason when it comes to imploding the array all I get back is one image (which corresponds to the last post that's loaded in). I'm sure there is probably just something I've put in the wrong place but I just can't figure it out. Any help would be much appreciated.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$gallery_images = get_custom_field('catImage:to_array');
$thumbs_html = array();
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$description = get_custom_field('catIntro:do_shortcode'); //get pages introductory text
$caption = get_the_title(); //get page title
$button_html .= '<div id="description-button-' . $gallery_images_count . '" class="show-description-button">Show Caption</div>';
$description_html .= '<div id="description-' . $gallery_images_count . '" class="photoDescription" style="display:none;">' . $description . '</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" class="photoCaption" style="display:none;">' . $caption . '</div>';
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
$thumbs_html[] = '<div class="indvlThumbCont"><img class="thumbImg" src="' . $thumb_img[0] .'"></div>';
$gallery_images_count++;
}//end forEach
//calculate the width of the thumbar
$widthPerImg = 157;
$thumbBarWidth = $gallery_images_count * $widthPerImg;
print $gallery_images_count;
?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<div id="thumbsBar">
<div id="left" class="scrollCtrl left desktopOnly"><span></span></div>
<div class="toHideScrollBar" id="toHideScrollBar">
<div class="moveCanvas" style="width:<?php echo $thumbBarWidth; ?>px;">
<?php echo implode("\n", $thumbs_html); ?>
</div>
</div>
<div id="right" class="scrollCtrl right desktopOnly"><span></span></div>
<span id="total_number_in_gallery " class="<?php echo $gallery_images_count; ?>"></span>
</div>
If you are using a theme like TwentyTwelve (which by default only displays one post on the category page) then that's where the issue is. You'll solve this by (if you are fine with modifying the main loop), adding this just before your code:
query_posts('showposts=y&cat=x');

Modify a piece of PHP code

I have this:
<?php echo $this->htmlLink($this->viewer()->getHref(), $this->itemPhoto($this->viewer(), 'thumb.icon')); ?>
That generates an HTML code like:
<a href="http://www.domain.com/john">
<img src="http://www.domain.com/thumb_0205.jpg" alt="" class="thumb_icon item_photo_user thumb_icon">
</a>
Now, what I am trying to do, is to add:
<?php echo $this->viewer()->getTitle(); ?> //This will generate the member's name, like "John Doe"
to the code above, to generate an HTML code like:
<a href="http://www.domain.com/john">
<img src="http://www.domain.com/thumb_0205.jpg" alt="" class="thumb_icon item_photo_user thumb_icon">
<span>John Doe</span>
</a>
Anyway I can do that?
Thanks
This ought to work:
<?php echo $this->htmlLink(
$this->viewer()->getHref(),
$this->itemPhoto($this->viewer(), 'thumb.icon') . '<span>' . $this->viewer()->getTitle() . '</span>'
); ?>
Guessing, this should work:
<?php echo $this->htmlLink($this->viewer()->getHref(), $this->itemPhoto($this->viewer(), 'thumb.icon').'<span>'. $this->viewer()->getTitle().'</span>'); ?>
Just append the extra string to the second argument of htmlLink.
HtmlLink($href, $text, $title = "", array $attribs = array());

Categories