我有一个在所选类别中发帖的功能,除最新帖子外似乎有效。
最新的帖子不会显示,只显示一旦我发布了一个新帖子,那么之前会显示...?
<?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'
参数为null或者将show all清空则设置它。
<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>
答案 0 :(得分:1)
答案 1 :(得分:1)
最简单的类别可以是这样的
$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();
您可以根据您的要求增加或减少参数