也想出现在这里?联系我们

WordPress 调用最新、随机、热门、指定分类代码汇总

2024.03.19 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

Sure, I can provide you with the code snippets for calling the latest, random, popular, and specific category posts in WordPress.

  1. Latest Posts:
    To display the latest posts, you can use the following code:

    
    <?php
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    );

$latest_posts = new WP_Query($args);

if ($latest_posts>have_posts()) :
while ($latest_posts>have_posts()) : $latest_posts>the_post();
// Display post content or title as needed
the_title();
endwhile;
endif;
?>


2. Random Posts:
To display random posts, you can use the following code:
```php
<?php
$args = array(
    'post_type' => 'post',
    'orderby' => 'rand',
    'posts_per_page' => 5,
);

$random_posts = new WP_Query($args);

if ($random_posts>have_posts()) :
    while ($random_posts>have_posts()) : $random_posts>the_post();
        // Display post content or title as needed
        the_title();
    endwhile;
endif;
?>
  1. Popular/Popular Posts:
    Displaying popular posts typically requires a plugin or some sort of tracking mechanism to determine post popularity. You can use plugins like "Jetpack" or "Popular Posts Widget" to achieve this functionality.

  2. Posts from a Specific Category:
    To display posts from a specific category, you can use the following code:

    
    <?php
    $args = array(
    'post_type' => 'post',
    'cat' => 42, // Replace with your category ID
    'posts_per_page' => 5,
    );

$category_posts = new WP_Query($args);

if ($category_posts>have_posts()) :
while ($category_posts>have_posts()) : $category_posts>the_post();
// Display post content or title as needed
the_title();
endwhile;
endif;
?>


Replace 42 with the ID of the desired category.

Make sure to customize these snippets according to your specific requirements and theme structure.
有用4
  • 2024.03.19初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧