WordPress 的 PHP 函数

发布于 2024-11-07 16:34:29 字数 1334 浏览 0 评论 0原文

我在 WordPress 中有这个 php 函数,

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

它的作用是从 WordPress 页面获取短代码,这将是一个帖子 ID,并在

  • ..
  • 一切都好..但也不完全是。 我想要所有的
  • ; ...
      ...
    内的
  • 。这个功能可以实现吗?


    jeremysawesome,我忘记提到的一件事 return $content 不仅返回

  • 而且还返回短代码所在页面的内容,但是谢谢您时间。
  • 三星网页设计,你的解决方案表现得很奇怪,我得到这样的信息:

    <ul>
    
       <li>...</li>
    
       <li>...</li>
    
          <ul>
    
            <li>...</li>
    
            <li>...</li>
    
         </ul>
    </ul> 
    

    我真的不明白

    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

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    心是晴朗的。 2024-11-14 16:34:29

    只是在黑暗中开枪。如果您将 return 语句替换为下面的语句,它是否可以实现您想要的效果?

    return "<ul>$content</ul>";
    

    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>";
    
    拔了角的鹿 2024-11-14 16:34:29

    像这样的东西可能会起作用:

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

    不能 100% 确定这会起作用,但尝试一下!

    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!

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文