展示您最近發布的內容是改善幾乎所有網站性能指標的最簡單方法之一。
需要知道的是,您可以在帖子和頁腳之後的側邊欄等高流量區域中顯示最近發布的內容。這可確保您最大限度地提高內容的可見性,並獲得發布內容的所有好處。在本文中,我將向您展示如何創建最近的帖子插件,以簡化在您的網站上展示最近帖子的過程。
註冊插件
我將首先在網站上註冊插件。註冊後,該插件將顯示在網站的插件部分。
/ *
插件名稱:最近的帖子小部件擴展
說明:此插件創建一個小部件,用於在前端顯示最近的帖子。
版本:1.X
作者:WordPress最近的帖子小工具
* /
類RecentPostsWithExcerpts擴展WP_Widget {
function __construct(){
$ widget_ops = array('classname'=>'recent_with_excerpt','description'=> __('你最近的帖子,帶有可選的摘錄','recent_posts_with_excerpts'));
parent :: __ construct('RecentPostsWithExcerpts',__('Recent Posts with Excerpts','recent_posts_with_excerpts'),$ widget_ops);
}
1 |
/ * * / 類RecentPostsWithExcerpts擴展WP_Widget { function __construct(){ $ widget_ops = array('classname'=>'recent_with_excerpt','description'=> __('你最近的帖子,帶有可選的摘錄','recent_posts_with_excerpts')); parent :: __ construct('RecentPostsWithExcerpts',__('Recent Posts with Excerpts','recent_posts_with_excerpts'),$ widget_ops); } |
在上面的代碼片段中,RecentPostsWithExprects()函數註冊了該插件。因此,這就是插件條目在插件列表中的顯示方式:
小工具的代碼
以下是執行多個操作的代碼段。首先,它創建將出現在前端的小部件。接下來,該代碼段將檢索最新發布的博客。完成此操作後,博客的摘錄(內置的和高級摘錄)將通過循環顯示(請參閱代碼段中的注釋)。完成此操作後,函數wp_reset_query()將恢復原始主查詢的$ wp_query和全局發布數據。
函數小部件($ args,$ instance){
global $ before_widget,$ instance;
extract($ args);
$ title = apply_filters('widget_title',$ instance('title'));
echo $ before_widget,$ title;
$ ul_classes ='recent_posts_with_excerpts';
$ ul_classes = apply_filters('recent_posts_with_excerpts_list_classes',$ ul_classes);
if(!empty($ ul_classes))
$ ul_classes ='class =「'。$ ul_classes。'」';
$ li_classes ='';
$ li_classes = apply_filters('recent_posts_with_excerpts_item_classes',$ li_classes);
if(!empty($ li_classes))
$ li_classes ='class =「'。$ li_classes。'」';
$ h2_classes ='recent_posts_with_excerpts';
$ h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes',$ h2_classes);
if(!empty($ h2_classes))
$ h2_classes ='class =「'。$ h2_classes。'」';
do_action( 'recent_posts_with_excerpts_begin');
回聲'
- 「;
- 「;
回聲''.get_the_title()'。
「;
if(!empty($ date))
回聲''.get_the_time($日期)。'
「;
{//顯示摘錄
?><a href =「「>
//檢索最後n篇博文
$ q = array('posts_per_page'=> $ instance('numposts'));
if(!empty($ instance('tag')))
$ q('tag')= $ instance('tag');
$ q = apply_filters('recent_posts_with_excerpts_query',$ q,$ instance);
$ rpwe = new wp_query($ q);
//循環
if($ rpwe-> have_posts()):
while($ rpwe-> have_posts()):$ rpwe-> the_post();
回聲'
<?PHP
do_action( 'recent_posts_with_excerpts_end');
wp_reset_query();
}
1 |
函數小部件($ args,$ instance){ global $ before_widget,$ instance; extract($ args); $ title = apply_filters('widget_title',$ instance('title')); echo $ before_widget,$ title; $ ul_classes ='recent_posts_with_excerpts'; $ ul_classes = apply_filters('recent_posts_with_excerpts_list_classes',$ ul_classes); if(!empty($ ul_classes)) $ ul_classes ='class =「'。$ ul_classes。'」'; $ li_classes =''; $ li_classes = apply_filters('recent_posts_with_excerpts_item_classes',$ li_classes); if(!empty($ li_classes)) $ li_classes ='class =「'。$ li_classes。'」'; $ h2_classes ='recent_posts_with_excerpts'; $ h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes',$ h2_classes); if(!empty($ h2_classes)) $ h2_classes ='class =「'。$ h2_classes。'」'; do_action( 'recent_posts_with_excerpts_begin'); 回聲'
//檢索最後n篇博文 $ q = array('posts_per_page'=> $ instance('numposts')); if(!empty($ instance('tag'))) $ q('tag')= $ instance('tag'); $ q = apply_filters('recent_posts_with_excerpts_query',$ q,$ instance); $ rpwe = new wp_query($ q); //循環 if($ rpwe-> have_posts()): while($ rpwe-> have_posts()):$ rpwe-> the_post(); 回聲'
<?PHP do_action( 'recent_posts_with_excerpts_end'); wp_reset_query(); } |
小部件放置的表單
下一個代碼段生成用戶在將小部件放在前端時將看到的表單。此代碼段創建了一個簡單的表單,用戶可以填寫該表單以將小部件放在網站上。
函數形式($ instance){
if(get_option('show_on_front')=='page')
$ link = get_permalink(get_option('page_for_posts'));
其他
$ link = home_url();
//默認
$ instance = wp_parse_args((array)$ instance,array(
'title'=> __('Recent Posts','recent_posts_with_excerpts'),
'numposts'=> 5,
'numexcerpts'=> 5,
'date'=> get_option('date_format'),
'more_text'=> __('Read More','recent_posts_with_excerpts'),
'words'=>'25',
'tag'=>'',
));
?>
<label for =「get_field_id( '標題'); ?>「>
<input class =「widefat」id =「get_field_id( '標題'); ?>「name =」get_field_name( '標題'); ?>「type =」text「value =」「/>
<label for =「get_field_id( 'numposts'); ?>「>
<input class =「widefat」id =「get_field_id( 'numposts'); ?>「name =」get_field_name( 'numposts'); ?>「type =」text「value =」「/>
<label for =「get_field_id( 'more_text'); ?>「>
<input class =「widefat」id =「get_field_id( 'more_text'); ?>「name =」get_field_name( 'more_text'); ?>「type =」text「value =」「/>
<無線電通信
<label for =「get_field_id( '單詞'); ?>「>
<input class =「widefat」id =「get_field_id( '單詞'); ?>「name =」get_field_name( '單詞'); ?>「type =」text「value =」「/>
<label for =「get_field_id( '標籤'); ?>「>
<input class =「widefat」id =「get_field_id( '標籤'); ?>「name =」get_field_name( '標籤'); ?>「type =」text「value =」「/>
<?PHP
}
1 |
函數形式($ instance){ if(get_option('show_on_front')=='page') $ link = get_permalink(get_option('page_for_posts')); 其他 $ link = home_url(); //默認 $ instance = wp_parse_args((array)$ instance,array( 'title'=> __('Recent Posts','recent_posts_with_excerpts'), 'numposts'=> 5, 'numexcerpts'=> 5, 'date'=> get_option('date_format'), 'more_text'=> __('Read More','recent_posts_with_excerpts'), 'words'=>'25', 'tag'=>'', )); ?> <label for =「get_field_id( '標題'); ?>「> <input class =「widefat」id =「get_field_id( '標題'); ?>「name =」get_field_name( '標題'); ?>「type =」text「value =」「/> <label for =「get_field_id( 'numposts'); ?>「> <input class =「widefat」id =「get_field_id( 'numposts'); ?>「name =」get_field_name( 'numposts'); ?>「type =」text「value =」「/> <label for =「get_field_id( 'more_text'); ?>「> <input class =「widefat」id =「get_field_id( 'more_text'); ?>「name =」get_field_name( 'more_text'); ?>「type =」text「value =」「/> <無線電通信 <label for =「get_field_id( '單詞'); ?>「> <input class =「widefat」id =「get_field_id( '單詞'); ?>「name =」get_field_name( '單詞'); ?>「type =」text「value =」「/> <label for =「get_field_id( '標籤'); ?>「> <input class =「widefat」id =「get_field_id( '標籤'); ?>「name =」get_field_name( '標籤'); ?>「type =」text「value =」「/>
<?PHP } |
以下是窗口小部件的表單在行動中的樣子:
如何設置和使用Recent Post插件
首先,創建名稱為「recent-post-widget-extended」的文件夾。在此文件夾中,創建名稱為「recent-post-widget-extended.php」的文件。激活插件比在小部件區域中顯示的插件,如圖2所示。
要設置插件並使其可操作,請在WordPress插件文件夾中創建名為recent-post-widget-extended的文件夾。在此文件夾中,創建一個文件並將其命名為recent-post-widget-extended.php。將插件的代碼粘貼到此文件中並保存。
接下來,激活插件,您就可以在WordPress儀錶板的插件部分看到它。
準備就緒後,您可以將小部件插入WordPress網站前端的任何位置
部署後,以下是窗口小部件在前端的外觀:
插件的完整代碼
'recent_with_excerpt','description'=> __('你最近的帖子,帶有可選的摘錄','recent_posts_with_excerpts'));
parent :: __ construct('RecentPostsWithExcerpts',__('Recent Posts with Excerpts','recent_posts_with_excerpts'),$ widget_ops);
}
函數小部件($ args,$ instance){
global $ before_widget,$ intance;
extract($ args);
$ title = apply_filters('widget_title',$ instance('title'));
echo $ before_widget,$ title;
$ ul_classes ='recent_posts_with_excerpts';
$ ul_classes = apply_filters('recent_posts_with_excerpts_list_classes',$ ul_classes);
if(!empty($ ul_classes))
$ ul_classes ='class =「'。$ ul_classes。'」';
$ li_classes ='';
$ li_classes = apply_filters('recent_posts_with_excerpts_item_classes',$ li_classes);
if(!empty($ li_classes))
$ li_classes ='class =「'。$ li_classes。'」';
$ h2_classes ='recent_posts_with_excerpts';
$ h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes',$ h2_classes);
if(!empty($ h2_classes))
$ h2_classes ='class =「'。$ h2_classes。'」';
do_action( 'recent_posts_with_excerpts_begin');
回聲'
- 「;
- 「;
回聲''.get_the_title()'。
「;
if(!empty($ date))
回聲''.get_the_time($日期)。'
「;
{//顯示摘錄
?><a href =「「>
//檢索最後n篇博文
$ q = array('posts_per_page'=> $ instance('numposts'));
if(!empty($ instance('tag')))
$ q('tag')= $ instance('tag');
$ q = apply_filters('recent_posts_with_excerpts_query',$ q,$ intance);
$ rpwe = new wp_query($ q);
//循環
if($ rpwe-> have_posts()):
while($ rpwe-> have_posts()):$ rpwe-> the_post();
回聲'
__('Recent Posts','recent_posts_with_excerpts'),
'numposts'=> 5,
'numexcerpts'=> 5,
'date'=> get_option('date_format'),
'more_text'=> __('Read More','recent_posts_with_excerpts'),
'words'=>'25',
'tag'=>'',
));
?>
<label for =「get_field_id( '標題'); ?>「>
<input class =「widefat」id =「get_field_id( '標題'); ?>「name =」get_field_name( '標題'); ?>「type =」text「value =」「/>
<label for =「get_field_id( 'numposts'); ?>「>
<input class =「widefat」id =「get_field_id( 'numposts'); ?>「name =」get_field_name( 'numposts'); ?>「type =」text「value =」「/>
<label for =「get_field_id( 'more_text'); ?>「>
<input class =「widefat」id =「get_field_id( 'more_text'); ?>「name =」get_field_name( 'more_text'); ?>「type =」text「value =」「/>
<label for =「get_field_id( '單詞'); ?>「>
<input class =「widefat」id =「get_field_id( '單詞'); ?>「name =」get_field_name( '單詞'); ?>「type =」text「value =」「/>
<label for =「get_field_id( '標籤'); ?>「>
<input class =「widefat」id =「get_field_id( '標籤'); ?>「name =」get_field_name( '標籤'); ?>「type =」text「value =」「/>
<?PHP
}
}
function recent_posts_with_excerpts_init(){
register_widget( 'RecentPostsWithExcerpts');
}
add_action('widgets_init','recent_posts_with_excerpts_init');
1 |
'recent_with_excerpt','description'=> __('你最近的帖子,帶有可選的摘錄','recent_posts_with_excerpts'));
parent :: __ construct('RecentPostsWithExcerpts',__('Recent Posts with Excerpts','recent_posts_with_excerpts'),$ widget_ops); } 函數小部件($ args,$ instance){ global $ before_widget,$ intance; extract($ args); $ title = apply_filters('widget_title',$ instance('title')); echo $ before_widget,$ title; $ ul_classes ='recent_posts_with_excerpts'; $ ul_classes = apply_filters('recent_posts_with_excerpts_list_classes',$ ul_classes); if(!empty($ ul_classes)) $ ul_classes ='class =「'。$ ul_classes。'」'; $ li_classes =''; $ li_classes = apply_filters('recent_posts_with_excerpts_item_classes',$ li_classes); if(!empty($ li_classes)) $ li_classes ='class =「'。$ li_classes。'」'; $ h2_classes ='recent_posts_with_excerpts'; $ h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes',$ h2_classes); if(!empty($ h2_classes)) $ h2_classes ='class =「'。$ h2_classes。'」'; do_action( 'recent_posts_with_excerpts_begin'); 回聲'
//檢索最後n篇博文 $ q = array('posts_per_page'=> $ instance('numposts')); if(!empty($ instance('tag'))) $ q('tag')= $ instance('tag'); $ q = apply_filters('recent_posts_with_excerpts_query',$ q,$ intance); $ rpwe = new wp_query($ q); //循環 if($ rpwe-> have_posts()): while($ rpwe-> have_posts()):$ rpwe-> the_post(); 回聲' __('Recent Posts','recent_posts_with_excerpts'), 'numposts'=> 5, 'numexcerpts'=> 5, 'date'=> get_option('date_format'), 'more_text'=> __('Read More','recent_posts_with_excerpts'), 'words'=>'25', 'tag'=>'', )); ?> <label for =「get_field_id( '標題'); ?>「> <input class =「widefat」id =「get_field_id( '標題'); ?>「name =」get_field_name( '標題'); ?>「type =」text「value =」「/> <label for =「get_field_id( 'numposts'); ?>「> <input class =「widefat」id =「get_field_id( 'numposts'); ?>「name =」get_field_name( 'numposts'); ?>「type =」text「value =」「/> <label for =「get_field_id( 'more_text'); ?>「> <input class =「widefat」id =「get_field_id( 'more_text'); ?>「name =」get_field_name( 'more_text'); ?>「type =」text「value =」「/> <label for =「get_field_id( '單詞'); ?>「> <input class =「widefat」id =「get_field_id( '單詞'); ?>「name =」get_field_name( '單詞'); ?>「type =」text「value =」「/> <label for =「get_field_id( '標籤'); ?>「> <input class =「widefat」id =「get_field_id( '標籤'); ?>「name =」get_field_name( '標籤'); ?>「type =」text「value =」「/> <?PHP } function recent_posts_with_excerpts_init(){ register_widget( 'RecentPostsWithExcerpts'); } add_action('widgets_init','recent_posts_with_excerpts_init'); |
結論
此插件是在網站前端展示您最近發布的內容的絕佳方式。如果放在您選擇的位置,最近的帖子將吸引讀者進一步探索您的網站。您需要做的就是提出一個簡單的表單,其中包括帖子的標題,在窗口小部件中可見的帖子數量以及帖子的摘錄。
如果您需要有關此插件的幫助,請發表評論,我會儘快回復您。