Author.php上的WordPress查询不工作

Author.php上的WordPress查询不工作,wordpress,Wordpress,以下查询不适用于我的主题的WordPress author.php模板。它包含在页脚中,页脚在所有页面上都是相同的,除了author.php之外,查询在其他页面上都可以正常工作 <?php if(have_posts()):?> <?php query_posts( array( 'post_type' => 'connect' ) ); while (have_posts()) : the_post(); ?> <div class="title

以下查询不适用于我的主题的WordPress author.php模板。它包含在页脚中,页脚在所有页面上都是相同的,除了author.php之外,查询在其他页面上都可以正常工作

  <?php if(have_posts()):?>
  <?php query_posts( array( 'post_type' => 'connect' ) ); while (have_posts()) : the_post(); ?>
  <div class="title"><?php the_title();?></div>
  <div class="logos">  
  <?php the_content();?>
  </div>
  <?php endwhile;?>
  <?php wp_reset_query(); ?>
  <?php endif;?>
global $post;
$args = array( 'post_type' => 'connect' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); 
   //you can call the_title the_content and any other method that runs under query_posts and WP_Query
endforeach; 


我已经花了一个多小时试图弄清楚到底发生了什么以及为什么这不起作用,但我现在感觉我的前进在与混凝土碰撞。为什么不起作用

尝试改用wp查询

$the_query = new WP_Query();
$the_query->query(array( 'post_type' => 'connect' ));
if ($the_query->have_posts()) : 
while($the_query->have_posts()) : $the_query->the_post();

endwhile; 
endif; 
wp_reset_postdata();
或者,如果wp\u查询不起作用,您也可以尝试使用获取帖子。我很确定这会在author.php中起作用

  <?php if(have_posts()):?>
  <?php query_posts( array( 'post_type' => 'connect' ) ); while (have_posts()) : the_post(); ?>
  <div class="title"><?php the_title();?></div>
  <div class="logos">  
  <?php the_content();?>
  </div>
  <?php endwhile;?>
  <?php wp_reset_query(); ?>
  <?php endif;?>
global $post;
$args = array( 'post_type' => 'connect' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); 
   //you can call the_title the_content and any other method that runs under query_posts and WP_Query
endforeach; 

尝试改用wp查询

$the_query = new WP_Query();
$the_query->query(array( 'post_type' => 'connect' ));
if ($the_query->have_posts()) : 
while($the_query->have_posts()) : $the_query->the_post();

endwhile; 
endif; 
wp_reset_postdata();
或者,如果wp\u查询不起作用,您也可以尝试使用获取帖子。我很确定这会在author.php中起作用

  <?php if(have_posts()):?>
  <?php query_posts( array( 'post_type' => 'connect' ) ); while (have_posts()) : the_post(); ?>
  <div class="title"><?php the_title();?></div>
  <div class="logos">  
  <?php the_content();?>
  </div>
  <?php endwhile;?>
  <?php wp_reset_query(); ?>
  <?php endif;?>
global $post;
$args = array( 'post_type' => 'connect' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); 
   //you can call the_title the_content and any other method that runs under query_posts and WP_Query
endforeach; 

为了让我解释为什么它只在某些页面上工作,您需要了解query_posts()的实际功能

query_posts()修改默认的Wordpress循环。无论您在哪个页面上,都会有一个由核心初始化的默认循环。除非您打算修改该循环,否则停止使用query_posts()是至关重要的

query_posts()经常被误用的原因有很多,在许多论坛以及Wordpress Codex中都有详细介绍。但这正进入一个与你的问题无关的领域

首先,让我们看看您的代码在做什么:

<?php if(have_posts()):?> //If the default loop finds posts....
<?php query_posts( array( 'post_type' => 'connect' ) );?> //Modify the loop to fit these new parameters

如果有帮助,请告诉我。

为了让我解释为什么它只对某些页面起作用,您需要了解query_posts()的实际作用

query_posts()修改默认的Wordpress循环。无论您在哪个页面上,都会有一个由核心初始化的默认循环。除非您打算修改该循环,否则停止使用query_posts()是至关重要的

query_posts()经常被误用的原因有很多,在许多论坛以及Wordpress Codex中都有详细介绍。但这正进入一个与你的问题无关的领域

首先,让我们看看您的代码在做什么:

<?php if(have_posts()):?> //If the default loop finds posts....
<?php query_posts( array( 'post_type' => 'connect' ) );?> //Modify the loop to fit these new parameters

如果有帮助,请告诉我。

谢谢您的详细回答:)我已替换了有问题的查询,并更新了我网站上的其他查询。但是,我仍然可以看到查询在所有其他页面上工作,但在author.php页面上不返回任何内容的行为。在
$q=newwp\u Query…
之后和
if($q->have\u posts())…
之前添加此-
var\u转储($q->posts)-这将告诉您,您的查询是否确实收到了任何帖子。如果没有,请在此处进行注释,我们可以帮助您调试查询并找出原因。@maiorano84-+1以获取详细说明。通常人们会在应该使用的时候使用
query\u posts()
。读一读你的文章肯定会使他们正确。正如我前面提到的,除了author.php之外,上面的精确查询在网站的每个页面上都有效——我相信这是因为在author.php上,WordPress循环通常会查找来自当前作者(你所在页面的作者)的文章。“连接”自定义帖子类型中的页面/帖子不是由该作者创建的,因此,我相信,为什么它不返回该帖子/页面。感谢您的详细回答:)我已替换了相关查询,并更新了我网站上的其他查询。但是,我仍然可以看到查询在所有其他页面上工作,但在author.php页面上不返回任何内容的行为。在
$q=newwp\u Query…
之后和
if($q->have\u posts())…
之前添加此-
var\u转储($q->posts)-这将告诉您,您的查询是否确实收到了任何帖子。如果没有,请在此处进行注释,我们可以帮助您调试查询并找出原因。@maiorano84-+1以获取详细说明。通常人们会在应该使用的时候使用
query\u posts()
。读一读你的文章肯定会使他们正确。正如我前面提到的,除了author.php之外,上面的精确查询在网站的每个页面上都有效——我相信这是因为在author.php上,WordPress循环通常会查找来自当前作者(你所在页面的作者)的文章。“Connect”自定义帖子类型中的页面/帖子不是由该作者创建的,因此,我认为,为什么它不返回帖子/页面。这在author.php上不起作用-它在其他页面上都起作用。这在author.php上不起作用-它在其他页面上都起作用。