bluefrog Posted December 3, 2015 Share Posted December 3, 2015 (edited) I have no idea how to properly describe this topic as I'm fairly new to php, but here is a description of what I am trying to do: This is firstly for a wordpress functions file Ok what I am trying to achieve is have a small piece of code (in this case a wordpress shortcode) read a number in plain text: e.g. [myshortcode num=43] And replace the shortcode input (43) with an image named the same (43.jpg (http://www.mysite/path/43.jpg")) This is for a lottery style site where I want to put my picks for the day so I have 49 numbers in total. And each number is currently a full line of html code - I really need to make input faster lol. I have tried 'if and' I have tried 'if or' but I'm struggling. Anybody know how to take that and turn into a function? I know how to get it into wordpress and make it a shortcode, I just need help with the actual function side of things, my code was incredibly long and didnt work lol Thank you all in advance Edited December 3, 2015 by bluefrog Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2015 Share Posted December 3, 2015 Basically, instead of $num = 43; echo $num; // --> 43 you would use $num = 43; echo "<img src='/path/$num.jpg'>"; // --> outputs image 43.jpg Quote Link to comment Share on other sites More sharing options...
bluefrog Posted December 4, 2015 Author Share Posted December 4, 2015 Sorry you'll have to bare with me, I'm not great with php as I said. How do I get the number into a variable? The number will literally just be written into a shortcode (which will be the same for each number) with the only difference being the number in the shortcode: [myshortcode num=40] [myshortcode num=13] [myshortcode num=6] for example. Thanks in advance Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 4, 2015 Share Posted December 4, 2015 You use add_shortcode() in your functions file, then create the function that will process the and include the image. Depending on how you're trying to get the image, you may also have to look into get_attachment_thumb_url(). Quote Link to comment Share on other sites More sharing options...
bluefrog Posted December 5, 2015 Author Share Posted December 5, 2015 No I understand how to create and use a shortcode, I've done a few, I'm struggling with the function element. Barand suggests a good solution, however I'm a little confused as to how to correctly write the number from the shortcode to the function. Would something like this work: function ball_number($atts,$content,$tag){ $values = shortcode_atts(array( 'ball' => ' ' ),$atts); $output = ''; if($values['ball'] == '1'){ $output = 'image code for ball number 1'; } else if($values['ball'] == '2'){ $output = 'image code for ball number 2'; } else if($values['ball'] == '3'){ $output = 'image code for ball number 3'; } else{ $output = ' '; } return $output; } add_shortcode('ball','ball_number'); Thanks for all of your input so far, it's appreciated Quote Link to comment Share on other sites More sharing options...
bluefrog Posted December 5, 2015 Author Share Posted December 5, 2015 Actually that only returns the 'else' statement ;/ Quote Link to comment Share on other sites More sharing options...
Solution bluefrog Posted December 5, 2015 Author Solution Share Posted December 5, 2015 Ah I got it. Is there a shorter version than this: function ball_callback($atts,$content,$tag){ //collect values, combining passed in values and defaults $values = shortcode_atts(array( 'num' => 'other' ),$atts); //based on input determine what to return $output = ''; if($values['num'] == '1'){ $output = 'output for 1'; } else if($values['num'] == '2'){ $output = 'output for 2'; } else if($values['num'] == '3'){ $output = 'output for 3'; } else{ $output = ' '; } return $output; } add_shortcode('ball','ball_callback'); Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted December 7, 2015 Share Posted December 7, 2015 You could create an array of valid values and then run the test. For example <?php function ball_callback($atts,$content,$tag){ //collect values, combining passed in values and defaults $values = shortcode_atts(array( 'num' => 'other' ),$atts); //based on input determine what to return $output = ''; $validValues = array('1', '2', '3'); if(in_array($values['num'], $validValues)){ $output = "output for {$values['num']}"; } return $output; } add_shortcode('ball','ball_callback'); ?> Note that you could use range() to create the array for $validValues. More information can be found here: http://php.net/manual/en/function.range.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.