WordPress 调用最新、随机、热门、指定分类代码汇总
Sure, I can provide you with the code snippets for calling the latest, random, popular, and specific category posts in WordPress.
- 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;
?>
-
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. -
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.
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。