Php 通过WordPress中的选定类别获取帖子查询
我有一个功能,可以获取所选类别中的帖子,除了最新的帖子外,它似乎还能工作 最新的帖子不会显示,它只显示我发布一篇新帖子后,上一篇就会显示Php 通过WordPress中的选定类别获取帖子查询,php,wordpress,Php,Wordpress,我有一个功能,可以获取所选类别中的帖子,除了最新的帖子外,它似乎还能工作 最新的帖子不会显示,它只显示我发布一篇新帖子后,上一篇就会显示 <?php global $post; global $categoryName; //access it through a global variable. $myposts = get_posts(array( 'posts_per_page' => 5, 'offset' => 1, 'category' =&
<?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>';
?>
您正在将
offset
1传递给您的get\u posts
呼叫
因此,您只需将结果减一,并跳过最新的帖子
去掉偏移量,你就会好起来。最简单的分类方法就是这样
$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();
您可以根据需要增加或减少参数
$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();