Tagged: Wordpress RSS

  • Jay Kwong 7:30 am on February 25, 2010 Permalink | Reply
    Tags: Functions, , Wordpress   

    如何使用WordPress 2.9内置文章缩略图功能Post Thumbnail | 帕兰映像 

    1. 激活文章缩略图功能

    要激活该功能,打开你主题的functions.php文件,输入下面的代码:

    add_theme_support( 'post-thumbnails' );
    

    你也可以添加一个参数来指定在post还是page激活该功能:

    add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
    

    默认是在两者里都激活的。激活后,在你的post或page编辑页面的侧边栏底部,就能看到该功能的设置模块了。

    2.输出到主题

    要在你的主题中显示出设置的图片,你需要在loop里面添加下面这个函数:

    
    

    你可以给该函数传递一个参数值来调用图片的不同尺寸,

    the_post_thumbnail();                  // 默认显示缩略图
    
    the_post_thumbnail('thumbnail');       // 显示缩略图
    the_post_thumbnail('medium');          // 显示中等尺寸
    the_post_thumbnail('large');           // 显示大尺寸
    
    the_post_thumbnail( array(100,100) );  // 自定义尺寸
    

    3. 在loop外调用某篇文章的文章缩略图

    如果你想在loop以外调用某些文章的缩略图,另一个函数为你准备:

    
    

    和the_post_thumbnail()相比,它需要再传入一个参数来指定文章的ID:

    get_the_post_thumbnail($id);
    
    get_the_post_thumbnail($id, 'thumbnail');
    get_the_post_thumbnail($id, 'medium');
    get_the_post_thumbnail($id, 'large');
    
    get_the_post_thumbnail($id, array(100,100) );
    

    对于大多数wordpress用户来说,知道上面这些就已经足够了。如果你是一个主题开发者,想了解的更深入,可以查看justintadlock的这篇文章:

    Everything you need to know about WordPress 2.9’s post image feature

    里面还介绍了如何添加函数连接到过滤器动作来改变文章缩略图尺寸和缩略图的html代码输出。

    via 如何使用WordPress 2.9内置文章缩略图功能Post Thumbnail | 帕兰映像.

     
  • Jay Kwong 7:49 am on February 23, 2010 Permalink | Reply
    Tags: caption, Wordpress   

    wp-caption 

    .wp-caption{ background: #ffffff; text-align:center!important; border:1px solid #D0D0D0; width:570px!important; padding:3px;-moz-border-radius: 5px;
    -webkit-border-radius: 5px; }
    .wp-caption img{ max-width:100%; text-align:center; padding:0px!important; margin:0 auto}
    .wp-caption .wp-caption-text{ padding:3px!important; margin:0!important; font-size:11px!important; color: #666}
    
     
  • Jay Kwong 11:38 am on February 17, 2010 Permalink | Reply
    Tags: Widget, Wordpress   

    Widgetize Your WordPress Theme 

    if (function_exists('register_sidebar')) {
    
    	register_sidebar(array(
    		'name' => 'Widgetized Area',
    		'id'   => 'widgetized-area',
    		'description'   => 'This is a widgetized area.',
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h4>',
    		'after_title'   => '</h4>'
    	));
    
    }
    

    This is all that is needed for the first step. Once this widgetizing code is placed in your functions.php file, proceed to Step 2. The remainder of this section is explanation.

    This code checks to make sure that the current version of WordPress supports widgets, and then declares an array of values that will be used to create the widgetized area in your theme. Here are the different values:

    * name – the name of the widgetized area as displayed in the WP Admin
    * id – a unique identifier for your widgetized area
    * description – description of the widgetized area
    * before_widget – the markup generated before any user-added widgets
    * after_widget – the markup generated after any user-added widgets
    * before_title – the markup generated before the title of any user-added widgets
    * after_title – the markup generated after the title of any user-added widgets

    So, given these parameters, our widgetizing code would result in the following output, say, if the built-in Search widget were added to our widgetized area:

    <div id="search-3" class="widget widget_search">
    	<h4>Search</h4>
    	<form role="search" method="get" id="searchform" action="http://localhost/283/" >
    		<div>
    			<label class="screen-reader-text" for="s">Search for:</label>
    			<input type="text" value="" name="s" id="s" />
    			<input type="submit" id="searchsubmit" value="Search" />
    		</div>
    	</form>
    </div>
    

    Note the markup created for the opening

    , which gets its attribute information based on the wild-card matching specified in our widgetizing array. To see this more clearly, examine the following side-by-side comparison of the sprintf directives and the resulting (X)HTML source code:

    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    <div id="search-3" class="widget widget_search">
    

    Hopefully the relationship is clear. This is a powerful way to ensure that all widgets have a similar class names and unique IDs, which are important for easy CSS styles.

    Widgetize Your WordPress Theme: Step 2

    Finally, add the following code to the location in your theme’s template file(s) where you would like the widgetized area to appear:

    <div id="widgetized-area">
    
    	<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('widgetized-area')) : else : ?>
    
    	<div class="pre-widget">
    		<p><strong>Widgetized Area</strong></p>
    		<p>This panel is active and ready for you to add some widgets via the WP Admin</p>
    	</div>
    
    	<?php endif; ?>
    
    </div>
    
     
  • Jay Kwong 9:48 am on February 17, 2010 Permalink | Reply
    Tags: search, Wordpress   

    Highlight Search Terms in Results 

    In an attempt to make your WordPress search even more user friendly, you can highlight the search terms in the results. We did this for one of our clients, so we thought it would be useful for other users. In this article we will show you how you can highlight search terms in the results in WordPress.

    First open your search.php and look for the following code:

    <?php the_title(); ?>

    Replace the above code with:

    <?php echo $title; ?>

    Make sure that you paste this line above the title code:

    <?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?>
    

    Now open your CSS file and add the styling for the class search-excerpt, and it will highlight the term. Currently the code is making the search terms bold.

     
  • Jay Kwong 7:08 am on January 19, 2010 Permalink | Reply
    Tags: images, , , Wordpress   

    Automatically resize post pictures 

    First, note that this code use TimThumb to resize pictures. This recipe explain how to install TimThumb on your WordPress blog.

    Let’s start by creating the shortcode. Just paste the code below into your functions.php file.

    function imageresizer( $atts, $content = null ) {
    	return '<img src="/timthumb/timthumb.php?src='.$content.'&w=590" alt="" />';
    }
    
    add_shortcode('img', 'imageresizer');
    

    Then, you can use the following syntax to add an automatically resized image to your blog post:

    [img]http://www.yoursite.com/yourimage.jpg[/img]
    
     
  • Jay Kwong 6:57 am on January 19, 2010 Permalink | Reply
    Tags: attachments, , Wordpress   

    Show WordPress post attachments 

    To achieve this recipe, just paste the following code anywhere in your post.php file and attachments will be displayed.

    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => null,
    	'post_status' => null,
    	'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, false);
    	}
    }
    
     
  • Jay Kwong 6:42 am on January 19, 2010 Permalink | Reply
    Tags: , Wordpress   

    Content in two columns 

    This code is poweful but definitely easy to implement. Just paste it on your functions.php file and it will automatically output your post content in columns.
    Your post content will be splitted on tags.

    functions.php

    function my_multi_col($content){
    $columns = explode('</h2>
    <h2>', $content);
    
    $i = 0;
    
    foreach ($columns as $column){
    if (($i % 2) == 0){
    $return .= '
    <div class="content_left">' . "\n";
    if ($i &gt; 1){
    $return .= "
    <h2>";
    } else{
    $return .= '
    <div class="content_right">
    
    ' . "\n
    <h2>";
    }
    $return .= $column;
    $return .= '</h2>
    </div>
    ';
    $i++;
    }
    
    if(isset($columns[1])){
    $content = wpautop($return);
    }else{
    $content = wpautop($content);
    }
    echo $content;
    }
    }
    add_filter('the_content', 'my_multi_col');</h2>
    </div></h2>
    

    CSS

    .content_right, .content_left{ float:left; width:45%; }
    .content_left{ padding-right:5%; }
    

    http://www.wprecipes.com/

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel