Jump to content

Using php to display image based on text number


Go to solution Solved by bluefrog,

Recommended Posts

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 by bluefrog

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

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().

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 :)

  • Solution

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');

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.