How to overwrite page number variable?

#447828
  • Resolved Raf
    Rank Math free

    Hi,

    I created template files programmatically for some post types and terms, some came by default on the theme I’m currently using. I don’t know if there is some kind of conflict between my theme and rank math plugin but numbering (pagination) on the page title doesn’t appear. It only appears on the URL.

    I added the following snippet of code to help, but it only worked in some pages:
    if ( ! function_exists( 'multipage_title' ) ) {
    function multipage_title( $title ) {
    global $page;
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    ! empty ( $page ) && 1 < $page && $paged = $page;
    //$paged > 1 && $title .= ' - ' . sprintf( __( 'Part %s', 'theme' ), $paged );
    $paged > 1 && $title .= '';
    return $title;
    }
    add_filter( 'rank_math/frontend/title', 'multipage_title', 100, 1 );
    }

    There’s unusual issue with pages post types, in this case template files and rank math.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    That filter is not correct because you are returning the variable $title but you are not defining it anywhere.

    Also, it seems that there are some conditions missing and some statements not being added correctly.

    ​​​​​​​You could try dumping the variable we mentioned before and retuning it to make sure it’s getting parsed.

    Don’t hesitate to get in touch if you have any other questions.

    Raf
    Rank Math free
     if ( ! function_exists( 'multipage_title' ) ) {
        function multipage_title( $title ) {
           global $wp_query;
          $temp_title = $title;            
          if ( is_page_template('template-videos.php') || is_page_template('template-actors.php') ) {
             $wp_query->page > 1 && $temp_title .= '';
             return $temp_title;
          } else {
             return $title;
          }
        }
        add_filter( 'rank_math/frontend/title', 'multipage_title' );
     }

    I tried many workarounds, nothing seems to work. The code above is just simple test, which failed anyway. BTW, what do you mean by defining $title variable somewhere? I mean the function returns one argument for rank_math/frontend/title filter that I guess is used for header template parts on frontend.

    Hello,

    Why are you returning $temp_title and then $title if they are essentially the same as you have stated in the code in the first lines?

    We would recommend using the variables available to use in the title for pagination if you are not familiar with setting them dynamically via code snippets.

    Here’s a very simple implementation of what a code for what you are trying to achieve would look like:

    
    add_filter( 'rank_math/frontend/title', function( $title ) {
        if( get_query_var('paged') > 1 ) {
            $title = 'Paged page';
        }
    	return $title;
    });
    

    Please note that we don’t offer personalized code snippets so you would need to edit that code further to cater to your exact needs.

    Don’t hesitate to get in touch if you have any other questions.

    Raf
    Rank Math free

    Hi,

    Many thanks. I finally fixed the issue for my case needs.

    In the end I have this filter:

     if ( ! function_exists( 'multipage_title' ) ) {
        function multipage_title( $title ) {
          global $paged;
          if (is_page_template()) {
             "Page $paged" && $title = '';
          }
    
          return $title;
    
        }
        add_filter( 'rank_math/frontend/title', 'multipage_title' );
     }

    Where I have global variable called $paged and a function that returns current page query in each template files I have.

    Hello,

    I am glad that you are able to resolve the issue and thank you for sharing your final code.

    Please do not hesitate to let us know if you need our assistance with anything else.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this ticket.