I have this PHP code and I use this same at multiple places on the same pages, So I think it will be better if it will be a functions and I use it by calling it. <?php $text1Val = get_post_custom_values("thumb"); $text2Val = get_post_custom_values("video"); if($text1Val) { echo $text1Val[0]; ?> <?php } else if($text2Val) { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&h=106&w=196&zc=1 <?php } else { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&h=106&w=196&zc=1 <?php } ?> PHP:
this is the example how you can do function functionName() { code to be executed; } Code (markup): PHP function guidelines: Give the function a name that reflects what the function does The function name can start with a letter or underscore (not a number)
Thanks for reply filegrasper, Do you mean like this: function get_video_thumb() { <?php $text1Val = get_post_custom_values("thumb"); $text2Val = get_post_custom_values("video"); if($text1Val) { echo $text1Val[0]; ?> <?php } else if($text2Val) { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&h=106&w=196&zc=1 <?php } else { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&h=106&w=196&zc=1 <?php } ?> } PHP:
like this <html> <body> <?php function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> </body> </html> Code (markup): Output : My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.
<?php function my_vid(){ $text1Val = get_post_custom_values("thumb"); $text2Val = get_post_custom_values("video"); if($text1Val) { echo $text1Val[0]; ?> <?php } else if($text2Val) { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&h=106&w=196&zc=1 <?php } else { ?> <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&h=106&w=196&zc=1 <?php } } ?> Put this function in your functions.php file and invoke/call it from anywhere inside your post loop. I am sure you're working with WordPress.
Thanks Cssknight for the function, You are absolutely right, I am on wordpress. Can you please also tell me how to call that code, I don't know that also.
Hello friend, Just call this function using the name and parameters, Example: my_vid(var1,var2); That is enough... Thanks