Hiding a when custom field is empty

Hi Guys,

Video site questions continued... I need to hide the video embed <div> when there is no video url to embed...

I am using CustomPress custom fields...

  • Jack Kitterhing

    Hi there @aircooled76

    I hope you are well today and thank you for your question.

    You want to completely hide the div and all content, if there is no content to be inserted? In that case your going to want to add a else statement to that above code.

    Some possibly handy links for you to take a look over:

    http://www.w3schools.com/php/php_if_else.asp

    http://php.net/manual/en/control-structures.else.php

    http://www.dynamicdrive.com/forums/showthread.php?33611-display-none-in-PHP/page2&s=83c9eb749dcc0b36c71097c383a24e76

    Thank you for being a WPMU DEV member!

    Kind Regards
    Jack,

  • aircooled76
    <?php
    /*
    
    Template Name: Single Video Page
    
     */
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    		<?php
    if ($ct_Video_URL_text_a90a = "")
    {
    <div id="youtube_video" style="display:block;width:768px;height:432px;margin-left: 15%; box-shadow: 0px 0px 15px #888888;margin-bottom: 3rem;">
    <iframe width="768" height="432" src="http://www.youtube.com/embed/<?php echo do_shortcode('[ct id="_ct_text_51755ecebb9f7" property="title | description | value"]'); ?>" frameborder="0" allowfullscreen></iframe>
    </div>
    } else {
    		<div id="video">
    			<script src="http://ultitv.s3.amazonaws.com/flowplayer-3.2.12.min.js"></script>
    <p>
    <a href="http://ultitv.s3.amazonaws.com/<?php echo do_shortcode('[ct id="ct_Video_URL_text_a90a" property="title | description | value"]'); ?>_HD.mp4"
        style="display:block;width:768px;height:432px;margin-left: 15%;<?php if (has_post_thumbnail( $post->ID ) ): ?><?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>background-image: url('<?php echo $image[0]; ?>');background-size:768px;<?php endif; ?>box-shadow: 0px 0px 15px #888888;margin-bottom: 3rem;"
        id="player">
    		<img onmouseout="this.src='http://ultitv.s3.amazonaws.com/playbutton768x432_60.png'" onmouseover="this.src='http://ultitv.s3.amazonaws.com/playbutton768x432_100.png'" src="http://ultitv.s3.amazonaws.com/playbutton768x432_60.png">
    	</a>
    <!-- this script block will install Flowplayer inside previous A tag -->
    <script>
    $f("player", "http://ultitv.s3.amazonaws.com/flowplayer.commercial-3.2.16.swf", {
    	key: '#$9576cb7c2931e590d4c',
    	    clip: {
            provider: 'rtmp',
            autoPlay: true,
            urlResolvers: 'brselect',
            // preserve aspect ratios
            scaling: 'fit',
            bitrates: [
    
                // use the 800 kbps item by default, the SD item, marked with sd: true
                { url: "mp4:<?php echo do_shortcode('[ct id="ct_Video_URL_text_a90a" property="title | description | value"]'); ?>_SD", bitrate: 800, sd: true, isDefault: true },
    
                // this is the HD item, marked with hd: true
                { url: "mp4:<?php echo do_shortcode('[ct id="ct_Video_URL_text_a90a" property="title | description | value"]'); ?>_HD", bitrate: 2000, hd: true }
            ]
        },
    	plugins: {
            brselect: {
                url: "http://ultitv.s3.amazonaws.com/flowplayer.bitrateselect-3.2.13.swf",
    
                // comment this out in production
                //onStreamSwitch: function (newItem) {
                 //  $f().getPlugin('content').setHtml("Switched to: " + newItem.streamName);
               // }
            },
    		        slowmotion: {
                url: "http://ultitv.s3.amazonaws.com/flowplayer.slowmotion-3.2.9.swf",
                serverType: "fms"
            },
            rtmp: {
                url: "http://ultitv.s3.amazonaws.com/flowplayer.rtmp-3.2.11.swf",
                netConnectionUrl: 'rtmp://s6p4wfktqfysx.cloudfront.net/cfx/st'
            },
    		gatracker: {
                url: "http://ultitv.s3.amazonaws.com/flowplayer.analytics-3.2.8.swf",
    
                // track all possible events. By default only Start and Stop
                // are tracked with their corresponding playhead time.
                events: {
                    all: true
                },
                debug: false,
                accountId: "UA-33679811-1" // your Google Analytics id here
            },
    		// cloudfront: {
            //    url: "https://ultitv.s3.amazonaws.com/flowplayer.cloudfrontsignedurl.bundle-ulti-1.0.swf"
            //},
           // content: {
           //     url: "http://releases.flowplayer.org/swf/flowplayer.content-3.2.8.swf",
           //     top: 0, left: 0, width: 400, height: 150,
           //     backgroundColor: 'transparent', backgroundGradient: 'none', border: 0,
            //    textDecoration: 'outline',
             //   style: {
             //       body: {
             //           fontSize: 14,
              //          fontFamily: 'Arial',
              //          textAlign: 'center',
              //          color: '#ffffff'
               //     }
              //  }
           // }
    	}
    }).ipad();
    </script>
    </div>
    }
    ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
    		<div class="featured-post">
    			<?php _e( 'Featured post', 'twentytwelve' ); ?>
    		</div>
    		<?php endif; ?>
    		<header class="entry-header">
    			<?php if ( is_single() ) : ?>
    			<h1 class="entry-title"><?php the_title(); ?></h1>
    			<?php else : ?>
    			<h1 class="entry-title">
    				<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    			</h1>
    			<?php endif; // is_single() ?>
    			<?php if ( comments_open() ) : ?>
    				<div class="comments-link">
    					<?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentytwelve' ) . '</span>', __( '1 Reply', 'twentytwelve' ), __( '% Replies', 'twentytwelve' ) ); ?>
    				</div><!-- .comments-link -->
    			<?php endif; // comments_open() ?>
    		</header><!-- .entry-header -->
    
    		<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    
    		<footer class="entry-meta">
    			<?php twentytwelve_entry_meta(); ?>
    			<?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?>
    			<?php if ( is_singular() && get_the_author_meta( 'description' ) && is_multi_author() ) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries. ?>
    				<div class="author-info">
    					<div class="author-avatar">
    						<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentytwelve_author_bio_avatar_size', 68 ) ); ?>
    					</div><!-- .author-avatar -->
    					<div class="author-description">
    						<h2><?php printf( __( 'About %s', 'twentytwelve' ), get_the_author() ); ?></h2>
    						<p><?php the_author_meta( 'description' ); ?></p>
    						<div class="author-link">
    							<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author">
    								<?php printf( __( 'View all posts by %s <span class="meta-nav">&rarr;</span>', 'twentytwelve' ), get_the_author() ); ?>
    							</a>
    						</div><!-- .author-link	-->
    					</div><!-- .author-description -->
    				</div><!-- .author-info -->
    			<?php endif; ?>
    		</footer><!-- .entry-meta -->
    	</article><!-- #post -->
    
    				<nav class="nav-single">
    					<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
    					<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
    					<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
    				</nav><!-- .nav-single -->
    
    				<?php comments_template( '', true ); ?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
  • Imperative Ideas

    Ok. I don't know CustomPress off the top of my head but you should be able to test for a variable value using:

    <?php
        if (isset($variable)) {
           echo "Variable is set";
        }
    ?>

    So if your variable is "$ct_Video_URL_text_a90a" (wow, that's complex) then...

    <?php
        if(isset($ct_Video_URL_text_a90a)) {
            // Do Stuff Here
        } else {
            // Do something else
        }
    ?>

    isset() in PHP is the equivalent of not null (!null).

  • aircooled76

    OK... I added in the test code and it does not echo the "Variable is set"...

    I also did the following code and got the response "Do something else" (same as variable not set)

    <?php
        if(isset($ct_Video_URL_text_a90a)) {
            echo "Do Stuff Here";
        } else {
            echo "Do something else";
        }
    ?>

    So the ct_Video_URL_text_a90a must not be the correct reference.... It is however working in the embed code for the Custom field
    <?php echo do_shortcode('[ct id="ct_Video_URL_text_a90a" property="title | description | value"]'); ?>
    correctly

    Any suggestions... this is holding me up quite a bit ATM...

  • Imperative Ideas

    This is a common mistake that people make when they don't understand how programming works. Just because it looks like it should be simple, that doesn't mean it will be the case.

    A shortcode is essentially a PHP function. You are feeding it specific variables and it is being executed. Just because you don't see the engine under the hood doesn't mean it runs by magic.

    In this case, what you want to do is a bit scary if you don't know any PHP but it's fairly elementary as far as this sort of thing goes. All we need to know is the name of the custom field you are checking against. We should be able to pull that out of the CustomPress configuration if you can post a screenshot of the full page.

  • Imperative Ideas

    Also, no, there is not an easier way to achieve this. You are asking how to write a conditional statement based on a field that your only interface with, thus far, is via shortcode.

    A shortcode is like buying a T-Shirt in "small, medium, large, x-large" and "black, white, gray, red, blue"

    What you're after is more like sewing it yourself but it's not as complex as farming and processing your own cotton/dye.

  • aircooled76

    WOO HOO!!!!! It is working!

    Sweet...

    Thank you so much for your help.... It is all a little bit like magic :stuck_out_tongue:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $videoyn = get_post_meta($postid, 'ct_Video_URL_text_a90a', true);
    if (isset($videoyn)) {
    echo "do something";
    } else {
    echo "do something else";}
    ?>

    where $videoyn is my made up variable to use in the next section of code.

    and

    'ct_Video_URL_text_a90a' is my CustomPress short code reference

  • aircooled76

    Ok... The conditional statement is working fine, but when I go to "echo" my html code into the php doc.... I am having some trouble....

    My flowplayer video code has many single quote marks ' and php shortcode actions....

    so if i echo ' and use the single quotes to wrap the div tag..... the ' s in the code end the echo... and I have to end and start the echo before and after each php function....

    is there a better way to call the HTML code for the video player?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.