Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从最新帖子提要wordpress中仅排除一个类别中的第一篇帖子_Php_Wordpress_Content Management System_Article_Featured - Fatal编程技术网

Php 从最新帖子提要wordpress中仅排除一个类别中的第一篇帖子

Php 从最新帖子提要wordpress中仅排除一个类别中的第一篇帖子,php,wordpress,content-management-system,article,featured,Php,Wordpress,Content Management System,Article,Featured,我管理一个运行Wordpress的网站()。正如你在头版看到的,我在页面顶部有一篇“主要/特色”文章,展示了特定类别的最新文章。在它下面,我有一个主循环,显示所有类别的最新帖子 但正如你从标题中看到的,当一篇文章被选为顶部特色空间中的位置时,它也会显示在最新的文章提要中 我的问题是,正如我的标题所说:我如何才能排除某一类别中最新/最新的帖子和所有最新的帖子出现在一起 我知道我可以在一段时间后通过更改类别之类的方式手动控制,但我希望它是自动完成的,我不知道怎么做 希望您能抽出一些时间来帮助我:)启

我管理一个运行Wordpress的网站()。正如你在头版看到的,我在页面顶部有一篇“主要/特色”文章,展示了特定类别的最新文章。在它下面,我有一个主循环,显示所有类别的最新帖子

但正如你从标题中看到的,当一篇文章被选为顶部特色空间中的位置时,它也会显示在最新的文章提要中

我的问题是,正如我的标题所说:我如何才能排除某一类别中最新/最新的帖子和所有最新的帖子出现在一起

我知道我可以在一段时间后通过更改类别之类的方式手动控制,但我希望它是自动完成的,我不知道怎么做


希望您能抽出一些时间来帮助我:)

启动一个变量并检查循环内部。一个简单的方法:

$i=0;

while(have_posts() == true)
{
 ++$i;
 if($i==1) //first post
  continue;

 // Rest of the code
}

启动一个变量并检查循环内部。一个简单的方法:

$i=0;

while(have_posts() == true)
{
 ++$i;
 if($i==1) //first post
  continue;

 // Rest of the code
}

您需要更新模板的逻辑,以便主循环跳过输出顶部输出的post

如果看不到您的模板代码,就很难具体说明,但类似的方法可能会奏效:

在顶部部分,保存要输出的帖子的ID:

$exclude_post_id = get_the_ID();
如果您需要直接获取给定类别中最新帖子的ID,而不是在循环过程中保存它,您可以使用以下方法执行此操作:

然后,在主循环中,选择alter以排除该post:

query_posts(array('post__not_in'=>$exclude_post_id));
或者手动将其排除在循环中,类似这样:

if (have_posts()): 
    while (have_posts()):
        the_post();
        if ($post->ID == $exclude_post_id) continue;
        the_content();
    endwhile;
 endif;

更多信息,以及。

您需要更新模板的逻辑,以便主循环跳过输出顶部输出的post

如果看不到您的模板代码,就很难具体说明,但类似的方法可能会奏效:

在顶部部分,保存要输出的帖子的ID:

$exclude_post_id = get_the_ID();
如果您需要直接获取给定类别中最新帖子的ID,而不是在循环过程中保存它,您可以使用以下方法执行此操作:

然后,在主循环中,选择alter以排除该post:

query_posts(array('post__not_in'=>$exclude_post_id));
或者手动将其排除在循环中,类似这样:

if (have_posts()): 
    while (have_posts()):
        the_post();
        if ($post->ID == $exclude_post_id) continue;
        the_content();
    endwhile;
 endif;
更多信息,以及。

,供您使用

query_posts('offset=1');
有关详细信息:

您可以使用

query_posts('offset=1');

有关更多信息:

方法-1

$cat_posts = new WP_Query('posts_per_page=1&cat=2'); //first 1 posts
while($cat_posts->have_posts()) { 
   $cat_posts->the_post(); 
   $do_not_duplicate[] = $post->ID;
}

//Then check this if exist in an array before display the posts as following.
 if (have_posts()) {
    while (have_posts()) {

    if (in_array($post->ID, $do_not_duplicate)) continue; // check if exist first post

     the_post_thumbnail('medium-thumb'); 

         the_title();

    } // end while
}
方法-2

query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();
此查询告诉循环仅显示最近的第一篇文章之后的5篇文章。这段代码中最重要的部分是“偏移量”,而这个神奇的单词正在完成整个过程


更多详细信息

方法-1

$cat_posts = new WP_Query('posts_per_page=1&cat=2'); //first 1 posts
while($cat_posts->have_posts()) { 
   $cat_posts->the_post(); 
   $do_not_duplicate[] = $post->ID;
}

//Then check this if exist in an array before display the posts as following.
 if (have_posts()) {
    while (have_posts()) {

    if (in_array($post->ID, $do_not_duplicate)) continue; // check if exist first post

     the_post_thumbnail('medium-thumb'); 

         the_title();

    } // end while
}
方法-2

query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();
此查询告诉循环仅显示最近的第一篇文章之后的5篇文章。这段代码中最重要的部分是“偏移量”,而这个神奇的单词正在完成整个过程


更多详细信息

这里有一个函数,它可以:

function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}

用法:假设我的类别id为22,则:

$last_post_ID = get_lastest_post_of_category(22);

您还可以将类别数组传递给此函数。

以下是一个函数,它可以执行以下操作:

function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}

用法:假设我的类别id为22,则:

$last_post_ID = get_lastest_post_of_category(22);

您还可以将类别数组传递给此函数。

从最近五篇文章中排除第一个类别

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'Past_Category_Name',
      'posts_per_page' => 5,
              'offset' => 1
   )); 
?>

从最近五篇文章中排除第一篇

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'Past_Category_Name',
      'posts_per_page' => 5,
              'offset' => 1
   )); 
?>


谢谢,但是我如何确保latest posts循环始终检查$top\u post\u id始终包含某个类别的最新帖子的id?我假设您的latest posts循环已经工作了-如果是这样,那么您不必-只需保存get\u the\u id()返回的值即可或者$post->ID在输出顶部帖子的特色帖子循环中-这是您正在输出的帖子的ID,也是您稍后要排除的帖子的ID。基本上,当您在顶部输出文章时,保存其ID,然后稍后排除此ID。问题是,特色文章是使用(get_template_part('includes/feat slider')从不同的模板获取的,因此特色文章循环与最新的文章循环不在同一个文件中。因此,我想知道是否有办法始终获取某个类别中最新帖子的ID。我已更新了答案,以显示如果在循环过程中无法保存,如何直接获取给定类别中最新帖子的ID。非常感谢!我真的很感激!谢谢。谢谢,但是我如何确保latest posts循环始终检查$top_post_id始终包含某个类别中最新帖子的id?我假设您的latest posts循环已经工作了-如果是这样,那么您不必-只需保存get_the_id()返回的值或者$post->ID在输出顶部帖子的特色帖子循环中-这是您正在输出的帖子的ID,也是您稍后要排除的帖子的ID。基本上,当您在顶部输出文章时,保存其ID,然后稍后排除此ID。问题是,特色文章是使用(get_template_part('includes/feat slider')从不同的模板获取的,因此特色文章循环与最新的文章循环不在同一个文件中。因此,我想知道是否有办法始终获取某个类别中最新帖子的ID。我已更新了答案,以显示如果在循环过程中无法保存,如何直接获取给定类别中最新帖子的ID。非常感谢!我真的很感激!非常感谢。