Loading...

Category: WordPress

  • The SourceBench 9:49 am on December 6, 2009 Permalink | Reply
    Tags: action, description, filter, meta, meta description, wp_head   Posted in WordPress

    Automatically insert meta description tag into posts/pages. 

    <?php
    /**
     * Automatically insert meta description tag into posts/pages.
     * - can be configured to use either first X chars/words of the post content or post excerpt if available
     * - can use category description for category archive pages if available
     * - can use tag description for tag archive pages if available
     * - can use blog description for everything else
     * - can use a default description if no suitable value is found
     * - can use the value of a custom field as description
     *
     * @usage
     * // add a custom configuration via filter
     * function set_meta_desc_settings( $settings ) {
     * 		return array( 'length' => 10, 'length_unit' => 'char|word', 'use_excerpt' => true, 'add_category_desc' => true, 'add_tag_desc' => true, 'add_other_desc' => true, 'default_description' => '', 'custom_field_key' => '' );
     * }
     * add_filter( 'meta_desc_settings', 'set_meta_desc_settings' );
     * add_action( 'wp_head', 'meta_desc' );
     *
     * @author Thorsten Ott
     */
    function meta_desc() {
    	$default_settings = array(
    								'length' => 25, // amount of length units to use for the meta description
    								'length_unit' => 'word', // the length unit can be either "word" or "char"
    								'use_excerpt' => true, // if the post/page has an excerpt it will overwrite the generated description if this is set to true
    								'add_category_desc' => true, // add the category description to category views if this value is true
    								'add_tag_desc' => true, // add the category description to category views if this value is true
    								'add_other_desc' => true, // add the blog description/tagline to all other pages if this value is true
    								'default_description' => '', // in case no description is defined use this as a default description
    								'custom_field_key' => '', // if a custom field key is set we try to use the value of this field as description
    								);
    
    	$settings = apply_filters( 'meta_desc_settings', $default_settings );
    
    	extract( shortcode_atts( $default_settings, $settings ) );
    
    	global $wp_query;
    
    	if( is_single() || is_page() ) {
    		$post = $wp_query->post;
    
    		// check for a custom field holding a description
    		if ( !empty( $custom_field_key ) ) {
    			$post_custom = get_post_custom_values( $custom_field_key, $post->ID );
    			if ( !empty( $post_custom ) )
    				$text = $post_custom[0];
    		}
    		// check for an excerpt we can use
    		else if( $use_excerpt && !empty( $post->post_excerpt ) ) {
    			$text = $post->post_excerpt;
    		}
    		// otherwise use the content
    		else {
    			$text = $post->post_content;
    		}
    
    		$text = str_replace( array( "\r\n", "\r", "\n", "  " ), " ", $text ); // get rid of all line breaks
    		$text = strip_shortcodes( $text ); // make sure to get rid of shortcodes
    		$text = apply_filters( 'the_content', $text ); // make sure it's save
    		$text = trim( strip_tags( $text ) ); // get rid of tags and html fragments
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;	
    
    	} else if( is_category() && true == $add_category_desc ) {
    		$category = $wp_query->get_queried_object();
    		$text = trim( strip_tags( $category->category_description ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    
    	} else if( is_tag() && true == $add_tag_desc ) {
    		$tag = $wp_query->get_queried_object();
    		$text = trim( strip_tags( $tag->description ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    
    	} else if ( true == $add_other_desc ) {
    		$text = trim( strip_tags( get_bloginfo('description') ) );
    		if ( empty( $text ) && !empty( $default_description ) )
    			$text = $default_description;
    	}
    
    	if ( empty( $text ) )
    		return;
    
    	if ( 'word' == $length_unit ) {
    		$words = explode(' ', $text, $length + 1);
    		if ( count( $words ) > $length ) {
    			array_pop( $words );
    			array_push( $words, '...' );
    			$text = implode( ' ', $words );
    		}
    	} else {
    		if ( strlen( $text ) > $length ) {
    			$text = mb_strimwidth( $text, 0, $length, '...' );
    		}
    	}
    
    	if ( !empty( $text ) ) {
    		echo "\n<meta name=\"description\" content=\"$text\" />\n";
    	}
    }
    ?>

     
  • The SourceBench 3:02 pm on November 28, 2009 Permalink | Reply
    Tags: comment, expand, , , thread, toggle   Posted in WordPress

    Always expand threads in single views with p2theme 

    // as on this page i wanted to have comment threads only collapsed
    // on category/frontpage and search views but expanded on views with is_single()
    // to do so open the file in/js.php in your p2 theme and search for p2_toggle_threads
    // and replace $hide_threads = get_option( 'p2_hide_threads' ); ?> with
    
    	if ( !is_single() )
    		$hide_threads = get_option( 'p2_hide_threads' );
    	else
    		$hide_threads = 0; ?>

     
  • The SourceBench 2:55 pm on November 28, 2009 Permalink | Reply
    Tags: clickable, , ,   Posted in WordPress

    Make links in P2 quotes clickable 

    // by default links in p2 quotes are not clickable. to make them clickable simply add the following to your functions.php
    add_filter( 'p2_get_quote_content', 'make_clickable' );

     
  • The SourceBench 11:13 pm on November 27, 2009 Permalink | Reply
    Tags: tag, tag usage, ,   Posted in WordPress

    Display posts by the TAG usage. Most used tags and their posts go first 

    <?php
    /*
     * Display posts order by the frequency a tag is used.
     * Most used tags first with the posts in it.
     * Found at http://www.devlounge.net/code/wordpress-snippet-display-posts-by-tag-frequency
     */
      $noOfTags = 10;
      $noOfPosts = 4;
      $cloudRight = get_tags("orderby=count&order=DESC&number=$noOfTags");
      foreach((array)$cloudRight as $tagRight) : ?>
    	  <?php
    	  $postsRight = new WP_Query();
    	  $postsRight->query("tag={$tagRight->slug}&showposts=$noOfPosts");
    	  ?>
    	  <?php if ( $postsRight->have_posts() ) :? >
    		<dl class="xoxo">
    		  <dt><?php echo $tagRight->name ?></dt>
    		  <?php while ( $postsRight->have_posts() ) : $postsRight->the_post(); ?>
    		  <dd><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></dd>
    		  <?php endwhile;?>
    		</dl>
    	  <?php endif; ?>
      <?php
    	unset($postsRight);
      endforeach;
    ?>

     
  • The SourceBench 11:05 pm on November 27, 2009 Permalink | Reply
    Tags: comments, ,   Posted in WordPress

    A really simple way to show top posts determined by comment count 

    <?php
    // a simple way to get most popular posts by comment count, just throw it in your templatefile and alter t your needs
    $max = 10; // amount of posts to get
    ?>
    <ul>
    <?php
    $result = $wpdb->get_results( $wpdb->prepare( "SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0, %d", $max ) );
    foreach ($result as $post) :
    	setup_postdata($post);
    	$postid = $post->ID;
    	$title = $post->post_title;
    	$commentcount = $post->comment_count;
    	if ($commentcount != 0) :
    ?>
    		<li>
    			<a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> (<?php echo $commentcount ?>)
    		</li>
    <?php
    	endif;
    endforeach;
    ?>
    </ul>

     
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