0

I have a simple custom shortcode that works fine inserting the attributes. However is it possible to put the standard lists within my shortcode and customise them for the output?

This is my input for the shortcode

[nr title="title" time="time" timec="c"]
[ul]
[li]Unordered[/li]
[/ul]

[ol]
[li]Ordered[/li]
[/ol]

[/nr]

This is my shortcode // Add Shortcode function custom_shortcode_nr( $atts , $content = null ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'time' => '15m',
            'timec' => 'h0m15',
            'title' => 'the title',
        ),
        $atts,
        'nr'
    );

    // Return image HTML code
    return '<div id="nr"><h2>' . $atts['title'] . '</h2><ul class="details"><li><strong>Time ' . $atts['time'] . '</strong> (' . $atts['timec'] . ')</li></ul>' . $content . '</div>';
}
add_shortcode( 'nr', 'custom_shortcode_nr' );

This is the output functionality I would like to happen to unordered and ordered lists within my shortcode

<h3>title un</h3>
<ul>
    <li class="unli">Unordered</li>
</ul>

<h3>title or</h3>
<ol>
    <li class="orli">Ordered</li>
</ol>

Is this possible?

thanks

1 Answer 1

0

IMO you need to create individual shortcodes for UL, OL and LI to make it sample.

Shortcode for UL.

function shortcode_ul( $atts , $content = null )
{
     return '<ul>' . do_shortcode( $content ) .'</ul>';
}
add_shortcode( 'ul', 'shortcode_ul' );

Shortcode for LI

function shortcode_li( $atts , $content = null )
{
     return '<li>' . do_shortcode( $content ) .'</li>';
}
add_shortcode( 'li', 'shortcode_li' );

Use

[ul] [li]Unordered[/li] [/ul]

It should output

<ul><li>Unordered</li></ul>

Note: Add attributes as per requirement. E.g for CSS class <li> use attribute class. It'll be more flexible and extendable.

1
  • Thanks so much, is there any way to use just the normal [li] tag for each of them but call a different function depending on what type of custom [ul] or [ol] they are in? Mar 21, 2018 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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