4 Replies - 323 Views - Last Post: 30 January 2013 - 02:03 PM Rate Topic: -----

#1 Eludiac  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 12-December 11

Show/hide badge on a loop item based on input field contents

Posted 30 January 2013 - 01:16 PM

Sorry in advance for my crappy terminology. I'm not hip with the proper php lingo so please bear with me.

So i have this php file called single-portfolio.php. In that file I have some code that makes certain things appear if there's content entered into the admin side of a portfolio item. In this case, whenever someone has entered data into the case-study field in the admin side, the div shows up on the page:
<?php if( (types_render_field( "case-study")) != "" ) { ?>
		<div class="right-details">
		
			<div class="badge badge-3" title="Case Study"></div>
			<div class="right-content"><?php echo(types_render_field("case-study", array("alt"=>"Case Study", "width"=>"300","height"=>"200","proportional"=>"true"))); ?></div>
		</div>
		<?php } ?>


That works perfectly fine on single-portfolio.php page. However, im also working on a file called shortcodes.php which deals with all of the theme's shortcodes, including the portfolio loop. I've found where to add my badges into the php and they all show up fine using this code:

$output .= '<div class="badgecontainer"><div class="port-badge pbadge1"></div><div class="port-badge pbadge2"></div><div class="port-badge pbadge3"></div></div><img src="' . $post_thumbnail_img[0] . '" alt="' . $post_thumbnail_data['alt'] . '" title="' . $post_thumbnail_data['title'] . '" class="entry-image ' . $post_thumbnail_data['class'] . '">';


So with my novice php skills i tried slapping some if statements into that code to see if i could get pbadge1 to only show if the case-study field was filled, but the code i put in completely breaks the page. What i tried is highlighted.

$output .= '<div class="badgecontainer">' .  if( (types_render_field( "case-study")) != "" ) { . '<div class="port-badge pbadge1"></div>' . } . '<div class="port-badge pbadge2"></div><div class="port-badge pbadge3"></div></div><img src="' . $post_thumbnail_img[0] . '" alt="' . $post_thumbnail_data['alt'] . '" title="' . $post_thumbnail_data['title'] . '" class="entry-image ' . $post_thumbnail_data['class'] . '">';


If anyone could give me any insight as to why this is happening or what i could do to fix it i would be so happy i would faint! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Show/hide badge on a loop item based on input field contents

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Show/hide badge on a loop item based on input field contents

Posted 30 January 2013 - 01:27 PM

You can't just splice in conditional flow logic like that. What you can do however is run the conditional flow statements before, figure out the answer, put the answer into a variable and use the variable in the string...

$badge1 = "";

if (types_render_field("case-study") != "") {
    $badge1 = '<div class="port-badge pbadge1"></div>';
}

// Now put badge1 into the string
$output .= '<div class="badgecontainer">' .  $badge1 . '<div class="port-badge pbadge2"></div><div class="port-badge pbadge3"></div></div><img src="' . $post_thumbnail_img[0] . '" alt="' . $post_thumbnail_data['alt'] . '" title="' . $post_thumbnail_data['title'] . '" class="entry-image ' . $post_thumbnail_data['class'] . '">';



This of course assumes that types_render_field is going to do the right thing and return an empty string if it is not set. But you get the idea.

:)

P.S. For those who probably will say it, yes you could use a ternary operator here but I figured I would show them the explicit way so they get the process.

This post has been edited by Martyr2: 30 January 2013 - 01:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 Eludiac  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 12-December 11

Re: Show/hide badge on a loop item based on input field contents

Posted 30 January 2013 - 01:43 PM

Ohhhhh! I think i get what you're saying! :D

Would it matter where i set up the first 5 lines of code you wrote? Should I put them into the same shortcodes.php file right above the string or is there a better place?

Sorry, php is still really new to me and what i know i've self-taught haha.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Show/hide badge on a loop item based on input field contents

Posted 30 January 2013 - 01:46 PM

You can put that if statement and such right above the string you are about to use it in. Just like I have there. Ideally in software you should always try to define a variable just before the place where it is first used. :)
Was This Post Helpful? 1
  • +
  • -

#5 Eludiac  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 12-December 11

Re: Show/hide badge on a loop item based on input field contents

Posted 30 January 2013 - 02:03 PM

OH MAH GAWD. Seriously... You are a savior. I actually learned a lot about php from your reply! :) If i could leap through your screen i would give you the biggest hug ever, I totally would! (at the expense of possibly freaking you way out because some dude just jumped through your screen.)

Again, thank you SO MUCH!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1