【问题标题】:WordPress - Pass template location into pagination arrayWordPress - 将模板位置传递到分页数组
【发布时间】:2017-07-29 17:11:24
【问题描述】:

我想将 svg 箭头图像作为 WordPress 后分页循环的 prev_text 和 next_text 传递。目前,我有硬编码的 localhost url。我想设置它,所以这些使用 get_template_directory_uri() 或类似的。

当前代码:

<?php
  $big = 999999999;

  echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
     'format' => '?paged=%#%',
     'current' => max( 1, get_query_var('paged') ),
     'next_text' => ('<img class="next-arrow" src="http://localhost:8888/mockingbird/wp-content/themes/mockingbird/images/arrow.svg" alt="next posts"/>'),
     'prev_text' => ('<img class="prev-arrow" src="http://localhost:8888/mockingbird/wp-content/themes/mockingbird/images/arrow.svg" alt="previous posts"/>'),
     'total' => $query->max_num_pages
   ) );
?>

我想要的伪代码如下:

'next_text' => ('<img class="next-arrow" src="<?php echo get_template_directory_uri(); ?>/images/arrow.svg" alt="next posts"/>'),
'prev_text' => ('<img class="prev-arrow" src="<?php echo get_template_directory_uri(); ?>/images/arrow.svg" alt="previous posts"/>'),

所以我的问题是,是否可以在数组的字符串中传入 php?

如果不是,如何在我的网址中不进行硬编码的情况下实现这一目标?

【问题讨论】:

    标签: php wordpress svg pagination


    【解决方案1】:

    通过将 next_text 和 prev_text 设置为空字符串,然后将 arrow.svg 设置为我的 css 中下一个和上一个链接的背景图像,找到了解决方案。

    这样,我的图片 url 就是我的 css 中设置的相对路径。

    <?php
      $big = 999999999;
    
      echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'next_text' => (''),
        'prev_text' => (''),
        'total' => $query->max_num_pages
    ) );
    ?>
    

    CSS:

    &.next {
      background-color: $transparent;
      background-image: url('images/arrow.svg');
      background-position: center;
      background-repeat: no-repeat;
      height: 1em;
      margin-left: .5em;
      -moz-transform: scaleX(-1);
      -o-transform: scaleX(-1);
      -webkit-transform: scaleX(-1);
      transform: scaleX(-1);
      filter: FlipH;
      -ms-filter: "FlipH";
    }
    
    &.prev {
      background-color: $transparent;
      background-image: url('images/arrow.svg');
      background-position: center;
      background-repeat: no-repeat;
      height: 1em;
      margin-right: .5em;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2018-08-19
      相关资源
      最近更新 更多