0

I have this php function in wordpress

function includePosts ($content = '') { 
    preg_match_all('/(?<=\\[\\[)\\d+?(?=\\]\\])/', $content, $matches, PREG_PATTERN_ORDER);
    $numMatches = count($matches[0]);
    for ($i = 0; $i < $numMatches; $i++) {
        $postId = $matches[0][$i];
        $post= get_post($postId);
        $linkToPost = '<a href="'.get_permalink($postId).'">';  
        $postTitle = $post->post_title;
        $postTitleText = "<li> $linkToPost$postTitle</a></li>";
        $content = str_replace("[[$postId]]", $postTitleText, $content);
    }
    return $content;
}
//  add_action('admin_menu', 'addAdminPage');
add_filter('the_content', 'includePosts', 1);

What this does is to bring the shortcode from the wordpress page, wich would be a post id and displays the title of that post between <li>..<li> Everything ok .. well not quite. I want all the <li> ... </li> inside one <ul>...</ul>. Is it posible in this function?


jeremysawesome, one thing i forgot to mention return $content returns not only the <li> but also the content of the page where the shortcode is, but thank you for your time.

Tristar Web Design, your solution acts strange, I get something like this:

<ul>

   <li>...</li>

   <li>...</li>

      <ul>

        <li>...</li>

        <li>...</li>

     </ul>
</ul> 

I really don't get it

0

2 Answers 2

1

Just a shot in the dark. If you replace your return statement with the one below, does it accomplish what you would like?

return "<ul>$content</ul>";
Sign up to request clarification or add additional context in comments.

1 Comment

That wouldn't work, that would wrap the whole content and the list elements in a ul
0

Something like this might work:

function includePosts ($content = '') { 
    preg_match_all('/(?<=\\[\\[)\\d+?(?=\\]\\])/', $content, $matches, PREG_PATTERN_ORDER);
    $numMatches = count($matches[0]);
    for ($i = 0; $i < $numMatches; $i++) {
        $postId = $matches[0][$i];
        $post= get_post($postId);
        $linkToPost = '<a href="'.get_permalink($postId).'">';  
        $postTitle = $post->post_title;
        $ul = ($i == 0) ? '<ul>' : '';
        $endul = ($i == $numMatches) ? '</ul>' : '';
        $postTitleText = "$ul<li> $linkToPost$postTitle</a></li>$endul";
        $content = str_replace("[[$postId]]", $postTitleText, $content);
    }
    return $content;
}
//  add_action('admin_menu', 'addAdminPage');
add_filter('the_content', 'includePosts', 1);

Not 100% sure this will work, but give it a try!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.