Wp查询分页和类别

时间:2018-03-09 10:26:02

标签: wordpress pagination categories archive

我是Wp的新手,我正在尝试开发我的第一个主题。将分页添加到我的归档页面后,类别过滤器不再起作用(可能是因为我覆盖了WP查询)。

我在查询中放置了什么,以便只查看被点击的类别的帖子? (如果我没有把任何东西放到魔法那里),但没有查询,分页就不起作用。

这是archive.php

 private void loginUser(final String email,final String password) {
        String cancel_req_tag = "login";   // Tag used to cancel the request
        progressDialog.setMessage("Logging u in...");
        showDialog();
        StringRequest strReq = new StringRequest(Request.Method.POST, 
    URL_FOR_LOGIN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response:" + response.toString());
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    if (!error) {
                        String user = 
     jObj.getJSONObject("user").getString("name");
                        //launch second activity

                        Intent intent = new Intent(LoginActivity.this, 
    SecondActivity.class);
                        intent.putExtra("username", user);
                        startActivity(intent);
                        finish();
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(), errorMsg, 
    Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login error:" + error.getMessage());
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
// show user error message

      try {
                        if(error.networkResponse!=null) {
                            if (error.networkResponse.data != null) {
                                String responseBody = new String(error.networkResponse.data, "utf-8");
                                JSONObject exception = new JSONObject(responseBody);
                                Toast.makeText(VerificationCodeActivity.this, exception.getString("message"), Toast.LENGTH_SHORT).show();
                            }
                        }


                    } catch ( JSONException e ) {
                        e.printStackTrace();
    //Handle a malformed json response
                    } catch (UnsupportedEncodingException e){
                        e.printStackTrace();
                    }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap <String, String>();       // Posting params to login url
                params.put("email", email);
                params.put("password", password);
                return params;
            }
        };
       // Adding request to request queue
                AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
    }

    private void showDialog() {
        if(!progressDialog.isShowing())
            progressDialog.show();
    }
    private void hideDialog() {
        if(progressDialog.isShowing())
            progressDialog.dismiss();
      }

这是我用于分页的代码:

<?php
    get_header(); ?>

    <?php
    if ( have_posts() ) : ?>

        <header class="page-header no-image">
            <?php
                the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
            ?>
        </header><!-- .page-header -->

        <?php get_template_part( 'template-parts/filter-category' ); ?>

        <?php
         /*QUERY:
         This is the problem if I move the query from there the category works but 
         If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
         */
          $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
          $args = array(
            'post_type'=>'post',
            'post_status'=>'publish',
            'posts_per_page' => 6,
            'paged' => $paged
          );
          $wp_query = new WP_Query( $args );
         /*END QUERY*/
        ?>

        <div id="blogpost-list" class="container">
          <div class="row blogpost-wrapper">
            <?php
            /* Start the Loop */
            $i = 1;
            while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
              <?php if ($i % 3 == 0): ?>
                <div class="blogpost-item-grid">
                    <!-- some stuff to display the post.. doesn't really matter-->
                  <?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
                </div>
              <?php else: ?>
                <div class="blogpost-item-grid">
                    <!-- some stuff to display the post doesn't really matter -->
                  <?php get_template_part( 'template-parts/content-post-preview' ); ?>
                </div>
              <?php endif; ?>
              <?php $i++; ?>
            <?php endwhile;?>
          </div>
        </div>

        <?php get_template_part( 'template-parts/pagination' ); ?>

        <?php
        else :
            get_template_part( 'template-parts/content', 'none' );
        endif; ?>

<?php
get_footer();

解决: 添加此项以获取类别

<?php //Require a wp->query ?>
<div class="container-fluid">
  <div class="pagination-wrapper">
    <?php
    $pag_args = array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $wp_query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-left.svg"/>'),
        'next_text'    => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-right.svg"/>'),
        'add_args'     => false,
        'add_fragment' => '',
    );
    echo paginate_links($pag_args);
    ?>

</div>
</div>

将其传递给查询:

get_header();
$catID = get_queried_object_id();

2 个答案:

答案 0 :(得分:0)

问题是您在实例化后没有使用 $ wp_query 变量。 您的 while循环应如下所示:

while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
   /*Your inner code here*/
<?php endwhile;?>

答案 1 :(得分:0)

如果category.php文件不存在,您可以创建它。然后在你的文件里面

    .file   "code_two.c"
    .section    .rodata.str1.1,"aMS",@progbits,1
.LC2:
    .string "Accumulator is %f\n"
    .section    .text.unlikely,"ax",@progbits
.LCOLDB3:
    .section    .text.startup,"ax",@progbits
.LHOTB3:
    .p2align 4,,15
    .globl  main
    .type   main, @function
main:
.LFB38:
    .cfi_startproc
    pxor    %xmm0, %xmm0
    movl    $300, %ecx
    movsd   .LC1(%rip), %xmm1
.L2:
    movl    $300, %esi
.L8:
    xorl    %edx, %edx
    .p2align 4,,10
    .p2align 3
.L6:
    movq    %rdx, %rax
    .p2align 4,,10
    .p2align 3
.L3:
    addq    $1, %rax
    addsd   %xmm1, %xmm0
    cmpq    $300, %rax
    jne .L3
    addq    $1, %rdx
    cmpq    $300, %rdx
    jne .L6
    subq    $1, %rsi
    jne .L8
    subq    $1, %rcx
    jne .L2
    subq    $8, %rsp
    .cfi_def_cfa_offset 16
    movl    $.LC2, %esi
    movl    $1, %edi
    movl    $1, %eax
    call    __printf_chk
    xorl    %eax, %eax
    addq    $8, %rsp
    .cfi_def_cfa_offset 8
    ret
    .cfi_endproc
.LFE38:
    .size   main, .-main
    .section    .text.unlikely
.LCOLDE3:
    .section    .text.startup
.LHOTE3:
    .section    .rodata.cst8,"aM",@progbits,8
    .align 8
.LC1:
    .long   0
    .long   1072693248
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits