最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Paginated pages not showing for non-logged-in users - Stack Overflow

programmeradmin3浏览0评论

I generate virtual pages (using index.php), which look like this:

domain/died-persons/2024/

where 'died-persons' is a static page with a table of contents (list of years) and '2024' is a virtual directory (variable) which I manage by a rewrite rule. This works fine whether I'm logged in or not.

When I try to open paginated pages like this:

domain/died-persons/2024/page/2/

it only works, when I'm logged in. When I'm logged out, the page isn't found and I get a 404 error.

In functions.php I have this:

function year_add_query_vars( $vars ) {
  $vars[] = 'variable';
  return $vars;
}
add_filter( 'query_vars', 'year_add_query_vars' );

add_rewrite_rule('died-persons/([0-9]{4})[/]page/([0-9]{1,})/?$', 'index.php?variable=$matches[1]&paged=$matches[2]', 'top');

In index.php I have this condition for getting the template:

if ( preg_match('#died-persons/([0-9]{4})/page/([0-9]{1,})/?$#', $_SERVER['REQUEST_URI'])) {    
get_template_part( 'template-parts/content-personlist' ); 
} 

In content-personlist.php I generate the list of items (persons), which I'm getting from an external database.

I would be happy if someone could help me with this problem. Thanks in advance!

Andreas

I generate virtual pages (using index.php), which look like this:

domain/died-persons/2024/

where 'died-persons' is a static page with a table of contents (list of years) and '2024' is a virtual directory (variable) which I manage by a rewrite rule. This works fine whether I'm logged in or not.

When I try to open paginated pages like this:

domain/died-persons/2024/page/2/

it only works, when I'm logged in. When I'm logged out, the page isn't found and I get a 404 error.

In functions.php I have this:

function year_add_query_vars( $vars ) {
  $vars[] = 'variable';
  return $vars;
}
add_filter( 'query_vars', 'year_add_query_vars' );

add_rewrite_rule('died-persons/([0-9]{4})[/]page/([0-9]{1,})/?$', 'index.php?variable=$matches[1]&paged=$matches[2]', 'top');

In index.php I have this condition for getting the template:

if ( preg_match('#died-persons/([0-9]{4})/page/([0-9]{1,})/?$#', $_SERVER['REQUEST_URI'])) {    
get_template_part( 'template-parts/content-personlist' ); 
} 

In content-personlist.php I generate the list of items (persons), which I'm getting from an external database.

I would be happy if someone could help me with this problem. Thanks in advance!

Andreas

Share Improve this question edited Mar 17 at 12:43 Andreas Böttcher asked Mar 17 at 11:44 Andreas BöttcherAndreas Böttcher 111 silver badge3 bronze badges 6
  • Hey there. you say in your post that the behaviour changes between logged in and logged out states, yet you don't include any code that involves user sessions. You want to check the whole process for a variable or otherwise which is only retrieved when logged in, but from what you've sent us, I doubt anyone can help – Michael Beeson Commented Mar 17 at 12:22
  • Looks like this is part of a WordPress system? Then you should mention that / appropriately tag the question. – C3roe Commented Mar 17 at 12:24
  • Sorry, yes, it's Wordpress. What exactly do you need to narrow down the cause of the problem? – Andreas Böttcher Commented Mar 17 at 12:43
  • Probably not related to your problem, but inspecting the raw URL can be troublesome in WordPress sometimes. You are correctly registering your interest in a custom variable using the query_vars hook, so I would encourage you to use get_query_var in index.php to check for its existence. – Chris Haas Commented Mar 17 at 14:19
  • paged is a special internal WordPress variable, however you are trying to use it with your own custom variable. I can't say this for absolute certain, but I wouldn't be surprised if this is confusing WordPress. Try using your own variable for pagination that isn't listed in codex.wordpress./WordPress_Query_Vars – Chris Haas Commented Mar 17 at 14:26
 |  Show 1 more comment

1 Answer 1

Reset to default 0
  1. Flush Rewrite Rules

    • Add this temporarily in functions.php and visit the site once:

      add_action('init', function() {
      flush_rewrite_rules();
      });

    • Remove it afterward.

  2. Ensure Query Vars Are Passed

    • Modify your query vars filter:

      function year_add_query_vars($vars) {
      $vars[] = 'variable';
      $vars[] = 'paged';
      return $vars;
      }

      add_filter('query_vars', 'year_add_query_vars');

  3. Fix Permalink Structure

    • Check if Settings → Permalinks is set to "Post Name" and save.
  4. Check Server Cache

    • If using a caching plugin or Cloudflare, clear the cache.
发布评论

评论列表(0)

  1. 暂无评论