Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    how to enlight searched text in search results

    Open your search.php temalate file, find the the_title() function, then replace it with the following:

    echo $title;

    Now, just before the modified line, add this code:

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

    Save the search.php file and apply your class to the style.css with the following class

    strong.search-excerpt { background: yellow; }

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    How to add body class to WordPress themes

    Add this to your functions file:

    <?php function mytheme_body_control() { if (is_home()) { echo 'id="home"'; } elseif (is_single()) { echo 'id = "single"'; } elseif (is_search()) { echo 'id="search"'; } elseif (is_archive()) { echo 'id="archive"'; } } ?>

    then in header add this after the closing header tag

    <body <?php mytheme_body_control(); ?>>

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    Complete separate style.css for each page template

    <?php
    if ( is_page_template('page.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/style.css" />
    <?php } ?>

    <?php
    if ( is_page_template('nosidebar.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/nosidebar.css" />
    <?php } ?>

    <?php
    if ( is_page_template('nopostpage.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/nopostpage.css" />
    <?php } ?>

    <?php
    if ( is_page_template('homepage.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/homepage.css" />
    <?php } ?>

    This will assign a different css file for each of your pages,
    of course all the page templates and its CSS files have to be created before using.

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display the last image attached to WP posts via shortcode

    add this code to your functions.php file

    <?php
    function sc_postimage($atts, $content = null) {
    extract(shortcode_atts(array(
    "size" => 'thumbnail',
    "float" => 'none'
    ), $atts));
    $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
    foreach( $images as $imageID => $imagePost )
    $fullimage = wp_get_attachment_image($imageID, $size, false);
    $imagedata = wp_get_attachment_image_src($imageID, $size, false);
    $width = ($imagedata[1]+2);
    $height = ($imagedata[2]+2);
    return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
    }
    add_shortcode("postimage", "sc_postimage");
    ?>

    than just add this shortcode via your wp post admin panel

    [postimage]

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    Link to some external ressource in post title

    add this code to your functions.php file

    <?php
    function print_post_title() { global $post; $thePostID = $post->ID; $post_id = get_post($thePostID); $title = $post_id->post_title; $perm = get_permalink($post_id); $post_keys = array(); $post_val = array(); $post_keys = get_post_custom_keys($thePostID); if (!empty($post_keys)) { foreach ($post_keys as $pkey) { if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') { $post_val = get_post_custom_values($pkey); } } if (empty($post_val)) { $link = $perm; } else { $link = $post_val[0]; } } else { $link = $perm; } echo '<h2>< a href= "'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</h2>'; }
    ?>

    open your index.php/home php template, replace the standard code for printing titles:

    <h2 >< a href ="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></ h2>

    by a call to the new print_post_title() function:

    <?php print_post_title() ?>

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get thumbnails from specific posts

    <ul >
    <?php //The name of custom field you'd like to get, don't forget to change it $custom_field_name = "featuredimg"; //Cool sql request $sql = "SELECT p.comment_count, p.ID, m.post_id, m.meta_key, m.meta_value FROM wp_posts p, wp_postmeta m WHERE p.ID = m.post_id LIMIT 0,10;"; $result = $wpdb->get_results($sql); foreach ($result as $topten) { $postid = $topten->ID; $title = $topten->post_title; $commentcount = $topten->comment_count; if ( $topten->meta_key == $custom_field_name) { $img = 'meta_value.'" alt=""/>'; ?>
    <li >< a href= "<?php echo get_permalink($postid); ?>">
    <?php echo $img; ?>
    </li >
    <?php } } ?> </ul >

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    Comment And Ping Count In WordPress 2.7

    <?php if ( ! empty($comments_by_type['comment']) ) : $count = count($comments_by_type['comment']); ($count !== 1) ? $txt = "Comments" : $txt = "Comment"; ?> <h3 id="comments"><?php echo $count . " " . $txt; ?></h3>

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Manually define to show full post or excerpt on your homepage

    On the index.php file, replace the current loop by the following:

    <?php if (have_posts()) :
    while (have_posts()) : the_post();
    $customField = get_post_custom_values("full");
    if (isset($customField[0])) {
    //Custom field is set, display a full post
    the_title();
    the_content();
    } else {
    // No custom field set, let's display an excerpt
    the_title();
    the_excerpt();
    endwhile;
    endif;
    ?>

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    Add new Gravatars to WordPress default gravatar list

    open the functions.php file, paste this inside and change img paths to the yours.

    <?php
    if ( !function_exists('fb_addgravatar') ) {
    function fb_addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('template_directory').'/images/avatar.gif';
    //default avatar
    $avatar_defaults[$myavatar] = 'people';

    $myavatar2 = get_bloginfo('template_directory').'/images/myavatar.png';
    //Avatar for user "admin"
    $avatar_defaults[$myavatar2] = 'admin';

    return $avatar_defaults;
    }

    add_filter( 'avatar_defaults', 'fb_addgravatar' );
    }
    ?>

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    if in the last X days

    <?php $mylimit=31 * 86400; $post_age = date('U') - get_post_time('U'); if ($post_age < $mylimit) { ?>/images/new.png" alt="" border="0" /><?php } ?>

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    get page slug

    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_slug = $post_obj->post_name;

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    specific template for first visit with cookie

    <?php
    /*
    Plugin Name: first-visit-template
    Description: Loads a welcome.php template in theme directory if it is the first visit (the function is not lauched if you're logged as admin), and writes a cookie so it's shown only once.
    Author: G.Lachance
    Author URI: http://tinyurl.com/9wb48o
    Credits: Based upon the "Welcome Visitor! Reloaded" plugin from Alaeddin, which is largely based upon the original Welcome Visitor! plugin by Kaf (http://guff.szub.net/2006/04/12/welcome-visitor/)
    which was released under the GNU General Public License.
    Version: 1

    */

    //for testing your template, uncomment line 30.

    function welcome_visitor_reloaded() {

    if(is_new_visitor()) {
    $tpl_file = TEMPLATEPATH . '/welcome.php';
    if ( file_exists($tpl_file) ) {
    include($tpl_file);
    exit;
    }
    }
    }

    function is_new_visitor()
    {

    //return true; //uncomment this for testing.

    global $visits;

    if (!is_admin())
    {
    if (isset($_COOKIE['visits']))
    $visits = $_COOKIE['visits'] + 1;
    else
    $visits = 1;

    $url = parse_url(get_option('home'));
    setcookie('visits', $visits, time()+60*60*24*365, $url['path'] . '/');
    }
    else return false;

    return $visits == 1;
    }

    ///
    add_action('template_redirect', 'welcome_visitor_reloaded');

    ?>

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    wp 2.7. comment classes

    ol.commentlist {}
    ol.commentlist li {}
    ol.commentlist li.alt {}
    ol.commentlist li.bypostauthor {}
    ol.commentlist li.byuser {}
    ol.commentlist li.comment-author-admin {}
    ol.commentlist li.comment {}
    ol.commentlist li.comment div.comment-author {}
    ol.commentlist li.comment div.vcard {}
    ol.commentlist li.comment div.vcard cite.fn {}
    ol.commentlist li.comment div.vcard cite.fn a.url {}
    ol.commentlist li.comment div.vcard img.avatar {}
    ol.commentlist li.comment div.vcard img.avatar-32 {}
    ol.commentlist li.comment div.vcard img.photo {}
    ol.commentlist li.comment div.vcard span.says {}
    ol.commentlist li.comment div.commentmetadata {}
    ol.commentlist li.comment div.comment-meta {}
    ol.commentlist li.comment div.comment-meta a {}
    ol.commentlist li.comment * {} - (p, em, strong, blockquote, ul, ol, etc.)
    ol.commentlist li.comment div.reply {}
    ol.commentlist li.comment div.reply a {}
    ol.commentlist li.comment ul.children {}
    ol.commentlist li.comment ul.children li {}
    ol.commentlist li.comment ul.children li.alt {}
    ol.commentlist li.comment ul.children li.bypostauthor {}
    ol.commentlist li.comment ul.children li.byuser {}
    ol.commentlist li.comment ul.children li.comment {}
    ol.commentlist li.comment ul.children li.comment-author-admin {}
    ol.commentlist li.comment ul.children li.depth-2 {}
    ol.commentlist li.comment ul.children li.depth-3 {}
    ol.commentlist li.comment ul.children li.depth-4 {}
    ol.commentlist li.comment ul.children li.depth-5 {}
    ol.commentlist li.comment ul.children li.odd {}
    ol.commentlist li.even {}
    ol.commentlist li.odd {}
    ol.commentlist li.parent {}
    ol.commentlist li.pingback {}
    ol.commentlist li.pingback div.comment-author {}
    ol.commentlist li.pingback div.vcard {}
    ol.commentlist li.pingback div.vcard cite.fn {}
    ol.commentlist li.pingback div.vcard cite.fn a.url {}
    ol.commentlist li.pingback div.vcard span.says {}
    ol.commentlist li.pingback div.commentmetadata {}
    ol.commentlist li.pingback div.comment-meta {}
    ol.commentlist li.pingback div.comment-meta a {}
    ol.commentlist li.pingback * {} - (p, em, strong, blockquote, ul, ol, etc.)
    ol.commentlist li.pingback div.reply {}
    ol.commentlist li.pingback div.reply a {}
    ol.commentlist li.pingback ul.children {}
    ol.commentlist li.pingback ul.children li {}
    ol.commentlist li.pingback ul.children li.alt {}
    ol.commentlist li.pingback ul.children li.bypostauthor {}
    ol.commentlist li.pingback ul.children li.byuser {}
    ol.commentlist li.pingback ul.children li.comment {}
    ol.commentlist li.pingback ul.children li.comment-author-admin {}
    ol.commentlist li.pingback ul.children li.depth-2 {}
    ol.commentlist li.pingback ul.children li.depth-3 {}
    ol.commentlist li.pingback ul.children li.depth-4 {}
    ol.commentlist li.pingback ul.children li.depth-5 {}
    ol.commentlist li.pingback ul.children li.odd {}
    ol.commentlist li.thread-alt {}
    ol.commentlist li.thread-even {}
    ol.commentlist li.thread-odd {}

  14. rss milo

    moderator


    rss Posted 3 years ago
    #

    Current Page .current_page_item

    <ul >< li <?php if (is_page($post->ID)) { echo'class="current_page_item"'; } ?>>Menu Item 1</li ></ul >

  15. rss milo

    moderator


    rss Posted 3 years ago
    #

    a variable is a valid email address

    function is_email($user_email) {
    $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if(strstr($user_email, '@') && strstr($user_email, '.')) {
    if (preg_match($chars, $user_email)) {
    return true;
    } else {
    return false;
    }
    } else {
    return false;
    }
    }

  16. rss milo

    moderator


    rss Posted 3 years ago
    #

    better archive

    /*
    * return html list with all posts grouped by month
    */
    function wp_getallpost() {
    // variables declaration
    $archives = "";
    $monthname = "";
    // get all posts
    $lastposts = get_posts('numberposts=-1&orderby=date');
    foreach($lastposts as $post) :
    $month = mysql2date('m', $post->post_date );
    $year = mysql2date('Y', $post->post_date );
    if ( $monthname <> mysql2date('F', $post->post_date ) ) {
    // check if i need to close list
    if ( $monthname != "" ) { $archives .= "</ul >"; }
    $archives .= "<p ><strong >< a href='".get_month_link($year, $month)."'>";
    $archives .= mysql2date('F Y', $post->post_date );
    $archives .= "</strong ></p >";
    $archives .= "< ul class='ba-single-column'>";
    // change current month
    $monthname = mysql2date('F', $post->post_date );
    }
    $archives .= "<li >";
    $archives .= mysql2date(' d | ', $post->post_date );
    $archives .= "< a href ='" . get_permalink($post->ID) . "'>" . $post->post_title . "";
    $archives .= "</li >";
    endforeach;
    $archives .= "</ul >";
    return $archives;
    }

  17. rss milo

    moderator


    rss Posted 3 years ago
    #

    recently updated posts

    <?php
    $today = current_time('mysql', 1);
    $howMany = 5; //Number of posts you want to display
    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
    ?>

    <h2><?php _e("Recent Updates"); ?></h2>
    <ul >
    <?php
    foreach ($recentposts as $post) {
    if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
    echo "<li >ID)."'>";
    the_title();
    echo '
    </li >';
    }
    ?>
    </ul >
    <?php endif; ?>

  18. rss milo

    moderator


    rss Posted 3 years ago
    #

    query using custom fields outside the loop

    <?php $quote = get_posts('post_type=page&showposts=1&meta_key=quote&orderby=rand'); foreach($quote as $post) : setup_postdata($post); ?>
    < li class="quote">< a href="<?php the_permalink(); ?>" title="Read the <?php the_title(); ?> case study"><?php echo get_post_meta($post->ID, "quote", true); ?>
    <em ><?php echo get_post_meta($post->ID, "quote-author", true); ?></em ></li >
    <?php endforeach; ?>

    $quote -- name of function post_type=page -- remove to query posts orderby=rand -- random order showposts=1 -- limit to 1 post

    effect -- displays 1 random custom excerpt outside of the loop

  19. rss milo

    moderator


    rss Posted 3 years ago
    #

    Latest Posts from Category

    <div class="pod">
    <h2>Latest News</h2>
    <ul >
    <?php query_posts('cat=CAT ID&showposts=NUMBER'); ?>
    <?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>
    <li >
    < a href= "<?php the_permalink() ?>" rel="bookmark" title="Permalink: <?php the_title(); ?>"><?php the_title(); ?>
    </li >
    <?php endif; endwhile; ?>
    </ul >
    </div>

  20. rss milo

    moderator


    rss Posted 3 years ago
    #

    If In Category

    <?php if (in_category('YOUR CATNAME')) : ?>
    <div class='eventinfo'>
    <h2>Event Details</h2>
    <?php the_meta(); ?>
    </div>
    <?php endif; ?>

  21. rss milo

    moderator


    rss Posted 3 years ago
    #

    query post for a specific page

    <?php $contact_drop = new WP_Query('pagename=contact'); while ($contact_drop->have_posts()) : $contact_drop->the_post(); $do_not_duplicate = $post->ID; ?>
    <div id="contact-slide">
    <h3 class="page-page-title"><?php the_title(); ?></h3>
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>

  22. rss milo

    moderator


    rss Posted 3 years ago
    #

    custom templates for posts

    function custom_template($template) {
    global $wp_query;
    $cats = get_the_category($wp_query->post->ID);
    $cat = $cats[0];
    if (file_exists(TEMPLATEPATH."/category-".$cat->cat_ID.'.php')) {
    return TEMPLATEPATH."/category-".$cat->cat_ID.'.php';
    }
    return $template;
    }

    add_filter('single_template', 'custom_template');

  23. rss milo

    moderator


    rss Posted 3 years ago
    #

    load jquery within header to use

    <?php wp_enqueue_script('jquery'); ?>

  24. rss milo

    moderator


    rss Posted 3 years ago
    #

    if custom field else

    <?php
    $number = get_post_meta($post->ID, 'number', true);
    if ( $number ) {
    echo 'there is a custom field value';
    }
    else {
    echo 'no custom field value';
    }
    ?>

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.