我对 Wordpress 帖子库有疑问:
我创建了一个 php 代码,它获取所有帖子画廊图像并将其显示在 jquery 幻灯片上。当我向我的博客添加帖子时,我设置了“创建画廊”,然后单击“插入帖子”。一切都很好......我的代码获取所有画廊图像并以漂亮的幻灯片显示。但问题是:如何从帖子中删除图库(因为它出现在我放置幻灯片的位置和帖子内容中)?
请查看此图片以获取更多说明:
提前非常感谢!
只需在functions.php中按类过滤内容,删除节点并返回新内容。您可以从帖子中删除所有画廊,然后使用自己的自定义画廊样式
function remove_gallery($content) {
$doc = new DOMDocument();
// turn off html5 errors
libxml_use_internal_errors(true);
// load html with utf8
$doc->loadHTML('<?xml encoding="utf-8" ?>'.$content);
libxml_clear_errors();
$finder = new DomXPath($doc);
// find by class (you can also find by id one element without use DomXPath
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' wp-block-gallery ')]");
// remove them all
foreach($nodes as $node){
$node->parentNode->removeChild($node);
}
// get only body, and clear what we need to
$body = $doc->documentElement->lastChild;
$content = $doc->saveHTML($body);
//remove body tag
$content = str_replace("<body>", "", $content);
$content = str_replace("</body>", "", $content);
// clear HTML comments
$content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
// clear return characters
$content = str_replace(array("\n", "\r"), '', $content);
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
在编辑模式下打开您的帖子。
现在单击视觉布局。
选择图库,它会在左上角显示两个图标,单击第二个图标从帖子中删除图库。
单击更新按钮以保存您的更改。
好的,这将解决循环文件(functions.php)的问题。
您只能取消注册画廊短代码并再次注册为无所事事;)。
remove_shortcode('gallery');
add_shortcode('gallery', 'gallery_shortcode_lo');
function gallery_shortcode_lo($attr) {
}