Support

[sticky] [closed] WordPress Tips (241 posts)

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 2 years ago
    #

    More loops

    first
    <?php query_posts('showposts=8'); $ids = array(); while (have_posts()) : the_post(); $ids[] = get_the_ID(); the_title(); the_content(); endwhile; ?>

    second
    <?php query_posts(array('post__not_in' => $ids)); while (have_posts()) : the_post(); the_title(); the_content(); endwhile; ?>

  2. rss milo

    moderator


    rss Posted 2 years ago
    #

    Posts With A Specific Custom Field

    <?php query_posts('meta_key=review_type&meta_value=movie'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

  3. rss milo

    moderator


    rss Posted 2 years ago
    #

    Loop Of Images

    functions
    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image $first_img = "/images/default.jpg";
    }
    return $first_img;
    }

    loop
    <?php if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    < a href ="<?php the_permalink();?>" title ="<?php the_title(); ?>" class="img-loop">
    < img src="http://imageserver/<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /> </ a>
    endwhile;
    endif; ?>

  4. rss milo

    moderator


    rss Posted 2 years ago
    #

    Eliminate most of the duplicate content:

    <? if (is_home() && (!$paged || $paged == 1) || is_search() || is_single() || is_page()): ?>

    <div class="entry">

    <?php the_content() ?>

    <? else: ?>

    <small>Archived; click post to view.
    <b>Excerpt:</b> <?= substr(strip_tags($post->post_content), 0, 300); ?> … </small>

    <? endif; ?>

  5. rss milo

    moderator


    rss Posted 2 years ago
    #

    DiggProof your Wordpress with some valuable ideas, not all will work on a shared host.

  6. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display Tags In A Dropdown Menu
    functions.php file

    <?php
    function dropdown_tag_cloud( $args = '' ) {
    $defaults = array(
    'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
    'exclude' => '', 'include' => '' );
    $args = wp_parse_args( $args, $defaults );
    $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) );
    // Always query top tags
    if ( empty($tags) )
    return;
    $return = dropdown_generate_tag_cloud( $tags, $args );
    // Here's where those top tags get sorted according to $args
    if ( is_wp_error( $return ) )
    return false;
    else
    echo apply_filters( 'dropdown_tag_cloud', $return, $args ); }
    function dropdown_generate_tag_cloud( $tags, $args = '' ) {
    global $wp_rewrite;
    $defaults = array(
    'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC' );
    $args = wp_parse_args( $args, $defaults );
    extract($args);
    if ( !$tags )
    return;
    $counts = $tag_links = array();
    foreach ( (array) $tags as $tag ) {
    $counts[$tag->name] = $tag->count;
    $tag_links[$tag->name] = get_tag_link( $tag->term_id );
    if ( is_wp_error( $tag_links[$tag->name] ) )
    return $tag_links[$tag->name];
    $tag_ids[$tag->name] = $tag->term_id; }
    $min_count = min($counts);
    $spread = max($counts) - $min_count;
    if ( $spread <= 0 )
    $spread = 1;
    $font_spread = $largest - $smallest;
    if ( $font_spread <= 0 )
    $font_spread = 1;
    $font_step = $font_spread / $spread;
    // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
    if ( 'name' == $orderby )
    uksort($counts, 'strnatcasecmp');
    else
    asort($counts);
    if ( 'DESC' == $order )
    $counts = array_reverse( $counts, true );
    $a = array();
    $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
    foreach ( $counts as $tag => $count ) {
    $tag_id = $tag_ids[$tag];
    $tag_link = clean_url($tag_links[$tag]);
    $tag = str_replace(' ', ' ', wp_specialchars( $tag ));
    $a[] = "\t<option value='$tag_link'>$tag ($count)</option>"; }
    switch ( $format ) :
    case 'array' :
    $return =& $a; break; case 'list' :
    $return = "< ul class='wp-tag-cloud' >\n\t< li >";
    $return .= join("< / li >\n\t< li >", $a);
    $return .= "< /li >\n< /ul >\n";
    break;
    default :
    $return = join("\n", $a);
    break;
    endswitch;
    return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args ); } ?>

    theme file

    <select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> <option value="#">Tags</option> <?php dropdown_tag_cloud('number=0&order=asc'); ?> </select>

  7. rss milo

    moderator


    rss Posted 2 years ago
    #

    Disable WordPress’ Automatic Formatting

    function my_formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) {
    if (preg_match($pattern_contents, $piece, $matches)) {
    $new_content .= $matches[1];
    } else {
    $new_content .= wptexturize(wpautop($piece));
    } }
    return $new_content;
    }
    remove_filter('the_content', 'wpautop');
    remove_filter('the_content', 'wptexturize');
    add_filter('the_content', 'my_formatter', 99);

  8. rss milo

    moderator


    rss Posted 2 years ago
    #

    Disable WordPress From Auto-Saving Posts

    function disableAutoSave(){
    wp_deregister_script('autosave');
    }
    add_action( 'wp_print_scripts', 'disableAutoSave' );

  9. rss milo

    moderator


    rss Posted 2 years ago
    #

    Disable scripts and styles

    add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
    function my_deregister_javascript() {
    wp_deregister_script( 'contact-form-7' ); }

    add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
    function my_deregister_styles() {
    wp_deregister_style( 'wp-pagenavi' ); }

  10. rss milo

    moderator


    rss Posted 2 years ago
    #

    /*add avatar to wordpress default list*/
    add_filter( ‘avatar_defaults’, ‘newgravatar’ );
    function newgravatar ($avatar_defaults) {
    $myavatar = get_bloginfo(’template_directory’) . ‘/images/name-your-gravatar-image.jpg’;
    $avatar_defaults[$myavatar] = “administrator”;
    return $avatar_defaults;
    }

  11. rss milo

    moderator


    rss Posted 2 years ago
    #

    Show Most Recent Comments

    comments
    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    WHERE comment_approved = '1' AND comment_type = '' AND
    post_password = ''
    ORDER BY comment_date_gmt DESC
    LIMIT 10";
    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML;
    $output .= "\n
    <ul >";
    foreach ($comments as $comment) {
    $output .= "\n
    <li >".strip_tags($comment->comment_author)
    .":" . "< a href ="\">ID) .
    "#comment-" . $comment->comment_ID . "\" title=\"on " .
    $comment->post_title . "\">" . strip_tags($comment->com_excerpt)
    ."</li >
    ";
    }
    $output .= "\n</ul >
    ";
    $output .= $post_HTML;
    echo $output;?>

  12. rss milo

    moderator


    rss Posted 2 years ago
    #

    Email Posts to Their Friends

    < a title="Send this
    article to a friend!" href =" mailto:?subject=<?php the_title(); ?>&body=<?php the_permalink() ?>" >Email this

  13. rss milo

    moderator


    rss Posted 2 years ago
    #

    Translation Links

    < ul id="translations">
    <li >< a rel="nofollow" href ="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cde">Translate this page to Deutsch</li >
    <li >< a rel="nofollow" href=" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Ces">Translate this page to Espanol</li >
    <li >< a rel="nofollow" href=" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cfr">Translate this page to Francais</li >
    <li >< a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cit">Translate this page to Italiano</li >
    <li >< a rel="nofollow" href=" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cpt">Translate this page to Portugues</li >
    <li >< a rel="nofollow" href=" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Car">Translate this page to Arabic</li >
    <li >< a rel="nofollow" href =" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cja">Translate this page to Japanese</li >
    <li >< a rel="nofollow" href =" http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=en%7Cko">Translate this page to Korean</li >
    </ul >

  14. rss milo

    moderator


    rss Posted 2 years ago
    #

    Show your Twitter Followers on WordPress

    function string_getInsertedString($long_string,$short_string,$is_html=false){ if($short_string>=strlen($long_string))return false; $insertion_length=strlen($long_string)-strlen($short_string); for($i=0;$isaveHTML(); $element->parentNode->removeChild($element); $html2=$document->saveHTML(); return string_getInsertedString($html,$html2,true); } function getFollowers($username){ $x = file_get_contents("http://twitter.com/".$username); $doc = new DomDocument; @$doc->loadHTML($x); $ele = $doc->getElementById('follower_count'); $innerHTML=preg_replace('/^< [^>]*>(.*)< [^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele)); return $innerHTML; }

    echo getFollowers("cuisi")." followers";

  15. rss milo

    moderator


    rss Posted 2 years ago
    #

    Deactivate WordPress Widgets

    // unregister all default WP Widgets function unregister_default_wp_widgets() { unregister_widget('WP_Widget_Pages'); unregister_widget('WP_Widget_Calendar'); unregister_widget('WP_Widget_Archives'); unregister_widget('WP_Widget_Links'); unregister_widget('WP_Widget_Meta'); unregister_widget('WP_Widget_Search'); unregister_widget('WP_Widget_Text'); unregister_widget('WP_Widget_Categories'); unregister_widget('WP_Widget_Recent_Posts'); unregister_widget('WP_Widget_Recent_Comments'); unregister_widget('WP_Widget_RSS'); unregister_widget('WP_Widget_Tag_Cloud'); }

    add_action('widgets_init', 'unregister_default_wp_widgets', 1);

  16. rss milo

    moderator


    rss Posted 2 years ago
    #

    feedburner count in full text

    <?php $fburl=”https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YourURL“; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $fburl); $stored = curl_exec($ch); curl_close($ch); $grid = new SimpleXMLElement($stored); $rsscount = $grid->feed->entry['circulation']; echo $rsscount; ?>

  17. rss milo

    moderator


    rss Posted 2 years ago
    #

    Twitter Followers as Text

    function string_getInsertedString($long_string,$short_string,$is_html=false){ if($short_string>=strlen($long_string))return false; $insertion_length=strlen($long_string)-strlen($short_string); for($i=0;$i<strlen ($short_string);++$i){ if($long_string[$i]!=$short_string[$i])break; } $inserted_string=substr($long_string,$i,$insertion_length); if($is_html && $inserted_string[$insertion_length-1]=='<'){ $inserted_string='<'.substr($inserted_string,0,$insertion_length-1); } return $inserted_string; } function DOMElement_getOuterHTML($document,$element){ $html=$document->saveHTML(); $element->parentNode->removeChild($element); $html2=$document->saveHTML(); return string_getInsertedString($html,$html2,true); } function getFollowers($username){ $x = file_get_contents("http://twitter.com/sitesketch101"); $doc = new DomDocument; @$doc->loadHTML($x); $ele = $doc->getElementById('follower_count'); $innerHTML=preg_replace('/^< [^>]*>(.*)< [^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele)); return $innerHTML; }

    <?php echo getFollowers("username")." Followers"; ?>

  18. rss milo

    moderator


    rss Posted 2 years ago
    #

    Adding A Post Loop Anywhere In Your Theme

    <?php $my_query = new WP_Query('category_name=Featured&showposts=5'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
    <!-- POST CODES HERE -->
    <?php endwhile; ?>

  19. rss milo

    moderator


    rss Posted 2 years ago
    #

    Showing Related Posts Without A Plugin (based on related tags)

    < ?php $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'showposts'=>5, // Number of related posts that will be shown. 'caller_get_posts'=>1 ); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo ' <h3>Related Posts</h3> <ul >'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li >< a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?>">< ?php the_title(); ?>< /a>< /li> < ?php } echo '</ ul> '; } } ?></ ul>

  20. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display Author Information For Posts

    <div id="author-info">
    <div id="author-image"> < a href="<?php the_author_meta('user_url'); ?>">< ?php echo get_avatar( get_the_author_meta('user_email'), '80', '' ); ?>< /a></div>
    <div id="author-bio">
    <h4>Written by < ?php the_author_link(); ?></h4> < ?php the_author_meta('description'); ?> </div>
    </div>
    <!--Author Info-->

  21. rss milo

    moderator


    rss Posted 2 years ago
    #

    Displaying Ads In Your RSS Feed

    < ?php function insertAds($content) { $content = $content.'Originally found at an < a href="http://www.domain.com">Domain Blog'; return $content; } add_filter('the_excerpt_rss', 'insertAds'); add_filter('the_content_rss', 'insertAds'); ?>

  22. rss milo

    moderator


    rss Posted 2 years ago
    #

    Weather Man WordPress Plugin

    Download the plugin
    Extract and upload the contents of the folder to /wp-contents/plugins/ folder
    Go to the Plugin management page of WordPress admin section and enable the ‘Weather Man’ plugin
    Go to the Widget configuration(under Appearance) and add the Weather Man Widget into any sidebar.
    Alternatively, you can add the [weather-man] tag in any post and the Weather Man Widget will show up there.

  23. rss milo

    moderator


    rss Posted 2 years ago
    #

    Paypal Donate Widget

    <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="your paypal mail.com">
    <input type="hidden" name="item_name" value="Donation">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="amount" value="25.00">
    <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    </form>

  24. rss milo

    moderator


    rss Posted 2 years ago
    #

    Automatically output the content in two columns

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

Topic Closed

This topic has been closed to new replies.

Design by milo

Milo designs web sites that strike the perfect balance between professional high-class graphics, functionality, usability, user experience, and high performance.