通过WordPress中的所选类别获取帖子查询
问题描述:
我有一个函数可以在所选类别中获取帖子,它似乎可以工作,但最新帖子除外。通过WordPress中的所选类别获取帖子查询
最新的帖子不会显示,它只显示一次我发布一个新的帖子,然后以前会显示...?
<?php
global $post;
global $categoryName; //access it through a global variable.
$myposts = get_posts(array(
'posts_per_page' => 5,
'offset' => 1,
'category' => 3 // set cat here
));
echo '<div class="recent-post">';
if ($myposts) {
foreach ($myposts as $post) :
setup_postdata($post);
?>
<a class="col-xs-3 col-md-2" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<div class="col-xs-3 col-md-10">
<a class="" href="<?php the_permalink(); ?>">
<h2><?php the_title(); ?></h2>
</a>
<?php the_excerpt() ?>
<a href="<?php the_permalink(); ?>">Read Full</a>
<br>
<a class="" href="<?php comments_link(); ?>">
<?php comments_number('0 Comments', '1 Comments', '% Comments'); ?>.
</a>
</div>
<hr/>
<?php
endforeach;
wp_reset_postdata();
}
echo '</div>';
?>
更重要的是,我有一个功能,是清洁的,如果这个功能,可以修改为包括mypost
阵列,并有如果'category'
参数为空或全部清空显示其设置。
<div class="recent-post">
<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5');
while ($popular->have_posts()) : $popular->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<br>
<a href="<?php comments_link(); ?>">
<?php comments_number('0 Comments', '1 Comments', '% Comments'); ?>.
</a>
<?php the_excerpt() ?>
<a href="<?php the_permalink(); ?>">Read More</a>
<hr/>
<?php endwhile; ?>
</div>
答
最简单的按类别获得可能是这样
$type = $myseriesoption;
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
// your code
}
else
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => 500,
'post_status' => 'any',
'post_parent' => null
));
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
wp_reset_postdata();
}
}
wp_reset_query();
您可以根据您的要求增加或减少参数
只是固定它在发布信息之前1分钟,谢谢 –