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?
    SEMrush
    <?php
    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); ?>
    PHP:
    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
    SEMrush
  2. Divvy

    Divvy Well-Known Member

    Messages:
    729
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Fixed! Thank you anyway guys
     
    Divvy, Dec 21, 2015 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    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

    Messages:
    1,025
    Likes Received:
    58
    Best Answers:
    4
    Trophy Points:
    215
    #4
    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>
    </div>
    <?php endif;
    if ( !empty( $nextID ) ): ?>
    <div class="alignright">
    <a href="<?php echo get_permalink( $nextID ); ?>"
    title="<?php echo get_the_title( $nextID ); ?>">Next</a>
    </div>
    <?php endif; ?>
    </div><!-- .navigation -->
    Code (markup):
     
    Last edited by a moderator: Jan 4, 2016
    KangBroke, Jan 4, 2016 IP