软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 建站程序 > Blog教程 > 在WordPress中给推荐文章添加缩略图

在WordPress中给推荐文章添加缩略图

对于如何在WordPress中显示推荐文章,网上有非常多方法,但似乎不是那么令人满意。在本教程中,我们将与大家分享一种可以让用户选择某些特定文章并通过使用WordPress2.9新增的缩略图功能来显示这些文章的方法。此方法可以避免使用“置顶文章”,在其他一些自定义的方法中由于其他功能不可避免要使用到“置顶文章”。

注意: 使用本教程需要熟悉一些WordPress、PHP、HTML和CSS知识。

我们修改了由W3C Gallery公司San制作的Featured Posts Lists插件。虽然San给这款插件创建了一个高级版本可以显示图片,但它需要一个自定义字段。这样一来,你只能在2.9中使用这个缩略图功能。

最终效果

修改插件

首先,将下面的代码复制到functions.php 文件或将它作为插件上传到一个单独的文件里。
 

<?php
/*
Plugin Name: Featured Posts List with Thumbnail
Plugin URI: http://www.w3cgallery.com/w3c-css/display-specificmultiple-posts-as-featured-post-list-plugins
Description: Display specific/multiple posts List with Thumbnails on your sidebar or any place of your site. It creates a tab "Featured Posts List" in "Settings" tab
Version: 2.0
Author: SAN – w3cgallery.com & Windowshostingpoint.com & Syed Balkhi
Author URI: http://www.w3cgallery.com/
*/

// Main function to diplay on front end

function featuredpostsList() {
global $post, $wpdb, $posts_settings;
// posts_id from database
$posts_id = $posts_settings['posts_id'];

if($posts_id) {

$posts_idarray = explode(',',$posts_id);

foreach ($posts_idarray as $list){
$post = new WP_Query('p='.$list.'');
$post->the_post();
?>
<div class="popcontainer">
<div class="popthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="popcontent">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="popauthor"><?php the_time('M j, Y') ?></div>

</div>

</div>

<?php }
} else {
echo $before ."None found". $after;
}
}

$data = array(
'posts_id' => ''
);
$ol_flash = '';

$posts_settings = get_option('posts_settings');

// ADMIN PANLE SEETTING
function posts_add_pages() {
// Add new menu in Setting or Options tab:
add_options_page('Featured Posts List', 'Featured Posts List', 8, 'postsoptions', 'posts_options_page');
}

/* Define Constants and variables*/
define('PLUGIN_URI', get_option('siteurl').'/wp-content/plugins/');

/* Functions */

function posts_options_page() {
global $ol_flash, $posts_settings, $_POST, $wp_rewrite;
if (isset($_POST['posts_id'])) {
$posts_settings['posts_id'] = $_POST['posts_id'];
update_option('posts_settings',$posts_settings);
$ol_flash = "Your Featured List has been saved.";
}

if ($ol_flash != '') echo '<div id="message"class="updated fade"><p>' . $ol_flash . '</p></div>';

echo '<div class="wrap">';
echo '<h2>Add Posts ID to Create Featured Post List</h2>';
echo '<table class="optiontable form-table"><form action="" method="post">
<tr><td colspan="2"><strong>This plugin gives full freedom to display multiple posts as Featured Post List to your site.</strong></td></tr>
<tr><td><strong>Post ID :</strong></td><td><input type="text" name="posts_id" value="' . htmlentities($posts_settings['posts_id']) . '" size="50%" /></td></tr>
<tr><td colspan="2"><strong>SAN Hint: To Add Multiple Post IDs use " , " for exmple : " 1, 2, 3" </strong></td></tr>
</table>';

echo '<Div class="submit"><input type="submit" value="Save your list" /></div>
<p>Paste this code into where you want it to display featured posts list <strong>&lt;?php featuredpostsList(); ?&gt;</strong> <br/> Or you can pass variable before and after like this default setting <strong>&lt;?php featuredpostsList($before = &lt;li&gt;", $after = &lt;/li&gt;") ?&gt;</strong></p>
</form>';
echo '</div>';

}

add_action('admin_menu', 'posts_add_pages');

?>

 完成后,你可以在模板文件的任何地方粘贴该代码即可。

<?php featuredpostsList(); ?>

相关阅读
网友评论
栏目导航
推荐软件