1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Please help me with this small code

Discussion in 'PHP' started by Divvy, Dec 20, 2015.

  1. #1
    Hello guys,

    Can someone please help me with this small code?
    global $post;
    $args = array( 'posts_per_page' => 20, 'post_type'=> 'freeGallery', 'portfolio_cat' => 'Featured');
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
    I have 46 posts in total.
    I want to show 20 posts per page.
    I added a pagination in the bottom of the page.

    The problem with my code is that only is showing 20 posts in total, I dont have page 2 and 3 in my pagination, only page 1.
    What I'm doing wrong? :)

    Thanks for any help you can give!!
    Divvy, Dec 20, 2015 IP
  2. Divvy

    Divvy Well-Known Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Fixed! Thank you anyway guys
    Divvy, Dec 21, 2015 IP
  3. mfscripts

    mfscripts Banned

    Likes Received:
    Best Answers:
    Trophy Points:
    Digital Goods:
    How did you fix it? It would be helpful to post the resolution for anyone finding this thread in the future.
    mfscripts, Dec 30, 2015 IP
    nabil_kadimi likes this.
  4. KangBroke

    KangBroke Notable Member

    Likes Received:
    Best Answers:
    Trophy Points:
    The main issue is that you are only showing us part of your code. If this is all of your code then you are missing key things which would add your other pages (So you see more than just page 1).

    You would want to declare that if the page provided was somehow 0 or a number higher than your pages make this page 1. Then you are going to need to query the DB to check the total number of records and use some arithmetic to break that into pages.

    There are many ways to skin this cat.

    However in your case your using the wordpress codex so you can just do this

    global $post;
    $args = array( 'posts_per_page' => 20, 'post_type'=> 'freeGallery', 'portfolio_cat' => 'Featured');
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post);
    $posts[] += $post->ID;
    $current = array_search( get_the_ID(), $posts );
    $prevID = $posts[$current-1];
    $nextID = $posts[$current+1];
    <div class="navigation">
    <?php if ( !empty( $prevID ) ): ?>
    <div class="alignleft">
    <a href="<?php echo get_permalink( $prevID ); ?>"
      title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
    <?php endif;
    if ( !empty( $nextID ) ): ?>
    <div class="alignright">
    <a href="<?php echo get_permalink( $nextID ); ?>"
    title="<?php echo get_the_title( $nextID ); ?>">Next</a>
    <?php endif; ?>
    </div><!-- .navigation -->
    Code (markup):
    Last edited by a moderator: Jan 4, 2016
    KangBroke, Jan 4, 2016 IP