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

  1. rss milo

    moderator


    rss Posted 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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;
    ?>

Topic Closed

This topic has been closed to new replies.