Jump to content

Recommended Posts

Hi, the website have 470 topics, but loads only 10 from the first page. when i click 2,3 etc it also loads topic from the first. Here is the code


 

<?php
/* Template Name: News */
get_header();
?>

<div class="pozadina-about-us-vlez">
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1 class="largeFont">
                <div class="grid__item color-11">
                <?php if (ICL_LANGUAGE_CODE == "mk"){?>
                <a class="link link--yaku" href="#">
                    <span class="wow fadeIn" data-wow-delay="2.2s">н</span><span class="wow fadeIn" data-wow-delay="2.3s">о</span><span class="wow fadeIn" data-wow-delay="2.4s">в</span><span class="wow fadeIn" data-wow-delay="2.5s">о</span><span class="wow fadeIn" data-wow-delay="2.6s">с</span><span class="wow fadeIn" data-wow-delay="2.7s">т</span><span class="wow fadeIn" data-wow-delay="2.8s">и</span>                           
                </a>
                <?php } else if (ICL_LANGUAGE_CODE == "en"){ ?>
                <a class="link link--yaku" href="#">
                    <span class="wow fadeIn" data-wow-delay="2.2s">N</span><span class="wow fadeIn" data-wow-delay="2.3s">e</span><span class="wow fadeIn" data-wow-delay="2.4s">w</span><span class="wow fadeIn" data-wow-delay="2.5s">s</span>                            
                </a>
                <?php } ?>
            </div>
            </h1>
            <h1 class="smallFont">
                <?php
                if (ICL_LANGUAGE_CODE == "mk")
                    echo '<p class="wow fadeIn" data-wow-delay="2.0s">Новости</p>';
                if (ICL_LANGUAGE_CODE == "en")
                    echo '<p class="wow fadeIn" data-wow-delay="2.0s">News</p>';
                ?>
            </h1>
        </div>
    </div>
</div>

<div class="container news-sidebar wow fadeInLeftBig" data-wow-delay="2.9s">
    <div class="row">
    

        <?php
        $paged = (get_query_var('page')) ? get_query_var('page') : 1;
        $args = array(
          'post_type' => 'news',
          'post_status' => 'publish',
          'posts_per_page' => 10,
          'paged'    => $paged
        );
        $query = new WP_Query( $args );
        $i = 0;
        if ( $query->have_posts() ) {
          while ( $query->have_posts() ) {
            $query->the_post();
            $news_id = get_the_ID();
            $featured_image_id = get_post_thumbnail_id($news_id);
            $featured_image = wp_get_attachment_image_src($featured_image_id, 'large');
            $image_url = $featured_image[0];
            $content = get_post_meta( $news_id, 'short_description', true );
            if ($i == 0) {
        ?>
        <div class="col-md-9 wow fadeInUpBig" data-wow-delay="3.2s">
        <?php } else { ?>
        <div class="col-md-9 wow fadeInUpBig" data-wow-delay="0.3s">
        <?php } ?>
            <div class="row">
                <div class="col-md-6">
                    <a href="<?php the_permalink(); ?>">
                        <img src="<?php echo $image_url; ?>" alt="" class="img-responsive">
                    </a>
                </div>
                <div class="col-md-6">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </div>
                <div class="col-md-6">
                    <h4><i class="fa fa-calendar" aria-hidden="true" style="margin-right: 10px;"></i><?php echo get_post_time('d.m.Y'); ?></h4>
                    <h4><i class="fa fa-clock-o" aria-hidden="true"></i><?php echo get_post_time('H:i'); ?></h4>
                    <!-- <h4>20:00</h4> -->
                </div>
                <div class="col-md-6">
                    <?php echo $content; ?>
                    <a href="<?php the_permalink(); ?>" class="btn btn-news btn-sm"><?php _e( "Повеќе", "ohleto" ); ?></a>
                </div>
            </div>
        </div>
        <?php $i++; }
        } ?>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-12">
        <?php if ($query->max_num_pages > 1) { ?>
        <div class="pager" style="text-align: center;">
        <?php //previous_posts_link('Newer Posts'); echo paginate_links(); 
            $big = 999999999; // need an unlikely integer
            echo paginate_links( array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => max( 1, get_query_var('paged') ),
                'total' => $query->max_num_pages ,
                'prev_text' => __('«'),
                'next_text' => __('»')
            ) ); 
        ?>
        </div>
        <?php } ?>
<!-- <nav aria-label="Page navigation" id="div1">
  <ul class="pager">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">«</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">»</span>
      </a>
    </li>
  </ul>
</nav> -->
        </div>
        </div>
    </div>
</div>
</div>

<?php
get_footer();
?>

 

Edited by nikols
Link to comment
https://forums.phpfreaks.com/topic/312902-need-help-with-code/
Share on other sites

You need three things for pagination to work :

  1. Current Page being displayed
  2. Total Count of Records
  3. The number of pages to be displayed

Then you have to figure out the offset

 public function offset(): float|int
    {
        return $this->per_page * ($this->current_page - 1);
    }

Sorry, that's as far as I can go as that looks like WordPress and that is something that I don't do, but I'm sure WP has some built-in features for pagination. Maybe someone else will be able to help you better?

Edited by Strider64
Link to comment
https://forums.phpfreaks.com/topic/312902-need-help-with-code/#findComment-1587207
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.