从 span 标签 HTML DOM 替换类

我正在尝试在 HTML 类中替换和添加一些值...我在某种程度上成功地解决了这个问题,但现在我需要帮助。这是我的功能代码


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

            }

http://img1.mukewang.com/644b74c50001bbdf06260092.jpg

现在我还想替换<span class="page-numbers current">to的类<span class="pagination-link is-current "> ,使其看起来像这样
http://img3.mukewang.com/644b74d20001cc0401770069.jpg

我还试图<li></li>从下一页和上一页按钮中删除标签。任何类型的帮助将不胜感激


白板的微信
浏览 154回答 2
2回答

HUX布斯

在对我的代码进行一些实验后,我解决了我的问题。这是我的代码...function wpse247219_custom_pagination() {&nbsp; &nbsp; global $the_query;&nbsp; &nbsp; $big = 999999999; // need an unlikely integer&nbsp; &nbsp; $pages = paginate_links( array(&nbsp; &nbsp; &nbsp; &nbsp; 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),&nbsp; &nbsp; &nbsp; &nbsp; 'format' => '?paged=%#%',&nbsp; &nbsp; &nbsp; &nbsp; 'current' => max( 1, get_query_var('page') ),&nbsp; &nbsp; &nbsp; &nbsp; 'total' => $the_query->max_num_pages,&nbsp; &nbsp; &nbsp; &nbsp; 'prev_next' => false,&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'array',&nbsp; &nbsp; &nbsp; &nbsp; 'prev_next'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; 'prev_text'&nbsp; &nbsp; => __( 'Previous', 'text-domain' ),&nbsp; &nbsp; &nbsp; &nbsp; 'next_text'&nbsp; &nbsp; => __( 'Next page', 'text-domain'),&nbsp; &nbsp; ) );&nbsp; &nbsp; $output = '';&nbsp; &nbsp; if ( is_array( $pages ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );&nbsp; &nbsp; &nbsp; &nbsp; $size = count($pages)-1;&nbsp; &nbsp; &nbsp; &nbsp; $output .=&nbsp; '<ul class="pagination-list">';&nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $pages as $page ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($i < 1 || $i == $size){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "$page";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<li>$page</li>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; $output .= '</ul>';&nbsp; &nbsp; &nbsp; &nbsp; $dom = new \DOMDocument();&nbsp; &nbsp; &nbsp; &nbsp; $dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );&nbsp; &nbsp; &nbsp; &nbsp; $xpath = new \DOMXpath( $dom );&nbsp; &nbsp; &nbsp; &nbsp; $page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $page_numbers as $page_numbers_item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item_classes = explode( ' ', $page_numbers_item->attributes->item(0)->value );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( in_array( 'current', $page_numbers_item_classes ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $list_item_attr_class = $dom->createAttribute( 'class' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $list_item_attr_class->value = 'mynewclass';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item->parentNode->appendChild( $list_item_attr_class );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the class 'page-numbers' with 'page-link'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item->attributes->item(0)->value = str_replace(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page-numbers',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'pagination-link',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item->attributes->item(0)->value );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Replace the class 'page-numbers' with 'page-link'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item->attributes->item(0)->value = str_replace(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'prev pagination-link',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'pagination-previous',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $page_numbers_item->attributes->item(0)->value );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $yupo = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers current')]" );&nbsp; &nbsp; &nbsp; &nbsp; foreach($yupo as $yup){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $yup->attributes->item(1)->value = str_replace(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page-numbers current',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'pagination-link is-current',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $yup->attributes->item(1)->value );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Save the updated HTML and output it.&nbsp; &nbsp; &nbsp; &nbsp; $output = $dom->saveHTML();&nbsp; &nbsp; }&nbsp; &nbsp; return $output;}

小怪兽爱吃肉

您可以仅使用 vanilla JavaScript 轻松实现此目的:const elActivePage = document.querySelector('span.current');elActivePage.className = 'pagination-link is-current';
打开App,查看更多内容
随时随地看视频慕课网APP