Follow us on

Codestar Support Forum » WordPress Plugins » Codestar Framework

how to display media field data as background image

  1. Hello Sir,
    I hope you are well.

    I new in code start and still learning. I need a function like if a user uploads an image its show as background images, if it does not upload any image , so no need to showing blank CSS background-image property

    its my code for customier option

    array(
          'id'           => 'page-header-img',
          'type'         => 'media',
          'title'        => esc_html__( 'Background Image', 'textdomain' ),
          'library'      => 'image',
          'button_title' => 'Add',
          'remove_title' => 'Remove',
          'dependency' => array( 'page-title-option', '==', 'true' ),
        ),CopyCopied!

    the code used in my theme file

    <?php 
    
    	$page_header_bg = $name_customizer['page-header-img'];
    
    	$var_link = $page_header_bg['url'];
    
     if($var_link ){
         $banner_bg = 'style="background-image:url('.esc_url( $var_link ).');"';
     } else {
    	$banner_bg = "";
     }    
    
    		?>
    	<div class="page-header" <?php echo ($banner_bg);?>>CopyCopied!

    but this code does not work for me its how error

    Warning: Illegal string offset 'url' in and Notice: Uninitialized string offset: 0 in

    so please give an example of code as I use background-image also its Envato standard code for my theme

    Thank you

    Posted 2 years ago #

  2. Codestar
    Admin

    Hi,

    Thanks, I am fine, hope you too.

    Firstly, you should to check exists to url. for eg:

    <?php
    if ( ! empty( $name_customizer['page-header-img']['url'] ) ) {
      $bg_image = ' style="background-image:url('. esc_url( $name_customizer['page-header-img']['url'] ) .');"';
    } else {
      $bg_image = '';
    }
    ?>
    
    <div class="page-header"<?php echo( $bg_image ); ?>>CopyCopied!

    another way.

    <?php $bg_image = ( ! empty( $name_customizer['page-header-img']['url'] ) ) ? ' style="background-image:url('. esc_url( $name_customizer['page-header-img']['url'] ) .');"' : ''; ?>
    
    <div class="page-header"<?php echo( $bg_image ); ?>>CopyCopied!

    You need this code or ?

    Regards, Codestar

    Posted 2 years ago #
  3. Now Showing : Notice: Undefined variable: banner_bg

    Note: when I upload an image its work well but if I remove the image then back again the error

    so please make sure if a user does not upload any image its also works without any error . forget about my code style . give me a good example for background image dynamic with codestar.

    Posted 2 years ago #
  4. I like to use like but its work if I set up images else its showing error Notice: Undefined variable: load

    $new = $stok_customizer["page-header-img"] ;
    
    	if(! empty ($new['url'] )): 
    
    	$load = 'style="background-image: url(' .esc_url($new['url'] ). ');"';
    
    	 endif; ?>
    
    	<div class="page-header" <?php echo ( $load );?>>CopyCopied!

    Another way : its work well , also its work if i not use an images as i looking for but i dont like this way , i like first one . so help me to fix this issuse

    <div class="page-header"
    	<?php
    	$new = $stok_customizer["page-header-img"] ;
    
    	if(! empty ($new['url'] )): ?>
    	style="background-image: url(' <?php echo esc_url($new['url'] ) ?> ')"
    	<?php endif; ?>
    	>CopyCopied!
    Posted 2 years ago #

  5. Codestar
    Admin

    Hi,

    "Now Showing : Notice: Undefined variable: banner_bg"

    If you use my example code It's impossible. I think you missing something.

    You must check the value exists always. for eg:

    <?php
    $load = '';
    if ( ! empty( $stok_customizer["page-header-img"] ) ) {
      $new = $stok_customizer["page-header-img"] ;
      if ( ! empty( $new['url'] ) ) {
        $load = 'style="background-image: url(' .esc_url($new['url'] ). ');"';
      }
    }
    ?>
    <div class="page-header" <?php echo $load; ?>>CopyCopied!

    alternative

    <?php
    $load = '';
    if( ! empty( $stok_customizer['page-header-img']['url'] ) ) {
      $load = 'style="background-image: url(' .esc_url( $stok_customizer['page-header-img']['url'] ). ');"';
    }
    ?>
    <div class="page-header" <?php echo $load; ?>>CopyCopied!

    It's so clear.

    Regards, Codestar

    Posted 2 years ago #