Declare inline background image in functions.php - php

I'm not sure if this can be done as I cannot find anything to help me anywhere no matter how hard i try. I'm using Ajax to load to in posts on the press of the button. the posts that are loaded in are formatted by my function inside my functions.php. That all works perfetly,the issue i'm having is the background-image: property needs to be inline css so that it can pull the image from my custom field. This all works perfectly in the template file, however. no matter what I try the functions.php rejects my inline style for background image no matter what I try.
Here's my If statement in it's entirety:
if ($loop -> have_posts()) : while ($loop -> have_posts()) : $loop -> the_post();
if ($_SESSION["load_type"] == 'home')
{
$out .= '<div class="small-6 large-3 columns end thumb">
<div class="grid">
<figure class="effect-zoe">
'.get_the_post_thumbnail( get_the_ID(), $size, $attr ).'
<figcaption>
<h2>'.get_the_title().'</h2>
<hr class="light">
<p class="description">'.get_the_content().'</p>
</figcaption>
</figure>
</div>
</div>';
}
else
if ($_SESSION["load_type"] == 'category')
{
$out .= '<div class="small-6 large-6 columns end thumb under">
<div id="case">
<div class="th" id="element" >
<a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
</div>
</div>
<div class="brief">
'.get_the_title().'
<p class="suppy">Supplier</p>
<p>'.get_the_excerpt().'</p>
<a class="more-btn" href="'. get_permalink($post->ID) .'">More</a>
</div>
</div>';
}
else
if ($_SESSION["load_type"] == 'product')
{
$out .= '<div class="small-6 large-3 columns">
<div class="small-6 large-12 columns end no-pad morepro" style="background-image: url(''.the_field(product_image).'')">
<a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
</div>
<h2 class="product-load">'.get_the_title().'</h2>
</div>';
}
endwhile;
endif;
wp_reset_postdata();
die($out);
and heres the part of it that I'm having trouble with and crashes the whole site every time I try to load in a background-image:
if ($_SESSION["load_type"] == 'product')
{
$out .= '<div class="small-6 large-3 columns">
<div class="small-6 large-12 columns end no-pad morepro" style="background-image: url(' '.the_field(product-image).' ')">
<a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
</div>
<h2 class="product-load">'.get_the_title().'</h2>
</div>';
}
enter code here
Can anyone see where I'm going wrong?

Change the following
style="background-image: url(' '.the_field(product-image).' ')"
into the
style="background-image: url('.the_field("product-image").')"
This should work if:
Your "product-image" is custom defined size (if its constant use it without quotes) - or edit your question and explain what it is.
the_field() function need to "return" value and not to echo it. I am mentioning this because by wordpress codex usually use "the_something()" function will not return value, it will just display something. If you want to to return a value it is usually "get_the_something()". It is important because it will break your json object (if that is what you are using on js side)
Example: the_content() vs get_the_content(), or get_the_title() vs the_title() etc...
Quotes within background-image should be optional by this.

if ($_SESSION["load_type"] == 'product'){
$image = the_field(product_image);
$out .= '<div class="small-6 large-3 columns">
<div class="small-6 large-12 columns end no-pad morepro" style="background-image: url('"'.$image.'"')">
<a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
</div>
<h2 class="product-load">'.get_the_title().'</h2>
</div>';
}
and see if it works

Related

open images as pop up in PHP (WordPress) or scroll to image opened

Hi (sorry for my next english :))
i need help in a wordpress template oneengine in which in the portfolio images the photos are open down the timeline of the photos without an automatically scroll down, like that:
the fact could be 1) I need a code to scroll down or 2) I need a popup view here the code of the function.php:
<?php
}
}
wp_reset_query();
echo '</div>';
echo '<div id="portfolio_content">
<div class="container">
<div class="row">
<div class="port-control col-md-12">
<div class="port-content">
</div>
<a class="prev" href="#"><span class="arrow-port left"></span> '.__('Prev','oneengine').'</a>
<i class="fa fa-times"></i>
'.__('Next','oneengine').' <span class="arrow-port right"></span>
</div>
</div>
</div>
</div>';
}
function before_block( $instance ) {
extract( $instance );
echo '<div id="aq-block-'.$template_id.'-'.$number.'" class="aq-block aq-block-'.$id_base.'">';
echo '<div id="portfolio-page" class="portfolio-wrapper" style="margin:'.$margin_top.'px 0 '.$margin_bottom.'px;">';
}
function after_block( $instance ) {
extract( $instance );
echo '</div>';
echo '</div><!-- END ET-PORTFOLIO-BLOCK-->';
}
}
for all the documentation the theme is OneEngine https://www.enginethemes.com/themes/oneengine/

Can´t correctly write custom field data in custom function placed in functions.php

I need to write filter data of my custom post type in custom function placed in functions.php:
The majority are corrected, but I cannot process these lines:
<div class="cpt-studies-block-image picture">
<?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
<img class="img" src="<?php print($thumb_img[0]); ?>">
</div>
and:
<a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
?>
<?php if (get_field('button_text')):
the_field('button_text');
else : echo __('More', 'dw');?>
<?php endif;?>
</a>
Code in folder for writing Custom Post Type data:
if($query->have_posts()):
while($query->have_posts()) : $query->the_post();?>
<div class="col-md-6">
<div class="cpt-studies-block">
<div class="row">
<div class="col-md-6">
<a class="zoom-picture-hover" href="<?php the_permalink()?>">
<div class="cpt-studies-block-image picture">
<?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
<img class="img" src="<?php print($thumb_img[0]); ?>">
</div>
</a>
</div>
<div class="col-md-6">
<span><?php the_field('date')?></span>
<h3>
<a href="<?php the_permalink()?>">
<?php the_title()?>
</a>
</h3>
<p class="cpt-studies-block-text"><?php the_field('text')?></p>
<a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
?>
<?php if (get_field('button_text')):
the_field('button_text');
else : echo __('More', 'dw');?>
<?php endif;?>
</a>
</div>
</div>
</div>
</div>
<?php endwhile;
endif;
?>
Code in Custom function in functions.php:
if(have_posts($wp_query))
{
while(have_posts($wp_query))
{
the_post();
echo '<div class="col-md-6">'
.'<div class="cpt-studies-block">'.
'<div class="row">'.
'<div class="col-md-6">'
.'<a class="zoom-picture-hover" href="'.get_permalink().'">'
.'<div class="cpt-studies-block-image picture">'
.$thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien').
'<img class="img" src=".print($thumb_img[0]).">'
.'</div>'
.'</a>'
.'</div>'
.'<div class="col-md-6">'
.'<span>'.get_field('date').'</span>'
.'<h3>'
.'<a href="'.get_permalink().'">'
.get_the_title().
'</a>'
.'</h3>'
.'<p class="cpt-studies-block-text">'.get_field('text').'</p>'.
'<a href="'.get_permalink().'" class="cpt-studies-block-link link-read-more">'
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
'</a>'.
'</div>'.
'</div>'.
'</div>'.
'</div>';
}
}
Please can someone give me advice, or do I need to change method on writing these lines?
You got a syntax error:
.'<div class="cpt-studies-block-image picture">' // Add semicolon.
.$thumb_img = wp_get_attachment_image_src(
get_field('image'),'custom-crop-studien'). // Assignement can't be concatenated
// and missing parenthesis
'<img class="img" src=".print($thumb_img[0]).">' // Resume echo.
So:
// Ommited
.'<div class="cpt-studies-block-image picture">';
$thumb_img = wp_get_attachment_image_src(
get_field('image'), 'custom-crop-studien'));
echo '<img class="img" src=".print($thumb_img[0]).">'
Or better yet, use templates (as I explained in this other question of yours)

How to I will write loop for custom post type in this marqup

I've created a custom post type. But in my marqup I've to keep 2 post in one li. So I don't understand how to I write loop for this same marqup. Check my marqup below and Can any one help me ?
<li class="clearfix">
<div class="attorneys-slider">
<div class="">
<div class="single-attornys-slider">
<img src="<?php echo get_template_directory_uri(); ?>/img/Marisa-San-Filippo.jpg" alt="#">
<div class="hovertext">
<h2>Rolf D. Kruger</h2>
<p>Partner</p>
</div>
</div>
</div>
</div>
<div class="attorneys-slider">
<div class="">
<div class="single-attornys-slider">
<img src="<?php echo get_template_directory_uri(); ?>/img/Marisa-San-Filippo.jpg" alt="#">
<div class="hovertext">
<h2>Rolf D. Kruger</h2>
<p>Partner</p>
</div>
</div>
</div>
</div>
</li>
This may help you. go through the below code
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) :
the_post();
$i = $wp_query->current_post;
echo ( 0 == $i % 2 ) ? '<li class="clearfix">' : '';
?>
<div class="attorneys-slider">
<div class="">
<div class="single-attornys-slider">
<img src="<?php echo get_template_directory_uri(); ?>/img/Marisa-San-Filippo.jpg" alt="#">
<div class="hovertext">
<h2>Rolf D. Kruger</h2>
<p>Partner</p>
</div>
</div>
</div>
</div>
<?php
echo ( $wp_query->post_count == $i || 1 == $i % 2 ) ? '</li>' : '';
endwhile;
?>
<?php endif; ?>
If you are trying to display posts in batches of posts (it's not clear from your original post) then you'll need to do some manipulation inside the loop. To do so -using your example markup- you'd do something limke this:
$oddRow = '<li class="clearfix"><div class="attorneys-slider"><div class=""><div class="single-attornys-slider"><img src="' . get_template_directory_uri() . '/img/Marisa-San-Filippo.jpg" alt="#"><div class="hovertext"><h2>Rolf D. Kruger</h2><p>Partner</p></div></div></div></div>';
$evenRow = '<div class="attorneys-slider"><div class=""><div class="single-attornys-slider"><img src="' . get_template_directory_uri() . '/img/Marisa-San-Filippo.jpg" alt="#"><div class="hovertext"><h2>Rolf D. Kruger</h2><p>Partner</p></div></div></div></div></li>';
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$x++; //counter to determine if odd or even row
$output = ($x%2 == 0)? $oddRow : $evenRow;
echo $output
} // end while
} // end if
You'd need to tweak that slightly in order to dynamically display your images and other post related data. However, it should cover the bulk of what you were after and give you a base to work out the rest from.

wordpress plugin accessing post info

I am a novice to wordpress plugin development. I am trying to access the post information and based on if it is having a featured image I am creating a slide show. But the image url and post title are going out of my div tag. Below is my code.
//function to display post info
function postsinfo_collage_display($attr, $content) {
$plugins_url = plugins_url();
$html = '<div id="pscanvas" class="row">
<div class="row__inner mThumbnailScroller">';
// The Query
query_posts( array ('orderby' => 'date' ) );
// The Loop
while ( have_posts() ) : the_post();
if ( has_post_thumbnail() ) {
$html .= '<div class="tile">
<div class="tile__media">';
$html .='<a href="'.esc_url(get_permalink()).'">';
$html .='<img class="tile__img"';
$html .="src='".esc_url(the_post_thumbnail_url()). "'/>";
$html .='</a></div><div class="tile__details">
<div class="tile__title">';
$html .= the_title();
$html .='</div></div></div>';
}
endwhile;
// Reset Query
wp_reset_query();
$html .= '</div>
</div>
<script>
window.jQuery || document.write(\'<script src="minified/jquery-1.11.0.min.js"><\/script>\')
(function($){
$(window).load(function(){
});
})(jQuery);</script>';
return $html;
}
The output I am getting is:
<div class="entry-content">
http://localhost:9085/wordpress/wp-content/uploads/2016/04/call-of-duty-ghosts-20517-1366x768-1200x675.jpgpscanvashttp://localhost:9085/wordpress/wp-content/uploads/2016/04/Call-Of-Duty-Ghost-Background-For-Wallpaper-652-1200x675.jpgkwofkwc
<div id="pscanvas" class="row">
<div class="row__inner mThumbnailScroller ">
<div id="mTS_1" class="mTSWrapper mTS_horizontal">
<div class="mTSAutoContainer mTSContainer" ">
<div class="tile">
<div class="tile__media">
<a href="http://localhost:9085/wordpress/2016/09/04/pscanvas/">
<img class="tile__img" src=""></a>
</div><div class="tile__details">
<div class="tile__title"></div></div></div>
<div class="tile"><div class="tile__media">
<a href="http://localhost:9085/wordpress/2016/05/03/kwofkwc/">
<img class="tile__img" src=""></a></div><div class="tile__details">
<div class="tile__title"></div></div></div>
</div>
</div>
</div>
</div>
<script>
window.jQuery || document.write('<script src="minified/jquery-1.11.0.min.js"><\/script>')
(function($){
$(window).load(function(){
});
})(jQuery);</script>
<strong>Album Rating:</strong> <div class="genericon genericon-star"></div><br> </div>
I can see the image url should be inside the img tag but its coming out of the entire div tag.
You're using functions that directly output the image and title rather than appending it to your $html variable. You need to switch to functions that return a value instead.
Change the image from:
$html .="src='".esc_url(the_post_thumbnail_url()). "'/>";
To:
$html .= "src='" . esc_url( get_the_post_thumbnail_url() ) . "'/>";
And the title from:
$html .= the_title();
To:
$html .= get_the_title();
Docs:
https://developer.wordpress.org/reference/functions/get_the_title/
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Try
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
<img src="<?php echo $url; ?>"/>

Translate Wordpress block with <?php _e(

I have made some block custom in my wordpress theme. I did have to edit the shortcode.php file to add a price table. The thing I just realize is that I need those words ( From , /DAY , Book ) being translated though the .po file of my theme. So I have tried to add this line to replace the word ( "From" for example ). But I have a error code, I guess I don't have a good syntax here but can't find the right one. Can you Help?
Here is the original code
/*CUSTOM PRICE*/
}elseif($type === 'content_block_custom_price'){
$result = '<div class="featurecustom text-center">';
$result .= '<div class="thumbnail">
<img src="'.AUTORENT_IMAGE.''.$img.'" alt="">
</div>
<p class="titlecustom">'.$title.'</p>
<p class="fromprice"> From</p>
<span class="pricenumber">'.$pricetype.'<sup>€</sup></span><span class="fromprice">/ DAY</span>
<hr>
<p class="customdescription">'.$des.'</p>
<div class="bookingbutton" style="vertical-align:middle"><span>BOOK </span></div>
';
$result .= '</div>';
return $result;
/* CUSTOM PRICE*/
Here is the bad code i try to have my word translate
/*CUSTOM PRICE*/
}elseif($type === 'content_block_custom_price'){
$result = '<div class="featurecustom text-center">';
$result .= '<div class="thumbnail">
<img src="'.AUTORENT_IMAGE.''.$img.'" alt="">
</div>
<p class="titlecustom">'.$title.'</p>
<p class="fromprice"> <?php _e('From','themename'); ?></p>
<span class="pricenumber">'.$pricetype.'<sup>€</sup></span><span class="fromprice"><?php _e('/DAY','themename'); ?></span>
<hr>
<p class="customdescription">'.$des.'</p>
<div class="bookingbutton" style="vertical-align:middle"><span><?php _e('Book','themename'); ?> </span></div>
';
$result .= '</div>';
return $result;
/* CUSTOM PRICE*/
Post solved problem question : One I have new entries such as "Book" " FROM" and "/DAY", I guess I will have to creat them into the poedit file? Or will they be automatically added?
Thanks :)
Here is the good code (just inserting the gettex like this '. __("From","theme name").':
/*CUSTOM PRICE*/
}elseif($type === 'content_block_custom_price'){
$result = '<div class="featurecustom text-center">';
$result .= '<div class="thumbnail">
<img src="'.AUTORENT_IMAGE.''.$img.'" alt="">
</div>
<p class="titlecustom">'.$title.'</p>
<p class="fromprice"> '. __("From","theme name").'</p>
<span class="pricenumber">'.$pricetype.'<sup>€</sup></span><span class="from price">'. __("/DAY","theme name") .'</span>
<hr>
<p class="customdescription">'.$des.'</p>
<div class="bookingbutton" style="vertical-align:middle"><span>'. __("Book","themename"); .' </span></div>
';
$result .= '</div>';
return $result;
/* CUSTOM PRICE*/
For translation of themes & plugins there is a very effective and easy free plugin: Loco Translate
This plugin is going to scan your theme for new items to translate…

Categories