Wordpress教程 2023年06月25日
0 收藏 0 点赞 310 浏览 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

相关推荐
宝塔面板里mysql经常自动停止怎么办?
宝塔面板里mysql经常自动停止怎么办? 随着服务器里面的项目越来越多,会发现宝塔面板里mysql经常自动停止。通常这个是因为服务器内存不足…
日期:2023-08-06 点赞:0 阅读:38
更换WordPress网站的域名原始图片地址保持不变
将以上代码添加到你的主题的functions.php文件中,并将$old_domain和$new_domain的值替换为你的旧域名和新域名。…
日期:2023-07-18 点赞:0 阅读:995
PHP抓取新浪博客文章内容代码
<?php /** * 新浪博客文章内容解析 */ header("content-type:application/json; ch…
日期:2023-07-14 点赞:0 阅读:707
wp_list_bookmarks()函数删除li标签示例代码【友情链接】
可以使用wp_list_bookmarks()函数的before和after参数。这是更新后的代码: <?php $args = ar…
日期:2023-06-26 点赞:0 阅读:947
WordPress主题分类列表中显示当前分类下的置顶文章,并在下面显示最新发布的文章
在这个代码中,我们首先获取当前分类的ID ($cat_id) 和所有置顶文章的ID数组 ($sticky_posts)。 然后,我们使用 W…
日期:2023-06-25 点赞:0 阅读:310
自动为文章添加相关关键词标签
我们使用的是在当前主题 functions.php 文件中添加代码 <?php function wp_aatags_html2tex…
日期:2023-06-24 点赞:0 阅读:566
发表评论
暂无评论

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