Querying Advanced Custom Fields; want to paginate the results

Not sure if this pertains specifically to Advanced Custom Fields, but I’ll keep this short. I’m relatively new to PHP and want to know the best way of approaching this task. Also, this is a WordPress (4.1.1) environment.

I’m sending a query for all post types with an associated category type to pull in and display. If the query can find posts, it produces an <article> block with additional html for each result. Not to complicated right?

Read More

Now, I want to create a pagination element if/when the query produces greater than 8 results.

Environment with the front-end of the query results can be found:
http://test-hdwg.pantheon.io/news/

Template being utilized for this page (news.php) contains the following:

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

        <main id="primary" class="content-area">
          <div class="jumbotron">
            <div class="row">
              <div class="intro">
                <p><?php the_field('news_heading') ?></p>
              </div>
            </div>
          </div>
          <div class="news">
            <div class="row news-articles">
              <div class="column-wrapper">
                <div class="small-4 columns news-left-column">
                  <div class="select-dropdown">
                    <button href="#" data-dropdown="dropdown-items" aria-controls="dropdown-items" aria-expanded="false" class="dropdown">Select a news category</button>
                    <ul id="dropdown-items" data-dropdown-content class="f-dropdown" aria-hidden="true" tabindex="-1">
                      <?php $args = array(
                        'exclude'            => '',
                        'title_li'           => __( '' ),
                        'show_option_none'   => __( '<li><a href="#primary">No categories</a></li>' ),
                        'taxonomy'           => 'category',
                        'child_of'           => 2,
                        'current_category'   => 0
                      ); ?>
                      <?php wp_list_categories($args); ?>
                      <li class="cat-item cat-item-all-news"><a href="/news" alt="View all News" title="View all News">All News</a>
                    </ul>
                  </div>
                  <div class="news-block">
                    <span class="news-block-heading">Email Newsletters</span>
                    <div class="news-inner-block">
<?php if( have_rows('newsletters_list') ): ?>
  <?php while( have_rows('newsletters_list') ): the_row(); ?>
                      <span class="year"><?php the_sub_field('newsletter_year'); ?></span>
    <?php if( have_rows('newsletter_resource') ): ?>
                      <ul>
      <?php while( have_rows('newsletter_resource') ): the_row(); ?>
                        <li><a href="<?php the_sub_field('newsletter_url'); ?>" target="_blank"><?php the_sub_field('newsletter_title'); ?></a></li>
      <?php endwhile; ?>
                      </ul>
    <?php endif; ?>
  <?php endwhile; ?>
<?php endif; ?>
                    </div>
                  </div>
                </div>
                <div class="small-8 columns news-right-column">
<?php query_posts( 'post_type=post&cat=' ); ?>

  <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
                  <article class="large-6 columns ui-article-block">
                    <h3><?php echo get_the_date( 'd M Y' ); ?></h3>
                    <hr/>
                    <h2><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></h2>
                    <h1><?php the_title(); ?></h1>
                    <p>
      <?php
        $content = get_the_content();
        $content = strip_tags($content);
        echo substr($content, 0, 175) . "..."; // set a character cut-off at 175 characters
      ?>
                    </p>
                    <a href="<?php the_permalink(); ?>" alt="Read more about the article: <?php the_title_attribute(); ?>" title="Read more about the article: <?php the_title_attribute(); ?>" class="button postfix">Read More</a>
                  </article>
    <?php endwhile; ?>
  <?php else : ?>
                  <div class="large-8 columns">
                    <p class="lead">Well this is embarrassing. There are currently no news articles.</p>
                  </div>
  <?php endif; ?>
<?php wp_reset_query(); ?>
                </div>
              </div>
            </div>
          </div>
        </main>
<?php get_footer(); ?>

Just to be clear, this is not an issue, but more-so a request from more experience developers on how to approach a task utilizing Advanced Custom Field elements (if that matters).

Any help would be greatly appreciated!

Related posts

Leave a Reply

1 comment