【问题标题】:Querying Advanced Custom Fields; want to paginate the results查询高级自定义字段;想要对结果进行分页
【发布时间】:2015-05-27 21:00:45
【问题描述】:

不确定这是否专门针对高级自定义字段,但我会尽量简短。我对 PHP 比较陌生,想知道完成这项任务的最佳方法。 另外,这是一个 WordPress (4.1.1) 环境

我正在发送一个针对所有帖子类型以及关联类别类型的查询,以便拉入并显示。如果查询可以找到帖子,它会为每个结果生成一个带有附加 html 的

块。不要复杂吧?

现在,如果/当查询产生超过 8 个结果时,我想创建一个分页元素。

查询结果的前端环境可以找到: http://test-hdwg.pantheon.io/news/

用于此页面的模板 (news.php) 包含以下内容:

<?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(); ?>

需要明确的是,这不是问题,而是更多经验丰富的开发人员提出的关于如何使用高级自定义字段元素(如果重要的话)处理任务的请求。

任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress pagination advanced-custom-fields


    【解决方案1】:

    编辑

    此编辑是在 OP 澄清问题性质之后进行的。

    The tutorial here,解释了如何在初学者级别的帖子中处理数字导航。

    【讨论】:

    • 感谢您的回复!这似乎截断了我的结果,但我仍然想知道是否有人对建立分页有建议?我们需要它显示数字而不是标准(上一篇文章/下一篇文章)WordPress默认分页。有什么建议吗?
    • 我添加了一个很好的资源,也希望其他人也能参与进来。
    • 感谢您将我链接到该资源 Jude。我能够建立分页,但我注意到两个错误。我应该用我发现的错误更新这篇文章,还是应该创建一个新问题?
    • 一个新问题最好..我去看看
    • 我创建了一个新问题,该问题与我在您建议的链接中使用开箱即用方法所做的发现有关。请在此处查看问题:stackoverflow.com/questions/29420662/… 当您有时间时,谢谢!
    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 2015-08-20
    • 2018-02-04
    • 2023-03-17
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多