wp_head wordpress function doesn't accept style from other function - php

Trying to add styling code for background image using WordPress wp_head hook but it doesn't work. Here is the code: (when code is used inside f1 function I get "error" message but outside f1 function code works fine and gets url for background image correctly, so problem is with wp_head. Any thoughts?
function f1() {
?> <style>
.boxes{
background-image:
url(
<?php
$images = rwmb_meta( 'f3' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo $image['url'];
}
}
else {
echo 'error';
}
?>
);
}</style>
<?php
}
add_action( 'wp_head', 'f1');

Try passing in the post_id to your rwmb_meta() call
function f1() { ?>
<style>
.boxes{
background-image:
url(
<?php
global $post;
$images = rwmb_meta( 'f3', array(), $post->ID );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo $image['url'];
}
}
else {
echo 'error';
}
?>
);
}</style>
<?php
}
add_action( 'wp_head', 'f1');

Related

How to shorten the usage of one image source retrieved via php codes

newbie here. I'm using the following code to extract the secondary image source of my wordpress posts, but I think I could shorten it someway...
Here is the code I'm currently using inside img src=""
<?php $images = get_attached_media('image'); $featured_image_id = get_post_thumbnail_id(); if ( has_post_thumbnail() ) { unset($images[ $featured_image_id ] ); } $harukunt = wp_get_attachment_image_src( key($images),'large'); echo '' . $harukunt[0] . ''; ;?>
Maybe I could simplify this by defining some values in the header.php file, so then I can call the image in a shorter way in the posts?
<?php
$images = get_attached_media('image');
$featured_image_id = get_post_thumbnail_id();
if ( has_post_thumbnail()
)
{ unset($images[ $featured_image_id ] );
}
$harukunt = wp_get_attachment_image_src( key($images),'large');
echo '' . $harukunt[0] . '';
;
?>
And then call the image on posts by simply using img src="<?php harukunt("$post->ID"); ?>" or something. But it isnt working this way, due to my poor knownledge.
Can anybody please help me?
You can create a function and add it to your functions.php file like this:
function get_secondary_img($post_id, $print = true ){
if(has_post_thumbnail($post_id)) {
$attachments = get_attached_media('image',$post_id);
if(count($attachments) < 2){
$attachment_id = get_post_thumbnail_id($post_id);
} else {
foreach($attachments as $key => $attachment){
if($key !== get_post_thumbnail_id($post_id)){
$attachment_id = $key;
break;
}
}
}
if($attachment_id){
$attachment = wp_get_attachment_image_src($attachment_id,'large');
$src = $attachment[0];
$width = $attachment[1];
$height = $attachment[2];
$alt = get_post_meta($attachment_id,'_wp_attachment_image_alt',true);
if($print == true){
echo '<img width="'.$width.'" height="'.$height.'" src="'.$src.'" alt="'.$alt.'" />';
} else {
return $src;
}
}
}
}
Then if you want to output the html for the image you can just do
<?php echo get_secondary_img( $post_id ); ?>
Or if you want to just return the src attribute, you can just do
$yourvariable = get_secondary_img( $post_id, false );

How to pass value from WordPress functions.php to any other php file e.g. page template

I am trying to pass a value from functions file to a page template but not sure how to do it
I usually use get template part but since its a function file,
functions.php
function my_ajax_request() {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'my-ajax-script' ) ) {
die( 'Nonce value cannot be verified.' );
}
if ( isset($_REQUEST) ) {
$color = $_REQUEST['color'];
if ( $color == 'Black' ) {
$bgcolor = '#000'; // how to pass value to other file/page?
$txtcolor ='#fff'; // how to pass value to other file/page?
}
}
die();
}
add_action( 'wp_ajax_my_ajax_request', 'my_ajax_request' );
add_action( 'wp_ajax_nopriv_my_ajax_request', 'my_ajax_request' );
custompage.php
<div style="background:<?php $color; ?>;color:<?php $txtcolor; ?>;">
<p>Hello World</p>
</div>

If else statement not working

I am using Meta Box Plugin
I have two options in Post Edit. One to add a Logo and one to add a Title. On the front end (in my template) I want to show either the Logo or the Title, but not both.
I added this code to my template however it is not working:
<?php if( rwmb_meta('restauranttheme_hero_small_logo', false, 'url') !== '' ) { ?>
<?php $images = rwmb_meta( 'restauranttheme_hero_small_logo', 'type=image_advanced&size=full' );
foreach ( $images as $image ) {
echo "<img src='{$image['url']}'>"; } ?>
<?php } else { ?>
<h1 class="mini-title"><?php echo rwmb_meta('restauranttheme_hero_title' ); ?></h1>
<?php } ?>
I also tried:
<?php if( '' != rwmb_meta('restauranttheme_hero_small_logo' ) ) { ?>
<?php $images = rwmb_meta( 'restauranttheme_hero_small_logo', 'type=image_advanced&size=full' );
foreach ( $images as $image ) {
echo "<img src='{$image['url']}'>"; } ?>
<?php } else { ?>
<h1 class="mini-logo"><?php echo rwmb_meta('restauranttheme_hero_title' ); ?></h1>
<?php } ?>
I guess the error is in my if else statement.
Can anyone help?
Thanks so much

Wordpress: PHP echo inside echo?

I'm using the below code in my header.php file to have a different div depending on the page:
<?php
if ( is_singular( 'movie' ) ) {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
} elseif (has_post_thumbnail( $post->ID ) ) {
echo "<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?> <div class='heroImage' style='background-image:url(<?php echo $image[0]; ?>);'>";
} else {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
}
?>
The conditions for if ( is_singular( 'movie' ) ) { and else { work fine, but the one that checks if there's a featured image doesn't. The code in there is to get the URL of the featured image so it can insert it as the div's background image. I think the issue is the echo inside an echo?
When I look at the code it outputs in my browser, it looks like this:
<div class="heroImage" style="background-image:url(<?php echo ; ?>);">
I've tried and looked online to help fix it, but can't seem to find a solution. Appreciate any help.
PHP won't execute within a string. Simply assign the $image variable before echo-ing the string
elseif (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), 'full');
echo '<div class="heroImage" style="background-image:url(', $image[0], ')">';
}
Try assigning the $image variable before echoing the string.
I.e.
<?php
if ( is_singular( 'movie' ) ) {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
} elseif (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo "<div class='heroImage' style='background-image:url" . $image[0] . ')">' ;
} else {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
}
?>

Image Caption with the_post_thumbnail in WordPress

Is there a way to display the image caption where ever available when displaying the_post_thumbnail() image in WordPress on the posts in the primary loop.
Thanks! Appreciate all the help.
Here is an easier and shorter code :
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
As of WordPress 4.6, the function the_post_thumbnail_caption() has been added to core (/wp-includes/post-thumbnail-template.php).
Using the code posted here will cause the error:
Fatal error: Cannot redeclare the_post_thumbnail_caption()
I figured it out:
/************************************************************\
* Fetch The Post Thumbnail Caption
\************************************************************/
function the_post_thumbnail_caption() {
global $post;
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
echo $thumbnail_image[0]->post_excerpt;
}
}
if(!function_exists('get_post_thumbnail_caption')) {
function get_post_thumbnail_caption($post_id = null) {
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
$thumbnail_id = get_post_thumbnail_id($post_id);
if ($thumbnail = get_post($thumbnail_id))
return $thumbnail->post_excerpt;
return '';
}
}
if(!function_exists('the_post_thumbnail_caption')) {
function the_post_thumbnail_caption($post_id = null) {
echo get_post_thumbnail_caption($post_id);
}
}
if(has_post_thumbnail()) {
the_post_thumbnail();
the_post_thumbnail_caption();
$caption = get_post_thumbnail_caption(123);
if('' == $caption)
echo '<div class="caption">'.$caption.'</div>';
}

Categories