在WP_QUERY中为自定义分类设置POSTS_PER_PAGE

时间:2013-02-03 作者:Ahmad Ajmi

我使用下面的代码获取自定义分类术语下的所有页面,它显示了所有页面。我想要的是限制这一点,并且只说出在自定义分类法下发布的最后两页。

posts\\u per\\u page=1在此处不起作用并显示所有页面。

<?php
$post_type = \'page\';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );

echo \'<div id="activities_categories">\';
foreach( $terms as $term ) :

$customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );
$customposts->set(\'posts_per_page\', 1);
if( $customposts->have_posts() ):
    while( $customposts->have_posts() ) : $customposts->the_post();
        echo \'<li>\';
        echo \'<a href="\' . get_permalink() . \'">\' . get_the_title(); 
        echo \'</a></li>\';
    endwhile;

endif;

endforeach;

echo \'</div>\';
endforeach;
 ?>
谢谢

编辑正确的代码以解决问题

<?php
        $post_type = \'page\';

        // Get all the taxonomies for this post type
        $taxonomies = get_object_taxonomies($post_type); // names (default)

        foreach( $taxonomies as $taxonomy ) : 

            // Gets every "category" (term) in this taxonomy to get the respective posts
             $terms = get_terms( $taxonomy ); 

            echo \'<ul>\';
            foreach( $terms as $term ) :

            $args = array(
            \'showposts\' => 1,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => $taxonomy,
                \'terms\' => $term->slug,
                \'field\' => \'slug\'
                )
            )
            );

            $customposts = new WP_Query( $args );

            if( $customposts->have_posts() ):
                while( $customposts->have_posts() ) : $customposts->the_post();
                    echo \'<li>\';
                    echo \'<a href="\' . get_permalink() . \'">\' . get_the_title(); 
                    echo \'</a></li>\';
                endwhile;

            endif;

            endforeach;

            echo \'</ul>\';
        endforeach;
    ?>

2 个回复
最合适的回答,由SO网友:Max Yudin 整理而成

你的WP_Query 形状不正确。看见WP_Query Taxonomy Parameters.

请尝试以下代码:

<?php
$post_type = \'page\';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies($post_type); // names (default)
foreach( $taxonomies as $taxonomy ) : 
    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy ); 
    echo \'<div id="activities_categories">\';
    foreach( $terms as $term ) :
        $customposts = new WP_Query(
           array(
              \'posts_per_page\' => 1,
              \'tax_query\' => array(
                   array(
                       \'taxonomy\' => $taxonomy,
                       \'terms\' => $term->slug,
                       \'field\' => \'slug\'
                   )
              )
           )
        );
        if( $customposts->have_posts() ):
            while( $customposts->have_posts() ) : $customposts->the_post();
                echo \'<li><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></li>\';
            endwhile;
        endif;
    endforeach;
    echo \'</div>\';
endforeach;

SO网友:s_ha_dum

问题不在于你认为它在哪里。运行此(我已更改post_type “发布”以便对我方便,但这不重要):

var_dump(\'begin #################################################\');
$post_type = \'post\';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

  // Gets every "category" (term) in this taxonomy to get the respective posts
  $terms = get_terms( $taxonomy );
  // var_dump(\'raw_terms\',$terms);
  echo \'<div id="activities_categories">\';
  foreach( $terms as $term ) :

    $customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );
    // debuggging
//     var_dump(\'customposts_all\',$customposts); 
//     var_dump(\'customposts_posts\',$customposts->posts); 
    var_dump(\'customposts_count\',count($customposts->posts)); 
    // debugging
    $customposts->set(\'posts_per_page\', 1);
    if( $customposts->have_posts() ):
    while( $customposts->have_posts() ) : $customposts->the_post();
        echo \'<li>\';
        echo \'<a href="\' . get_permalink() . \'">\' . get_the_title(); 
        echo \'</a></li>\';
    endwhile;

    endif;

  endforeach;
  var_dump(\'middle #################################################\');
  echo \'</div>\';
endforeach;
var_dump(\'end #################################################\');
现在,在生成的页面中查找“customposts\\u counts”。每次都有一个结果。

我认为问题更多的是代码的逻辑,以及嵌套循环的逻辑。您正在多次重复相同的条款,至少可能是这样。您必须努力防止这些重复。看看这是否适合您:

var_dump(\'begin #################################################\');
$post_type = \'post\';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type ) );
// var_dump($taxonomies);
$unique_terms = array();
foreach( $taxonomies as $taxonomy ) {
  // Gets every "category" (term) in this taxonomy to get the respective posts
  $terms = get_terms( $taxonomy );
  // var_dump(\'raw_terms\',$terms);
  foreach( $terms as $term ) {
    // var_dump(\'one_term\',$term);
    $unique_terms[$term->term_id] = $term;
  }
}
// var_dump(\'unique_terms\',$unique_terms);

$dupl = array();
foreach ($unique_terms as $t) {
  // var_dump(\'dupl\',$dupl);
  $args = array(
      \'tax_query\' => array(
      \'taxonomy\' => $t->taxonomy,
      \'term\' => $t->slug,
      \'field\' => \'slug\'
    ),
    \'posts_per_page\' => 1
  );
  if (!empty($dupl)) $args[\'post__not_in\'] = $dupl;
  $customposts = new WP_Query($args);
  // var_dump(\'customposts_all\',$customposts); 
  // var_dump(\'customposts_posts\',$customposts->posts); 
  // var_dump(\'customposts_count\',count($customposts->posts)); 
  if( $customposts->have_posts() ) {
    while( $customposts->have_posts() ) {
      echo \'<div id="activities_categories">\';
    $customposts->the_post();
    $dupl[] = $post->ID;
    // var_dump(\'one_post\',$post);
    echo \'<li>\';
    echo \'<a href="\' . get_permalink() . \'">\' . get_the_title(); 
    echo \'</a></li>\';
      echo \'</div>\';
    }
  }
}

var_dump(\'end #################################################\');

结束

相关推荐