0

I just need some help with a WordPress shortcode function. I would like to create a list of links based on the link_category inside a post with a shortcode. I had the shortcode working until I added the limit argument to the function. So Basically can someone help me correct this problem? The code can be seen below, as copied from my functions.php file. Please note that I am no php expert, but I can edit/break existing code like a champ. The site in question is http://linuxlibrary.org

I appreciate the help!

add_shortcode('lynx', 'lynx_shortcode_handler');

function lynx_shortcode_handler($atts)
{
    // extract parameters
    $parms = shortcode_atts(array(
        'cat_name' => 'Learn More',
        'limit' => '5',
        'use_description' => 'false',
        'use_image' => 'false',
        'before_line' => '<li>',
        'after_line' => '</li>
',
        'before_content' => '<ul>
',
        'after_content' => '</ul>
',
        'target' => '',
        'xfn' => '',
        ), $atts);

    $cat_name = $parms['cat_name'];
    $use_description = strtolower($parms['use_description']);
    $limit = $parms['limit'];
    $use_image = strtolower($parms['use_image']);
    $before_line = $parms['before_line'];
    $after_line = $parms['after_line'];
    $before_content = $parms['before_content'];
    $after_content = $parms['after_content'];
    $target = $parms['target'];
    $xfn = $parms['xfn'];

    // process t/f options
    $b_use_description = false;
    if (($use_description == 'yes') || ($use_description == 'y') ||
        ($use_description == 'true') || ($use_description == '1'))
    {$b_use_description = true;}

    $b_use_image = false;
    if (($use_image == 'yes') || ($use_image == 'y') ||
        ($use_image == 'true') || ($use_image == '1'))
    {$b_use_image = true;}

    // exit
    return lynx($cat_name, $b_use_description,
        $b_use_image, $before_line,
        $after_line, $before_content,
        $after_content, $target, $xfn);

}


}

function lynx(
    $cat_name='Learn More',
    $limit='5',
    $use_description='1',
    $use_image='1',
    $before_line='<li>',
    $after_line='</li>
',
    $before_content='<ul>
',
    $after_content='</ul>
',
    $target='',
    $xfn='')
{

    // get the data
    $args = array('limit' => $limit,
            'orderby' => 'rating',
            'order' => 'DESC',
            'category_name' => $cat_name);
    $bookmarks = get_bookmarks($args);

    // process the data
    $output = $before_content;
    foreach ($bookmarks as $bmk)
    {
        $bmk_link = $bmk->link_url;
        $bmk_name = $bmk->link_name;
        $bmk_description = $bmk->link_description;
        $bmk_image = $bmk->link_image; // works best with icons (like favicon.ico)
        $href = '<a rel="' . $xfn . '" href="' . $bmk_link . '" target="' . $target . '">' . $bmk_name . '</a>';

        $output .= $before_line;
        if ($use_image == '1')
        {
            // assumes 16x16 icons and aligns margin accordingly
            // MSIE has issues with some favicon.ico files
            $output .= '<img src="' . $bmk_image . '" width="16" height="16" style="margin-bottom:-2px;" />&nbsp;';
        }
        $output .= $href;
        if ($use_description == '1')
        {
            $output .= ' ' . $bmk_description;
        }
        $output .= $after_line;
    }

    // exit
    $output = $output . $after_content;
    return $output;
}
  • I would like the default limit set to 4, with the option to add manual arguments to the shortcode in posts. – thinkinhurtz Feb 3 '14 at 17:28
0

You seem to not be passing $limit in your call to lynx(). Here is the corrected version:

return lynx($cat_name, $limit $b_use_description,
    $b_use_image, $before_line,
    $after_line, $before_content,
    $after_content, $target, $xfn);
| improve this answer | |
  • Thats great, I'll give it a try now. – thinkinhurtz Feb 3 '14 at 17:31
  • Seems to have worked just fine. I will continue to test it for further tweaks. Thanks again for the quick response! This really helps! – thinkinhurtz Feb 3 '14 at 17:35
  • Confirmed after testing output further. Everything seems to be working well. Thank you very much for the help. – thinkinhurtz Feb 3 '14 at 17:37

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.