WordPress.org

Ready to get started?Download WordPress

Forums

Why is max_num_pages empty? (7 posts)

  1. Adam
    Member
    Posted 1 year ago #

    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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    On which theme template file are you doing this?

  3. Adam
    Member
    Posted 1 year ago #

    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.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  5. Adam
    Member
    Posted 1 year ago #

    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

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    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/

  7. nguyentrongtiep
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic