skip to Main Content

I’m a newbie in WordPress and today I have to read a code of a senior, the code is from the index.php file for a website:

<?php
          $i = 0;
          if ( have_posts() ) : while ( have_posts() ) : the_post();
          {
            if ($i == 0 && $paged < 1)
            {
              show_content("","12","card", "4");
            }
            elseif ($paged >= 1) { get_template_part( 'template-parts/content', 'card' ); }
            $i = $i + 1;
          }
          endwhile; else:
            echo("<p>No post found :(</p>");
          endif;

This block of loop is for displaying the posts in the homepage. When I search for the variable $paged I can only find the result about the global variable "page" and "pages" in WordPress documentation, but not "paged". Then I asked my senior and even Chat GPT, both of them can tell me very clearly about the properties of the variable "paged" but when I asked them to send me a link to the documentation they couldn’t find it either except for some Stackoverflow post about it. So I’m not sure is this variable some kind of out-of-date or why can’t I find it on WordPress official pages? Is there something bad about it that WordPress team don’t want to put it on their site along with other global variables? It’d be wonderful if somebody can explain it to me. Thanks in advance.

I tried to Goolgle a lot of place but can’t find the answer.

2

Answers


  1. It is really difficult to answer you with just a partial code.

    The first thing that come in my mind about "paged" parameter is one of the parameters that you can use when requesting Db (via Wp_Query of other shortcode methods derivated form it like get_posts() which inherit some of its parameters).

    In this context, it tells wordpress how many posts you want to retrive (and display) per page when displaying a paginated listing of post.

    See here: https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters

    paged (int) – number of page. Show the posts that would normally show up just on page X when using the “Older Entries” link.
    

    That beeing said, in PHP, a variable can be global or not, and in the snippet you shared, there is nothing that tells us that $paged is a global variable, so it could simply be initiated earlier in the code (on in another php file included earlier on the page).

    Login or Signup to reply.
  2. References to $paged likely refer to the paged query variable. WordPress supports pagination on several of its rewrite rules, ex: /my-page-name/page/2. To access the number (in the example, 2), you would do this:

    $paged = get_query_var( 'paged', 1 );
    

    If the URL does not include pagination information (ex: /my-page-name/), then the $paged variable would default to the value of the second parameter, in this case 1.

    More:
    https://developer.wordpress.org/reference/functions/get_query_var/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search