Php 要从分页中删除h2中的屏幕阅读器文本
我使用的是215主题,对于分页,我使用了_posts_分页,希望删除h2标记和文本 我已经试过了,但它只是删除了“后导航”文本,而不是h2标记Php 要从分页中删除h2中的屏幕阅读器文本,php,wordpress,Php,Wordpress,我使用的是215主题,对于分页,我使用了_posts_分页,希望删除h2标记和文本 我已经试过了,但它只是删除了“后导航”文本,而不是h2标记 the_posts_pagination( array( 'prev_text' => __( ' ', 'twentyfifteen' ), 'next_text' => __( ' ', 'twentyfifteen' ), 'before_page_nu
the_posts_pagination( array(
'prev_text' => __( ' ', 'twentyfifteen' ),
'next_text' => __( ' ', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',//Page
'screen_reader_text' => __( ' ' )
) );
u后分页(数组)(
“上一个文本”=>”(“二十一五”),
“下一个文本”=>”(“二十一五”),
'在页数'=>''之前。'.''(''Twenty15')。',//第页
“屏幕\阅读器\文本”=>\(“”)
) );
请帮帮我。谢谢现在还不清楚为什么需要去掉h2标记,因为它有class
屏幕阅读器文本
,在标准的Twenty15主题中,该文本被剪裁,因此不会显示
但是,如果确实需要这样做,最简单的方法是使用get_the_posts_pagination()函数获取HTML并在显示之前将其删除:
$nav = get_the_posts_pagination( array(
'prev_text' => __( ' ', 'twentyfifteen' ),
'next_text' => __( ' ', 'twentyfifteen' ),
'screen_reader_text' => __( 'A' )
) );
$nav = str_replace('<h2 class="screen-reader-text">A</h2>', '', $nav);
echo $nav;
$nav=获取分页(数组)(
“上一个文本”=>”(“二十一五”),
“下一个文本”=>”(“二十一五”),
'screen\u reader\u text'=>\u('A')
) );
$nav=str_替换('A',''$nav);
echo$nav;
要了解更多…请尝试
$nav = str_replace('<h2 class="screen-reader-text">A</h2>', '', $nav);
$nav=str_replace('A',''$nav);
我知道这是一篇老文章,但另一个解决方案是将其隐藏在css中:
.屏幕阅读器文本{
显示:无
}
简单,肮脏,有效 用这个。它对我有用
function the_clean_pagination()
{
$thePagination = get_the_posts_pagination();
echo preg_replace('~(<h2\\s(class="screen-reader-text")(.*)[$>])(.*)(</h2>)~ui', '', $thePagination);
}
function_clean_pagination()
{
$thePagination=get_the_posts_pagination();
echo preg_替换(‘~(])(.*)()ui’,‘,$thePagination);
}
这是一个很旧的功能,但是-如果您转到该功能并搜索该功能,则\u posts\u navigation() 您将看到以下参数“screen\u reader\u text”=>(“Posts导航”), 所以我们需要重写
the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => '<span class="prev-page">Prev</span>',
'next_text' => '<span class="next-page">Next</span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
));
u后分页(数组)(
“中等尺寸”=>2,
“prev_text”=>“prev”,
“下一个文本”=>“下一个”,
'屏幕\阅读器\文本'=>'',
'在第页\u编号'=>''之前,
));
只需确保在其中添加空间即可返回空。不是。
另一个选项是使用过滤器并为导航挂钩“导航标记模板”创建我们自己的模板。您可以在function.php中看到添加此项的示例
function sanitize_pagination($content) {
// Remove h2 tag
$content = preg_replace('#<h2.*?>(.*?)<\/h2>#si', '', $content);
return $content;
}
add_action('navigation_markup_template', 'sanitize_pagination');
函数清理分页($content){
//移除h2标签
$content=preg#u replace(“#(.*?)#si”,“$content”);
返回$content;
}
添加操作(“导航标记模板”、“清理分页”);
删除我刚才添加的屏幕阅读器文本
'screen\u reader\u text'=>'
在分页功能中
这对我有用 您提到要删除h2标记和文本。但是对于可访问性,将h2与类
屏幕阅读器文本保持在一起是有帮助的。
显示如何直观地隐藏后期导航
文本,但使其可供屏幕阅读器使用
/* Text meant only for screen readers. */
.screen-reader-text {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
word-wrap: normal !important;
}
我说过它很脏!从技术上讲,文本仍然会在标记中,因此可能仍然会得到SEO的??更糟糕的是,用空白字符串替换字段,那么为什么要用-1??!
/* Text meant only for screen readers. */
.screen-reader-text {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
word-wrap: normal !important;
}