Skip Navigation

[Resolved] php conditional

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 1 year, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2623467

I have a function that creates a div and uses an image from a toolset field as a background. Id like the function not to run on pages where the custom field is empty. My function below works great except when i add the conditional to it- then it doesnt appear to run under any circumstances.

php isnt my strong card- Do I have to test for wpcf-header-background-image set correctly?

// Add div.wrap outsideof header and give it a background image if one exixts in the field header-backround-image

$field = get_post_meta(get_the_ID(), 'wpcf-header-background-image');

if (!empty($field)){

function sh_before_header_wrap() {

$url = do_shortcode( "[wpv-post-field name='wpcf-header-background-image']" );

echo "<div class='home-header_wrap' style='background-image: url( $url )'>";
}

add_action( 'genesis_before_header', 'sh_before_header_wrap' );

function sh_after_header_wrap() {
echo '</div>
<!-- end .header_wrap -->';
}
add_action( 'genesis_after_header', 'sh_after_header_wrap' );
}

#2623529

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where I can see the header image is available as well as the URL of the page where header image is not available and share admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2623687

Minesh thanks the site is on my local host right now. I'll probably have it provisioned on a temporary domain next week sometime and could share access then. If you have any other thoughts in the meanwhile let me know

Best

Steve

#2624011

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Its bettor to look at your code and actual setup that will help to see what exactly the issue and where.

#2624711

Minesh can you reset the private reply Ive moved the site to a temp domain so can provide access now

#2624715

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please share details of problem URL and to what file you have added the code you shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2624945

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure why you created and added the code to sailing-heals.php file inside the plugins folder. However it was pure custom code which is beyond the scope of our support policy but as its was something bit easily done using the genesis hooks only.

I've adjusted and added the following code to "Custom Code" section offered by Toolset with the code snippet namely "toolset-custom-code":
=> hidden link

// Add div.wrap outsideof header and  give it  a  background image if one  exixts in the  field  header-backround-image

function sh_before_header_wrap() {
  global $post;

  $field = get_post_meta($post->ID,'wpcf-header-background-image',true);

  $url = $field;
  if(!empty($url)) {
	echo "<div class='home-header_wrap' style='background-image: url(".$url.")'>";
  }

}
add_action( 'genesis_before_header', 'sh_before_header_wrap' );


function sh_after_header_wrap() {
global $post;

  $field = get_post_meta($post->ID,'wpcf-header-background-image',true);

  $url = $field;
  if(!empty($url)) {
  	echo '</div><!-- end .header_wrap -->'; 
  }
}
add_action( 'genesis_after_header', 'sh_after_header_wrap' );

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

Now, on home page I can see the header image:
- hidden link

But on other pages the extra space is gone and header image is not shown as its not configured:
- hidden link