daniel
daniel

Reputation: 1

wordpress php function

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

Upvotes: 0

Views: 147

Answers (2)

Tristar Web Design
Tristar Web Design

Reputation: 765

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!

Upvotes: 0

jeremysawesome
jeremysawesome

Reputation: 7254

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>";

Upvotes: 1

Related Questions