WordPress.org

Support

Support » How-To and Troubleshooting » Why is max_num_pages empty?

Why is max_num_pages empty?

  • Adam

    @cscottrun

    When doing something like:

    global $wp_query;
    $total_pages = $wp_query->max_num_pages;
    var_dump($total_pages); exit;

    I get:

    int(0)

    when there are like 15, 16 posts. so it should go 1 -> 16

    I see other posts, that no one has responded to in regardes to this

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    @keesiemeijer

    On which theme template file are you doing this?

    Adam

    @cscottrun

    single.php

    At the bottom.

    If I do:

    previous_post_link();

    I get a previous link for the post before the current one I am on.

    Moderator keesiemeijer

    @keesiemeijer

    The query on single.php returns a single post object that’s why the max_num_pages returns 0. What is it you want to do? Count all posts of the current post type?

    Maybe try wp_count_posts().

    or:
    http://codex.wordpress.org/Function_Reference/get_adjacent_post

    Adam

    @cscottrun

    No, pagination. I have already read the docs, but I only want it to show if there are more then one “page”

    Edit:

    I wanted it to show numbers. ONLY if there are more then one post or page.

    I figured out that to get this too work it has to go on the index.php template >.>

    How ever even with that fix it still doesn’t work, I have even looked at and copied how twenty eleven does it and still get nothing

    Moderator keesiemeijer

    @keesiemeijer

    You need to use next_posts_link() and previous_posts_link() on your index.php (extra “s” in the function name). If you want to have numbered pagination you could try the paginate_links() function or a plugin: http://wordpress.org/extend/plugins/wp-pagenavi/

    This is loop in wordpress:
    $wp_query = new WP_Query();
    $wp_query->query(‘category_name=’.$catname.’&showposts=5′.’&paged=’.$paged);

    you can use $total_pages = $wp_query->max_num_pages;

    Other examples:
    $mypost=new WP_Query();
    $mypost->query(‘cat=9&paged=$paged’);
    while($chamngon->have_posts()):$chamngon->the_post();
    <!– Show post –>
    Endwhile;

    Use $total_pages = $mypost->max_num_pages;

    Note: The bold line

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Why is max_num_pages empty?’ is closed to new replies.