Php 替换span标记HTMLDOM中的类
我正在尝试替换HTML类并在其中添加一些值。。。我在一定程度上成功地解决了这个问题,但现在我需要帮助。这是我的函数代码Php 替换span标记HTMLDOM中的类,php,wordpress,Php,Wordpress,我正在尝试替换HTML类并在其中添加一些值。。。我在一定程度上成功地解决了这个问题,但现在我需要帮助。这是我的函数代码 function wpse247219_custom_pagination() { global $the_query; $big = 999999999; // need an unlikely integer $pages = paginate_links( array( 'base' => str_replace( $big,
function wpse247219_custom_pagination() {
global $the_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('page') ),
'total' => $the_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => true,
'prev_text' => __( 'Previous', 'text-domain' ),
'next_text' => __( 'Next page', 'text-domain'),
) );
$output = '';
if ( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );
$output .= '<ul class="pagination-list">';
foreach ( $pages as $page ) {
$output .= "<li>$page</li>";
}
$output .= '</ul>';
$dom = new \DOMDocument();
$dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );
$xpath = new \DOMXpath( $dom );
$page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );
foreach ( $page_numbers as $page_numbers_item ) {
$page_numbers_item_classes = explode( ' ', $page_numbers_item->attributes->item(0)->value );
if ( in_array( 'current', $page_numbers_item_classes ) ) {
$list_item_attr_class = $dom->createAttribute( 'class' );
$list_item_attr_class->value = 'mynewclass';
$page_numbers_item->parentNode->appendChild( $list_item_attr_class );
}
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'page-numbers',
'pagination-link',
$page_numbers_item->attributes->item(0)->value );
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'prev pagination-link',
'pagination-previous',
$page_numbers_item->attributes->item(0)->value );
}
// Save the updated HTML and output it.
$output = $dom->saveHTML();
}
return $output;
}
函数wpse247219_自定义分页(){
全局$u查询;
$big=99999999;//需要一个不太可能的整数
$pages=paginate_链接(数组(
'base'=>str_replace($big,%#%',esc_url(get_pagenum_link($big)),
'格式'=>'?分页=%#%',
“当前”=>max(1,获取查询变量('page'),
“总计”=>$the\u query->max\u num\u页面,
“上一步”=>错误,
'类型'=>'数组',
“上一步”=>正确,
'prev_text'=>'('Previous','text domain'),
'下一页'=>'('下一页','文本域'),
) );
$output='';
if(is_数组($pages)){
$paged=(get_query_var('paged')==0)?1:get_query_var('paged');
$output.=';
foreach($页为$页){
$output.=“- $page
”;
}
$output.='
';
$dom=new\DOMDocument();
$dom->loadHTML(mb_convert_编码($output,'HTML-ENTITIES','UTF-8');
$xpath=new\DOMXpath($dom);
$page_numbers=$xpath->query(“//*[包含(concat(“”,规范化空间(@class),“”),“页码”)”);
foreach($页码作为$页码项目){
$page\u numbers\u item\u classes=分解(“”,$page\u numbers\u item->attributes->item(0)->value);
如果(在数组('current',$page\u number\u item\u classes)中){
$list_item_attr_class=$dom->createAttribute('class');
$list_item_attr_class->value='mynewclass';
$page\u number\u item->parentNode->appendChild($list\u item\u attr\u class);
}
//将类“页码”替换为“页面链接”
$page\u number\u item->attributes->item(0)->value=str\u replace(
“页码”,
“分页链接”,
$page\u number\u item->attributes->item(0)->value);
//将类“页码”替换为“页面链接”
$page\u number\u item->attributes->item(0)->value=str\u replace(
“预分页链接”,
“以前的分页”,
$page\u number\u item->attributes->item(0)->value);
}
//保存更新的HTML并将其输出。
$output=$dom->saveHTML();
}
返回$output;
}
此代码当前输出此修改的Html代码
<nav class="pagination is-rounded" role="navigation" aria-label="pagination">
<ul class="pagination-list">
<li>
<a class="pagination-previous" href="https://blog.igsavers.com/page/3/">Previous</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/1/">1</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/2/">2</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/3/">3</a></li>
<li><span aria-current="page" class="page-numbers current">4</span></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/5/">5</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/6/">6</a></li>
<li><span class="pagination-link dots">…</span></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/11/">11</a></li>
<li><a class="next pagination-link" href="https://blog.igsavers.com/page/5/">Next page</a></li></ul>
</nav>
-
- 四,
现在我还想将
类替换为
所以它看起来像这样我还试图从“下一页”和“上一页”按钮中删除标签。非常感谢任何类型的帮助您只需使用普通JavaScript即可轻松实现这一点:
const elActivePage=document.querySelector('span.current');
elActivePage.className='分页链接是当前的';
用我的代码做了一些实验后,我解决了我的问题。这是我的密码
function wpse247219_custom_pagination() {
global $the_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('page') ),
'total' => $the_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => true,
'prev_text' => __( 'Previous', 'text-domain' ),
'next_text' => __( 'Next page', 'text-domain'),
) );
$output = '';
if ( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );
$size = count($pages)-1;
$output .= '<ul class="pagination-list">';
$i = 0;
foreach ( $pages as $page ) {
if($i < 1 || $i == $size){
$output .= "$page";
}else{
$output .= "<li>$page</li>";
}
$i++;
}
$i = 0;
$output .= '</ul>';
$dom = new \DOMDocument();
$dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );
$xpath = new \DOMXpath( $dom );
$page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );
foreach ( $page_numbers as $page_numbers_item ) {
$page_numbers_item_classes = explode( ' ', $page_numbers_item->attributes->item(0)->value );
if ( in_array( 'current', $page_numbers_item_classes ) ) {
$list_item_attr_class = $dom->createAttribute( 'class' );
$list_item_attr_class->value = 'mynewclass';
$page_numbers_item->parentNode->appendChild( $list_item_attr_class );
}
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'page-numbers',
'pagination-link',
$page_numbers_item->attributes->item(0)->value );
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'prev pagination-link',
'pagination-previous',
$page_numbers_item->attributes->item(0)->value );
}
$yupo = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers current')]" );
foreach($yupo as $yup){
$yup->attributes->item(1)->value = str_replace(
'page-numbers current',
'pagination-link is-current',
$yup->attributes->item(1)->value );
}
// Save the updated HTML and output it.
$output = $dom->saveHTML();
}
return $output;
}
函数wpse247219_自定义分页(){
全局$u查询;
$big=99999999;//需要一个不太可能的整数
$pages=paginate_链接(数组(
'base'=>str_replace($big,%#%',esc_url(get_pagenum_link($big)),
'格式'=>'?分页=%#%',
“当前”=>max(1,获取查询变量('page'),
“总计”=>$the\u query->max\u num\u页面,
“上一步”=>错误,
'类型'=>'数组',
“上一步”=>正确,
'prev_text'=>'('Previous','text domain'),
'下一页'=>'('下一页','文本域'),
) );
$output='';
if(is_数组($pages)){
$paged=(get_query_var('paged')==0)?1:get_query_var('paged');
$size=计数($pages)-1;
$output.=';
$i=0;
foreach($页为$页){
如果($i<1 | |$i==$size){
$output.=“$page”;
}否则{
$output.=“- $page
”;
}
$i++;
}
$i=0;
$output.='
';
$dom=new\DOMDocument();
$dom->loadHTML(mb_convert_编码($output,'HTML-ENTITIES','UTF-8');
$xpath=new\DOMXpath($dom);
$page_numbers=$xpath->query(“//*[包含(concat(“”,规范化空间(@class),“”),“页码”)”);
foreach($页码作为$页码项目){
$page\u numbers\u item\u classes=分解(“”,$page\u numbers\u item->attributes->item(0)->value);
如果(在数组('current',$page\u number\u item\u classes)中){
$list_item_attr_class=$dom->createAttribute('class');
$list_item_attr_class->value='mynewclass';
$page\u number\u item->parentNode->appendChild($list\u item\u attr\u class);
}
//将类“页码”替换为“页面链接”
$page\u number\u item->attributes->item(0)->value=str\u replace(
“页码”,
“分页链接”,
$page\u number\u item->attributes->item(0)->value);
//将类“页码”替换为“页面链接”
$page\u number\u item->attributes->item(0)->value=str\u replace(
“预分页链接”,
“以前的分页”,
$page\u number\u item->attributes->item(0)->value);
}
$yupo=$xpath->query(“/*[contains(concat(“”,normalize space(@class),“”),”页码)