Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Last Modified Time and Date for Posts

    Posted on <?php the_time('F jS, Y') ?>
    <?php $u_time = get_the_time('U');
    $u_modified_time = get_the_modified_time('U');
    if($u_modified_time != $u_time) {
    echo "and last modified on ";
    the_modified_time('F jS, Y');
    echo ". ";
    } ?>

  2. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Total Number of Trackbacks and Pingbacks

    functions

    function comment_count() {
    global $wpdb;
    $count = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = 'pingback' OR comment_type = 'trackback'";
    echo $wpdb->get_var($count);
    }

  3. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Private Posts to Logged-in Users

    <?php if (have_posts()) : while (have_posts()) : the_post();
    $private = get_post_custom_values("private");
    if (isset($private[0]) && $private == "true") {
    if (is_user_logged_in()) {
    // Display private post to logged user
    }
    } else {
    // Display public post to everyone
    }
    endwhile; endif; ?>

  4. rss milo

    moderator


    rss Posted 1 year ago
    #

    Disable Post Revisions

    wp config file

    /* disable post-revisioning nonsense */
    define('WP_POST_REVISIONS', FALSE);

  5. rss milo

    moderator


    rss Posted 1 year ago
    #

    Limit Post Revisions

    wp config file

    /* limit number of post revisions */
    define('WP_POST_REVISIONS', 3);

  6. rss milo

    moderator


    rss Posted 1 year ago
    #

    Automatically Remove Code Mistakes in Posts

    functions

    function clean_bad_content($bPrint = false) {
    global $post;
    $szPostContent = $post->post_content;
    $szRemoveFilter = array("~<p[^>]*>\s?</p>~", "~<a[^>]*>\s?~", "~<font[^>]*>~", "~<\/font>~", "~style\=\"[^\"]*\"~", "~<span[^>]*>\s?</span>~");
    $szPostContent = preg_replace($szRemoveFilter, '', $szPostContent);
    $szPostContent = apply_filters('the_content', $szPostContent);
    if ($bPrint == false) return $szPostContent;
    else echo $szPostContent;
    }

    template within loop

    <?php if (function_exists('clean_bad_content')) clean_bad_content(true); ?>

  7. rss milo

    moderator


    rss Posted 1 year ago
    #

    Random Posts

    functions

    function query_random_posts($query) {
    return query_posts($query . '&random=true');
    }
    class RandomPosts {
    function orderby($orderby) {
    if (get_query_var('random') == 'true')
    return "RAND()";
    else
    return $orderby;
    }
    function register_query_var($vars) {
    $vars[] = 'random';
    return $vars;
    }
    }
    add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
    add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );

    template

    <?php query_posts('cat=11&showposts=11&random=true'); ?>

  8. rss milo

    moderator


    rss Posted 1 year ago
    #

    Latest Comments

    <?php global $wpdb;
    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,50) // NUMBER OF CHARACTERS
    AS com_excerpt FROM $wpdb->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 5"; // NUMBER OF COMMENTS
    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML;
    $output .= "\n< ul>";

    foreach ($comments as $comment) {
    $output .= "\n< li>"."< a href=\"" . get_permalink($comment->ID) .
    "#comment-" . $comment->comment_ID . "\" title=\"on " .
    $comment->post_title . "\">" .strip_tags($comment->comment_author)
    .":
    <div>" . strip_tags($comment->com_excerpt)
    ."</div></ a></ li>";
    }
    $output .= "\n";
    $output .= $post_HTML;
    echo $output;
    ?>

  9. rss milo

    moderator


    rss Posted 1 year ago
    #

    Most Commented Posts

    < ul>
    <?php
    $result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); // NUMBER OF POSTS
    foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ($commentcount != 0) {
    ?>
    < li>< a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?>< /a></ li>

    <?php } } ?>
    < /ul>

  10. rss milo

    moderator


    rss Posted 1 year ago
    #

    Widgetizable Sidebars

    <?php // multiple widgetizable sidebars
    if (function_exists('register_sidebar')) {
    $sidebars = array('Home Sidebar', 'Post Sidebar', 'Page Sidebar');
    foreach($sidebars as $name) {
    register_sidebar(array('name'=> $name,
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h3 class="widgetTitle">',
    'after_title' => '</h3>',
    ));
    }
    }
    ?>

  11. rss milo

    moderator


    rss Posted 1 year ago
    #

    Three Columns

    template

    <?php // multiple columns
    $page_columns = explode("<--column-->", $post->post_content);
    echo $page_columns[0]; // before columns
    echo '<div class="first column">'.$page_columns[1].'</div>'; // first column
    echo '<div class="second column">'.$page_columns[2].'</div>'; // second column
    echo '<div class="third column">'.$page_columns[3].'</div>'; // third column
    ?>

    css

    /* column structure */
    .column {
    margin-right: 10px;
    float: left;
    width: 33%;
    }
    /* column styles */
    column.first, column.second, column.third {}

  12. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Posts with Specific Custom Fields

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php $custom_field = get_post_custom_values("name-of-custom-field"); ?>
    <?php if (isset($custom_field[0])) { ?>
    <h1>< a href="<?php the_permalink(); ?>"><?php the_title(); ?>< /a></h1>
    <?php the_content(); ?>
    <?php } else { ?>
    <h2>< a href="<?php the_permalink(); ?>"><?php the_title(); ?>< /a></h2>
    <?php the_excerpt(); ?>
    <?php } ?>
    <?php endwhile; endif; ?>

  13. rss milo

    moderator


    rss Posted 1 year ago
    #

    Related Posts without a Plugin

    <?php // related posts based on first tag of current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    echo '<h3>Related Posts</h3>';
    $first_tag = $tags[0]->term_id;
    $args = array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts' => 7, // how many posts?
    'caller_get_posts' => 1
    );
    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) { ?>
    < ul>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    < li>< a href="<?php the_permalink(); ?>"><?php the_title(); ?>< /a>< /li>
    <?php endwhile; ?>
    < /ul>
    <?php } ?>
    <?php } ?>

  14. rss milo

    moderator


    rss Posted 1 year ago
    #

    Disable Widgets

    functions

    <?php // disable all widgets
    function disable_all_widgets($sidebars_widgets) {
    $sidebars_widgets = array(false);
    return $sidebars_widgets;
    }
    add_filter('sidebars_widgets', 'disable_all_widgets');
    ?>

  15. rss milo

    moderator


    rss Posted 1 year ago
    #

    Private Page in Navigation Menu

    < ul>
    <?php // add a private page to your navigation menu
    wp_list_pages('depth=1&title_li=0&sort_column=menu_order');
    if(current_user_can('read_private_pages')) : ?>
    < li>< a href="<?php echo get_permalink(10); ?>">For Authors only< /a>< /li>
    <?php endif; ?>
    </ ul>

  16. rss milo

    moderator


    rss Posted 1 year ago
    #

    Additional Links to wp_list_pages

    // include additional links
    function add_bookmarks_to_menu($output) {
    $bookmarks = (array) get_bookmarks('hide_invisible=0&category_name=wp_list_pages');
    foreach ($bookmarks as $bookmark) {
    $output .= "< li>< a href='{$bookmark->link_url}' title='{$bookmark->link_name}'>{$bookmark->link_name}< /a></ li>\n";
    }
    return $output;
    }
    add_filter('wp_list_pages', 'add_bookmarks_to_menu');

  17. rss milo

    moderator


    rss Posted 1 year ago
    #

    Customizing the Dashboard

    add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
    function my_custom_dashboard_widgets() {
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    wp_add_dashboard_widget('custom_help_widget', 'Help and Support', 'custom_dashboard_help');
    }
    function custom_dashboard_help() {
    echo '<p>Welcome to your custom theme! Need help? Contact the developer here.</p>';
    }

  18. rss milo

    moderator


    rss Posted 1 year ago
    #

    Customizing the Contextual Help Dropdown

    //hook loading of new page and edit page screens
    add_action('load-page-new.php','add_custom_help_page');
    add_action('load-page.php','add_custom_help_page');
    function add_custom_help_page() {
    //the contextual help filter
    add_filter('contextual_help','custom_page_help');
    }
    function custom_page_help($help) {
    //keep the existing help copy
    echo $help;
    //add some new copy
    echo "<h5>Custom Features</h5>";
    echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>";
    }

  19. rss milo

    moderator


    rss Posted 1 year ago
    #

    Your Own Logo

    //hook the administrative header output
    add_action('admin_head', 'my_custom_logo');

    function my_custom_logo() {
    echo '
    <style type="text/css">
    #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
    </style>
    ';
    }

  20. rss milo

    moderator


    rss Posted 1 year ago
    #

    Custom Meta Box

    /* Use the admin_menu action to define the custom boxes */
    add_action('admin_menu', 'nyc_boroughs_add_custom_box');
    /* Adds a custom section to the "side" of the post edit screen */
    function nyc_boroughs_add_custom_box() {
    add_meta_box('nyc_boroughs', 'Applicable Borough', 'nyc_boroughs_custom_box', 'post', 'side', 'high');
    }
    /* prints the custom field in the new custom post section */
    function nyc_boroughs_custom_box() {
    //get post meta value
    global $post;
    $custom = get_post_meta($post->ID,'_nyc_borough',true);
    // use nonce for verification
    echo '<input type="hidden" name="nyc_boroughs_noncename" id="nyc_boroughs_noncename" value="'.wp_create_nonce('nyc-boroughs').'" />';
    // The actual fields for data entry
    echo '<label for="nyc_borough">Borough</label>';
    echo '<select name="nyc_borough" id="nyc_borough" size="1">';
    //lets create an array of boroughs to loop through
    $boroughs = array('Manhattan','Brooklyn','Queens','The Bronx','Staten Island');
    foreach ($boroughs as $borough) {
    echo '<option value="'.$borough.'"';
    if ($custom == $borough) echo ' selected="selected"';
    echo '>'.$borough.'</option>';
    }
    echo "</select>";
    }
    /* use save_post action to handle data entered */
    add_action('save_post', 'nyc_boroughs_save_postdata');
    /* when the post is saved, save the custom data */
    function nyc_boroughs_save_postdata($post_id) {
    // verify this with nonce because save_post can be triggered at other times
    if (!wp_verify_nonce($_POST['nyc_boroughs_noncename'], 'nyc-boroughs')) return $post_id;
    // do not save if this is an auto save routine
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    $nyc_borough = $_POST['nyc_borough'];
    update_post_meta($post_id, '_nyc_borough', $nyc_borough);
    }

  21. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display an Author List

    function contributors() {
    global $wpdb;

    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");

    foreach($authors as $author) {
    echo "< li>";
    echo "< a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    echo get_avatar($author->ID);
    echo "< /a>";
    echo '<div>';
    echo "< a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "</ a>";
    echo "</div>";
    echo "< /li>";
    }
    }

  22. rss milo

    moderator


    rss Posted 1 year ago
    #

    Extend the User Contact

    functions

    function my_new_contactmethods( $contactmethods ) {
    // Add Twitter
    $contactmethods['twitter'] = 'Twitter';
    //add Facebook
    $contactmethods['facebook'] = 'Facebook';
    return $contactmethods;
    }
    add_filter('user_contactmethods','my_new_contactmethods',10,1);

    template

    <?php
    $current_author = get_userdata(get_query_var('author'));
    ?>
    < a href="<?php echo esc_url($current_author->twitter);?>" title="Twitter"> Follow me on Twitter< /a>

  23. rss milo

    moderator


    rss Posted 1 year ago
    #

    load plugins on pages

    add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
    if ( !is_page('Events') ) {
    wp_deregister_script( 'wpng-calendar' );
    wp_deregister_script( 'date-js' );
    wp_deregister_script( 'thickbox-js' );
    wp_deregister_script( 'jquery-js' );
    wp_deregister_script( 'wiky-js' );
    }
    }

    If you need to show the plugin on multiple pages, change the if line to this:

    if ( !is_page(array(2,'events','about-us')) ) {

  24. rss milo

    moderator


    rss Posted 1 year ago
    #

    Show Number of Retweets

    Sign up with Backtype to use get an API key.
    Once you have that, put it in next to $key.

    <?php
    $link = get_permalink($post->ID);
    $key = 'your key';
    $url = "http://api.backtype.com/tweetcount.xml?q=$link&key=$key";
    $request = new WP_Http;
    $result = $request->request( $url );
    echo "<span class=\"result\">".$result['body']."</span> tweets";
    ?>

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.