WordPress 的 PHP 函数
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是在黑暗中开枪。如果您将 return 语句替换为下面的语句,它是否可以实现您想要的效果?
Just a shot in the dark. If you replace your return statement with the one below, does it accomplish what you would like?
像这样的东西可能会起作用:
不能 100% 确定这会起作用,但尝试一下!
Something like this might work:
Not 100% sure this will work, but give it a try!