Wordpress教程 2023年06月25日
0 收藏 0 点赞 810 浏览 1606 个字

在这个代码中,我们首先获取当前分类的ID ($cat_id) 和所有置顶文章的ID数组 ($sticky_posts)。

然后,我们使用 WP_Query 分别查询当前分类下的置顶文章和最新发布的文章。对于置顶文章,我们设置查询参数,包括 post__in 用于指定置顶文章ID数组,category__in 用于限制只查询当前分类,以及 ignore_sticky_posts 设置为 1,忽略其他位置的置顶文章。

对于最新发布的文章,我们使用分类ID ($cat_id) 并排除置顶文章的ID数组 ($sticky_posts),以确保只显示最新发布的非置顶文章。

最后,我们使用默认的 have_posts()the_post() 循环来显示置顶文章和最新发布的文章。

请确保将上述代码嵌入到适当的WordPress主题文件中,并根据需要自定义HTML和样式。

<?php
$cat_id = get_queried_object_id();
$sticky_posts = get_option('sticky_posts');

// 查询当前分类下的置顶文章
$sticky_args = array(
  'post__in' => $sticky_posts,
  'category__in' => array($cat_id),
  'ignore_sticky_posts' => 1
);
$sticky_query = new WP_Query($sticky_args);

// 查询当前分类下的最新发布文章
$new_posts_args = array(
  'cat' => $cat_id,
  'post__not_in' => $sticky_posts
);
$new_posts_query = new WP_Query($new_posts_args);

// 显示置顶文章
if ($sticky_query->have_posts()) :
  while ($sticky_query->have_posts()) : $sticky_query->the_post();
    ?>
    <li>
      <div class="pbox"><a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=500&w=500&zc=1" border="0" alt="<?php the_title(); ?>"></a></div>
      <p><a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a></p>
    </li>
    <?php
  endwhile;
  wp_reset_postdata();
endif;

// 显示最新发布的文章
if ($new_posts_query->have_posts()) :
  while ($new_posts_query->have_posts()) : $new_posts_query->the_post();
    ?>
    <li>
      <div class="pbox"><a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=500&w=500&zc=1" border="0" alt="<?php the_title(); ?>"></a></div>
      <p><a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a></p>
    </li>
    <?php
  endwhile;
  wp_reset_postdata();
endif;
?>

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2550.html

相关推荐
WordPress去除/禁止后台侧栏插件-主题-版本更新提示
今天使用了张戈修改的nginx-help插件修改版,官方版并无自动重启功能,因此不能更新。但是这WordPress后台一直有更新提示,这可真…
日期:2024-12-30 点赞:0 阅读:26
WordPress网站js防扒代码-禁止右键/F12/调试自动关闭窗口
网站防扒老生常谈了,今天老白博客@老白也给大家分享一下从网上看到的“WordPress网站防扒代码/禁止F12调试教程-以7b2主题为例” …
日期:2024-12-30 点赞:0 阅读:36
WordPress优化加速之导航菜单栏缓存Menu Cache
今天老白博客@老白给大家分享又一个WordPress优化加速教程:“导航菜单栏缓存WordPress插件Menu Cache”。我们都知道数…
日期:2024-12-30 点赞:0 阅读:27
2024基于网站性能测试的WordPress优化指南
老白博客WordPress优化高级教程:在如今5G遍地走,千兆多如狗的高速网络时代,如果你访问网站,还出现转圈圈的情况,那么你十有八九是要关…
日期:2024-12-30 点赞:0 阅读:31
WP网站用户行为监测回放插件nicen-replay
在数字营销的世界里,了解用户行为是提升用户体验和转化率的关键。老白推荐一款炫酷的WordPress插件:nicen-replay 。它能够让…
日期:2024-12-30 点赞:0 阅读:31
2024新WordPress缓存插件atec Cache APCu
WordPress插件atec Cache APCu 是一款2024年才投入使用的的高效缓存插件,与其他内存缓存选项(如 Redis 和 M…
日期:2024-12-30 点赞:0 阅读:38
发表评论
暂无评论

还没有评论呢,快来抢沙发~