简体   繁体   English

仅从最新的帖子Feed,wordpress中排除一个类别的第一篇帖子

[英]Exclude only first post from one category from the latest posts feed, wordpress

I administrate a website( www.teknologia.no ) running Wordpress. 我管理运行Wordpress的网站( www.teknologia.no )。 As you can see on the front page I have a "main/featured" article on the top of the page showing the latest post from a specific category. 正如您在首页上看到的,我在页面顶部有一篇“主要/特色”文章,显示了特定类别的最新帖子。 And beneath it I have the main loop showing all the latest posts from all categories. 在它下面我有主循环显示所有类别的所有最新帖子。

But as you can see and read from the title, when a posts is chosen to be places in the featured space on the top, it is also shown in the latest posts feed. 但是,正如您可以从标题中看到和阅读的那样,当选择帖子作为顶部特色空间中的位置时,它也会显示在最新的帖子中。

My question is as my title say: How can I exclude the newest/latest post in a certain category from appearing with the all the latests posts. 我的问题是我的标题:我如何排除特定类别中的最新/最新帖子与所有最新帖子一起出现。

I know I can manually control this by changing the categories after a while and such, but I want it to be done automatically, and I don't know how. 我知道我可以通过一段时间之后改变类别来手动控制这个,但我想让它自动完成,我不知道如何。

Hope you can spare some time and help me :) 希望你能抽出一些时间帮助我:)

You would need to update the template's logic so that the main loop skips outputting the post that's output at the top. 您需要更新模板的逻辑,以便主循环跳过输出顶部输出的帖子。

Without seeing your template code, it's hard to be specific, but something like this would probably work: 没有看到你的模板代码,很难具体,但这样的事情可能会起作用:

In the top section, save the ID of the post that you're outputting: 在顶部,保存您要输出的帖子的ID:

$exclude_post_id = get_the_ID();

If you need to directly fetch the ID of the latest post in a given category, rather than saving it during the loop, you can do it like this instead, using WP_Query : 如果您需要直接获取给定类别中最新帖子的ID,而不是在循环期间保存它,您可以使用WP_Query这样做:

$my_query = new WP_Query('category_name=my_category_name&showposts=1');
while ($my_query->have_posts()):
    $my_query->next_post();
    $exclude_post_id = $my_query->post->ID;
endwhile;

Then, in the main loop, either alter the query to exclude that post: 然后,在主循环中,更改查询以排除该帖子:

query_posts(array('post__not_in'=>$exclude_post_id));

or manually exclude it inside the loop, something like this: 或者在循环内手动排除它,如下所示:

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

More information here , here and here . 更多信息, 这里这里这里

here is a function that does just that: 这是一个功能就是这样:

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;

} }

Usage: say my category id is 22 then: 用法:说我的类别ID是22然后:

$last_post_ID = get_lastest_post_of_category(22);

you can also pass an array of categories to this function. 您还可以将一组类别传递给此函数。

Initiate a variable and check inside your loop. 启动变量并检查循环内部。 A simple way: 一个简单的方法:

$i=0;

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

 // Rest of the code
}

for that you can use 为此你可以使用

query_posts('offset=1');

for more info : blog 了解更多信息: 博客

Method - 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
}

Method - 2 方法 - 2

query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();

This query is telling the loop to only display 5 posts which follow the most recent first post. 此查询告诉循环仅显示跟随最近的第一篇帖子的5个帖子。 The important part in this code is “offset” and this magic word is doing the whole thing. 这段代码中的重要部分是“偏移” ,这个神奇的词语正在做整件事。

More details from Here 来自Here的更多细节

Exclude first one From latest five posts 从最近的五个帖子中排除第一个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM