免插件呼叫最新文章 是我們在進行wordpress改造開發時常常面對的功能,搜集了網上幾種常用的方法,當一個頁面既有最新文章又有置頂文章時,我們要考慮在最新文章串列里排除掉置頂文章,
一、最簡單的方法wp_get_archvies
WordPress最新文章的呼叫可以使用一行很簡單的模板標簽wp_get_archvies來實作
<?php get_archives(‘postbypost’, 10); ?> (顯示10篇最新更新文章)
或者
<?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?>
type=postbypost:按最新文章排列
limit:限制文章數量最新20篇
format=custom:用來自定義這份文章串列的顯示樣式(fromat=custom也可以不要,默認以UL串列顯示文章標題,)
二、query_posts()函式
通過WP的query_posts()函式也能呼叫最新文章串列, 雖然代碼會比較多一點,但可以更好的控制Loop的顯示,比如你可以設定是否顯示摘要,具體的使用方法也可以查看官方的說明,
呼叫最新文章:(直接在想要呈現的位置放上以下代碼即可)
<li>
<h2>最新文章</h2>
<?php query_posts('showposts=6&cat=-111'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>
</li>
讀取6篇文章,排除分類ID為111里面的文章
三、推薦WP_Query函式
<ul>
<?php $post_query = new WP_Query(‘showposts=10’);
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><a href=https://blog.csdn.net/zcp528/article/details/”<?php the_permalink(); ?>”><?php the_title(); ?>
<?php endwhile;?>
四、推薦get_results()函式
<ul>
<?php $result = $wpdb->get_results(“SELECT ID,post_title FROM $wpdb->posts where post_status=’publish’ and post_type=’post’ ORDER BY ID DESC LIMIT 0 , 10″);
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
?>
<li><a href=https://blog.csdn.net/zcp528/article/details/”<?php echo get_permalink($postid); ?>” title=”<?php echo $title ?>”><?php echo $title ?>
<?php } ?>
五、最新文章中排除置頂文章
<h2>最新文章</h2>
<ul>
<?php
$recentPosts = new WP_Query(array('post__not_in' => get_option('sticky_posts')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li> <a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink() ?>" rel="bookmark" ><? echo get_the_title(); ?></a><?php the_time('m/d'); ?></li>
<?php endwhile; wp_reset_query();?>
</ul>
六、小結
1、使用get_results()函式最快
2、推薦使用WP_Query()函式,靈活好控制,這也是官網推薦的函式
您可能感興趣的文章:
? wordpress獲取某個分類目錄下文章數目的五種方法
? Youpzt-optimizer插件你的WordPress網站優化利器
? 使用Better WordPress Minify合并壓縮WordPress的js和css 檔案
? mod_rewrite快取模式WP Super Cache最快的加速模式
? 圖片類Wordpress網站必備回應式燈箱插件WF Magnific Lightbox
? 自定義wordpress登陸界面全屏漸變圖片輪播
? wordpress頁面生成二維碼
? 百度官方插件sitemap和Baidu Sitemap區別和使用
? wpdb和get_results讀取資料庫打造個性友情鏈接教程
? wordpress統計某個標簽下的文章總數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/4831.html
標籤:其他
上一篇:【CSS3】常用的選擇器
