0

In below function show_game_category_images_func I get 1-2-3-4 results. Depends on what I declared in table. With the add_shortcode I get all results.

Question: how can I split the results of this function to get 1-4 different shortcodes to use.

Example: with 2 results from the function I want as result

[show_game_category_images-1] and [show_game_category_images-2]

Is that somehow possible?

function show_game_category_images_func($atts) {
    $atts = shortcode_atts( array(
    'id' => 1,
     ), $atts );
$content = '';
global $wpdb;
$query  = 'select category_id, category_name, game_nr 
                            from wp_rdp_game_images 
                            where display_date = "'.date('Y-m-d', strtotime('-2 day')).'" 
                            limit  '.get_option('rdp_max_categories');

$cats = $GLOBALS['wpdb']->get_results($query);

if(count($cats) > 0){

    $content .= '<div id="rdp-cat" class="homepage_cats">';

    foreach($cats as $cat){

        $cat->category_id;
        $cat->category_name;

        $query = 'select category_image from  wp_rdp_category_images where category_id = '.$cat->category_id.' order by rand() limit 1';
        $record = $GLOBALS['wpdb']->get_results($query);

        if($record){
            foreach($record as $key => $rec){   
            $content .= '<div class="rdp-cat-image">
            <div class="rdp-cat-text"><div class="cat_name">'.$cat->category_name.'</div>
            <img src="'.$upload_url.$rec->category_image.'" ></div>
            <div class="rdp-cat-link"><a href="/spel/?cat='.$cat->category_id.'&&game='.$cat->game_nr.'">Start</a></div>
            </div>';
            }

            $i++;
        } //if($record)         

    } //foreach($cats

    $content .= '</div>';   
} // if(count($cats
// return $content .' '.$atts['id'];    
return $content;        
}
add_shortcode('show_game_category_images', 'show_game_category_images_func');
1

You might want to declare arguments for shortcodes. Here is documentation, and things you need to improve in your code to make it work.

Change

function show_game_category_images_func()

to

function show_game_category_images_func($atts)

Add following code at the top of your function:

$a = shortcode_atts( array(
    'id' => '1',
), $atts );

Now inside $a['id'] is value passed as argument when declaring shortcode, for example [show_game_category_images id=2]

4
  • Thanks, but how this id changes into 2-3-4. Tested your code but I always get all images instead of 1 when using [show_game_category_images id=2] – Hermants Aug 11 '16 at 12:06
  • You want to limit query results to that number? – Krzysztof Grabania Aug 11 '16 at 12:08
  • yes each result must get his own id. I put the code also after: foreach($record as $key => $rec){ But no success – Hermants Aug 11 '16 at 12:13
  • after end of foreach() print_r( $atts); results in Array ( [id] => 2 ) Array ( [id] => 2 ) Array ( [id] => 1 ) Array ( [id] => 1 ) – Hermants Aug 11 '16 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.