Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    different single template per category

    create each different layouts you’d like to use. In this recipes, they’re named single1.php and single2.php. The default posts template is single_default.php.

    insert the following code on your regular single.php file.

    <?php
    $post = $wp_query->post;

    if (in_category('1')) {
    include(TEMPLATEPATH.'/single1.php');
    } elseif (in_category('2')) {
    include(TEMPLATEPATH.'/single2.php');
    } else {
    include(TEMPLATEPATH.'/single_default.php');
    }
    ?>

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    Create a dynamic sidebar

    specify a sidebar name to be inclued:

    <?php get_sidebar('name'); ?>

    The above code will include the file named sidebar-name.php.

    The following code will include a custom sidebar according to the category you are on:

    <?php
    //to be able to use this outside the loop
    if ( have_posts() ) { the_post(); rewind_posts(); }

    if ( in_category('1') ) {
    get_sidebar('cat1');
    //gets sidebar-cat1.php
    } elseif ( in_category('2') ) {
    get_sidebar('cat2');
    //gets sidebar-cat2.php
    } elseif ( in_category('3') ) {
    get_sidebar('cat3');
    //gets sidebar-cat3.php
    } elseif ( in_category('4') || in_category('5') || in_category('6') ) {
    get_sidebar('catRest');
    //gets sidebar-catRest.php
    } else {
    get_sidebar()
    //gets sidebar.php
    }
    ?>

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display your latest twitter entry on your WP blog

    paste the code below anywhere on your blog.

    <?php

    // Your twitter username.
    $username = "TwitterUsername";

    // Prefix - some text you want displayed before your latest tweet.
    // (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
    $prefix = "<h2>My last Tweet</h2>";

    // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
    $suffix = "";

    $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

    function parse_feed($feed) {
    $stepOne = explode("<content type=\"html\">", $feed);
    $stepTwo = explode("</content>", $stepOne[1]);
    $tweet = $stepTwo[0];
    $tweet = str_replace("<", "<", $tweet);
    $tweet = str_replace(">", ">", $tweet);
    return $tweet;
    }

    $twitterFeed = file_get_contents($feed);
    echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
    ?>

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    List posts by author

    paste the following code where you want to list the post from a specific author:

    $numposts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = 1");

    echo "Posts by Author 1";
    < ul >
    foreach ($numposts as $numpost) {
    echo "< li >".$numpost->post_title."< /li >";
    }
    < /ul >

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    Control post revisions

    edit your wp-config.php file.
    To limit post revisions, just add the following lines:

    define ('WP_POST_REVISIONS', 5); //Defines a maximum of 5 different revisions per post.
    define('AUTOSAVE_INTERVAL', 3600); // Auto-saves on 1 hour interval

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    Change author attribution on all posts

    use phpmyadmin with the following commands to modify your WordPress database, don’t forget to create a backup before running any command throught phpmyadmin.

    The first thing to do is getting the IDs of WordPress users. Once logged in phpmyadmin, insert the following SQL command:

    SELECT ID, display_name FROM wp_users;

    Right now, phpmyadmin displayed a list of WordPress users associated with their IDs. Let’s say that NEW_AUTHOR_ID is the ID of the “new” author, and OLD_AUTHOR_ID is the old author ID.

    UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    Add a rss link on each post

    edit the single.php file from your theme.
    Locate the WordPress loop, and simply add the link below.

    <?php if (have_posts()) :
    while (have_posts()) : the_post();
    // WordPress loop ?>
    <div class="rss-box">< a href= " http://feeds.feedburner.com/ADRESS">Enjoyed this post? Suscribe to my RSS feeds!< /a ></div>
    <?php endwhile; ?>
    <?php endif; ?>

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Limit the size of the post excerpt

    Edit your single.php file and replace the the_excerpt() function by the following code:

    <?php
    $len = 50; //Number of words to display in excerpt
    $newExcerpt = substr($post->post_excerpt, 0, $len); //truncate excerpt according to $len
    if(strlen($newExcerpt) < strlen($post->post_excerpt)) {
    $newExcerpt = $newExcerpt."[...]";
    }
    echo "<p>".$newExcerpt."</p>"; //finally display excerpt
    ?>

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    Exclude categories from your rss feed

    you’ll have to know the numeric ID of the categories you want to exclude, open the functions.php file from your theme.

    Paste the following code in it:

    function myFilter($query) {
    if ($query->is_feed) {
    $query->set('cat','-5'); //Don't forget to change the category ID =^o^=
    }
    return $query;
    }

    add_filter('pre_get_posts','myFilter');

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display how many post have been published today

    <?php $today = date("Y-m-d H:i:s"); //Today's date $daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (1 * 24 * 60 * 60)); //Today - 1 day $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_date BETWEEN '$daysago' AND '$today'"); if ($numposts >0) { echo $numposts.' posts published today'; } else { echo "No posts published today". ?>

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    Use multiple custom headers

    Create replicates of your current header.php and insert your custom stuff,
    then replace the content of your header.php file with the following code:

    <?php
    if (is_page('contact')){
    <?php include(TEMPLATEPATH.'/headercontact.php'); ?>
    }
    elseif (is_page('gallery')){
    <?php include(TEMPLATEPATH.'/headergallery.php'); ?>
    }
    else {
    <?php include(TEMPLATEPATH.'/headerdefault.php'); ?>
    }
    ?>

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    Set post expiration date/time

    replace your current WordPress loop by this “hacked” loop:

    <?php
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    $expirationtime = get_post_custom_values('expiration');
    if (is_array($expirationtime)) {
    $expirestring = implode($expirationtime);
    }

    $secondsbetween = strtotime($expirestring)-time();
    if ( $secondsbetween > 0 ) {
    // For exemple...
    the_title();
    the_excerpt();
    }
    endwhile;
    endif;
    ?>

    create a custom field, then give it expiration as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get most commented posts of the week

    <?php $days = 7; //To fetch posts published during the last 7 days $today = date("Y-m-d H:i:s"); //Today's date $daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (7 * 24 * 60 * 60)); //Today - $days $result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN $daysago AND $today ORDER BY comment_count DESC LIMIT 0 , 10"); foreach ($result as $topten) { $postid = $topten->ID; $title = $topten->post_title; $commentcount = $topten->comment_count; if ($commentcount != 0) { ?>

  14. "><?php echo $title ?>
  15. <?php } } ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Create a page to display a random post

    create a page template, paste the following code in your new page template:

    <?php
    query_posts(array('orderby' => 'rand', 'showposts' => 1));
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    // WordPress loop, your random post will appear here
    endwhile;
    endif; ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Display the total number of comments

    $numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
    if (0 < $numcomms) $numcomms = number_format($numcomms);

    Right now, the $numcomms variable contains the total number of comments posted on your WordPress blog. To display this number, simply do something like:

    <?php echo "There's ".$numcomms." comments on this blog";

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Insert Adsense after the first post

    Paste it on your index.php file, instead of your current WP loop.

    <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count < 3) : ?>
    //Paste your Adsense code here
    <?php the_excerpt(); ?>
    <?php else : ?>
    <?php the_excerpt(); ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Display the total number of posts

    $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
    if (0 < $numposts) $numposts = number_format($numposts);

    Right now, the $numposts variable contains the total number of posts. You now just have to display it where you want:

    <?php echo $numposts.' has been published since January 12, 2008'; ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    display 1 full post and 3 excerpts

    <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count < 2) : ?> <?php the_content() ?> <?php else : ?> <?php the_excerpt(); ?> <?php endif; ?> <?php endwhile; ?> <?php endif; ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Display today’s posts

    $current_day = date('j'); query_posts('day='.$current_day); if (have_posts()) : while (have_posts()) : the_post(); ?> // WordPress loop endwhile; endif; ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Create Your Own WordPress Login Design

    create a folder called login within your theme files,
    place your images there,
    create a css file called login..css
    paste this in your functions file:

    custom_login() { echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('template_directory') . '/login/login.css" />'; } add_action('login_head', 'custom_login');

    add your CSS, this is the actual login.css used:

    * { margin: 0; padding: 0; }

    body {
    border-top-width: 30px;
    border-top-style: solid;
    font: 11px "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;
    }

    form {
    margin-left: 8px;
    padding: 16px 16px 40px 16px;
    font-weight: normal;
    -moz-border-radius: 11px;
    -khtml-border-radius: 11px;
    -webkit-border-radius: 11px;
    border-radius: 5px;
    background: #fff;
    border: 1px solid #e5e5e5;
    -moz-box-shadow: rgba(200,200,200,1) 0 4px 18px;
    -webkit-box-shadow: rgba(200,200,200,1) 0 4px 18px;
    -khtml-box-shadow: rgba(200,200,200,1) 0 4px 18px;
    box-shadow: rgba(200,200,200,1) 0 4px 18px;
    }

    form .forgetmenot { font-weight: normal; float: left; margin-bottom: 0; }

    #login form .submit input {
    font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;
    padding: 3px 10px;
    border: none;
    font-size: 12px;
    border-width: 1px;
    border-style: solid;
    -moz-border-radius: 11px;
    -khtml-border-radius: 11px;
    -webkit-border-radius: 11px;
    border-radius: 11px;
    cursor: pointer;
    text-decoration: none;
    margin-top: -3px;
    text-shadow: rgba(0,0,0,0.3) 0 -1px 0;
    }

    #login form p {
    margin-bottom: 0;
    }

    label {
    color: #777;
    font-size: 13px;
    }

    form .forgetmenot label {
    font-size: 11px;
    line-height: 19px;
    }

    form .submit { float: right; }

    form p { margin-bottom: 24px; }

    h1 a {
    background: url(../images/logo-login.gif) no-repeat top center;
    width: 326px;
    height: 67px;
    text-indent: -9999px;
    overflow: hidden;
    padding-bottom: 15px;
    display: block;
    }

    #nav {
    text-shadow: rgba(255,255,255,1) 0 1px 0;
    }

    #backtoblog a {
    position: absolute;
    top: 7px;
    left: 15px;
    text-decoration: none;
    }

    #login { width: 320px; margin: 7em auto; }

    #login_error, .message {
    margin: 0 0 16px 8px;
    border-width: 1px;
    border-style: solid;
    padding: 12px;
    -moz-border-radius: 3px;
    -khtml-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    }

    #nav { margin: 0 0 0 8px; padding: 16px; }

    #user_pass, #user_login, #user_email {
    font-size: 24px;
    width: 97%;
    padding: 3px;
    margin-top: 2px;
    margin-right: 6px;
    margin-bottom: 16px;
    border: 1px solid #e5e5e5;
    background: #fbfbfb;
    }

    modify it to your needs and look.

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Page break with wp-query

    <?php if (have_posts()) : ?> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=somecat&paged=$paged"); ?> <?php while (have_posts()) : the_post(); ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Image Metadata in WordPress

    add function in functions.php:

    function myMetaData($pic) {
    $html = '';
    $meta = wp_get_attachment_metadata($pic);
    if($meta) {
    $html = '

      ';
      foreach($meta['image_meta'] as $key => $value) {
      $html .= '
    • ' . $key . ': ' . $value . '
    • ';
      }
      $html .= '
    ';
    }
    return $html;
    }

    In image.php of the default theme we are inserting above the_content (Line 12) the new function:

    <?php echo myMetaData($post->ID); ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Archives page with all the posts in chronological order

    create a new php file and type this at the very beginning:

    <?php
    /*
    Template Name: Hierarchical Archives
    */
    ?>

    open the page.php file of your theme, and copy the structure, then add this

    <?php // Declare some helper vars $previous_year = $year = 0; $previous_month = $month = 0; $ul_open = false; // Get the posts $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC'); ?>
    <?php foreach($myposts as $post) : ?>
    <?php // Setup the post variables setup_postdata($post); $year = mysql2date('Y', $post->post_date); $month = mysql2date('n', $post->post_date); $day = mysql2date('j', $post->post_date); ?>
    <?php if($year != $previous_year || $month != $previous_month) : ?>
    <?php if($ul_open == true) : ?>
    < /ul >
    <?php endif; ?>
    <h3><?php the_time('F Y'); ?></h3>
    < ul class="your class" >
    <?php $ul_open = true; ?>
    <?php endif; ?>
    <?php $previous_year = $year; $previous_month = $month; ?>
    < li ><span class="the_day"><?php the_time('j'); ?></span>
    <span class="the_article">
    < a href = "<?php the_permalink(); ?>"><?php the_title(); ?>< /a ></span>< /li > <?php endforeach; ?> < /ul >

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Make a random post button

    <?php $randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1"); echo '< a href="'.$randomPost.'">Random Post< /a >'; ?>

  • 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.