WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] shortcode function (10 posts)

  1. danielpol
    Member
    Posted 3 years ago #

    Hi guys
    I have an issue with this function http://pastebin.com/JSxsQKQt

    What the function does is to replace the shortcode from a page with specified post title. The shortcode from the page is like [[123]] , where 123 is the ID of the post.
    It works but needs a little tweaking and my php skills suck.
    It returns the titles like <li> title </li> but I want all the <li> inside <ul> ... </ul>.
    The return $content; returns not just the <li>title</li> but also the content of the page where the shortcode is .

    Anyone can help ?
    Thanks

  2. shahar
    Member
    Posted 3 years ago #

    Hi, please take a look here:
    http://pastebin.com/P1EXiNaT

    I added 2 lines around $postTitleText (line 20, 22).

    This will only work if your shortcodes are next to each other, if you have other text in between it will wrap it all in the </ul>

    Hope it helps!

  3. danielpol
    Member
    Posted 3 years ago #

    shabar I added the 2 lines but bothing happens, it's the same with or without the 2 lines.
    Thanks for your time.

  4. shahar
    Member
    Posted 3 years ago #

    Did you change line 21 too? You need to add the '.' like this:

    $postTitleText .=

  5. danielpol
    Member
    Posted 3 years ago #

    oops, sorry.
    changed it now but still doesn't work as expected
    I get this

    <ul>
        <li>...</li>
        <li>...</li>
     <ul>
    <ul>
        <li>...</li>
        <li>...</li>
         the page content
     <ul>

    This is strange

  6. shahar
    Member
    Posted 3 years ago #

    Can you paste the post content so I can see what you're running this on?

    I used something like this and it worked:

    Page content here
    12
  7. danielpol
    Member
    Posted 3 years ago #

    This is what i have

    866  841  866  841
    <h4>Lorem ipsum </h4>
    .............
    ............
  8. shahar
    Member
    Posted 3 years ago #

    Because you're replacing a particular post id using the regular expression, it will replace each occurrence of the post id at the same time.

    So. If you try [[866]] [[841]] only the code should work fine.

    The problem in the example is that both times [[866]] occurs it is being replaced by the link that logically opens the<ul>.

    Does that make sense? Please try without duplicate post ids, it should work.

  9. danielpol
    Member
    Posted 3 years ago #

    Ah, it WORKS. Silly me :)
    Thanks a lot shabar.
    I owe you

  10. shahar
    Member
    Posted 3 years ago #

    My pleasure.
    That's what the forums are for :D

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.