Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    use initial caps

    add this to your function.php

    function initial_cap($content){
    // Regular Expression, matches a single letter
    // * even if it's inside a link tag.
    $searchfor = '/>(< a >]+>)?([^<s])/';
    // The string we're replacing the letter for
    $replacewith = '>$1<span class="capital">$2</span>';
    // Replace it, but just once (for the very first letter of the post)
    $content = preg_replace($searchfor, $replacewith, $content, 1);
    // Return the result return $content; }
    // Add this function to the WordPress hook
    add_filter('the_content', 'initial_cap');

    add the styling to your css

    span.initialcap{ float: left; font-size: 40px; line-height: 35px; /* Double the line height minus 1 pixel */ font-family: Georgia, "Times New Roman", Times, serif; color: #2583ad; /* Blue */ padding-right: 5px; }

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    display a random ‘Read More’ link

    Copy and paste this code somewhere in the index.php or functions.php file of your theme (but make sure you paste it before the loop):

    <?php $more_strings = array("Read More...", "Keep on Reading...", "Wait! There's more...", "Read the rest of the article..."); ?>

    You can add as many strings as you want.

    Then change the the_content(); bit to this:

    the_content($more_strings[rand(0,count($more_strings))]);

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    Category feeds

    You can add this to your archive.php with the following code:

    <?php if(is_category()) : ?>
    < a href ="<?php echo bloginfo("url") . "/wp-rss2.php?cat=$cat" ?>">Subscribe to the <?php single_cat_title(); ?> category< / a >
    <?php endif; ?>

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    is_subpage() - Custom Conditional Function

    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php if ( is_subpage() ): ?> <p> This is a subpage </p> <?php endif ?> <?php endwhile; ?>

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    List WordPress Users

    < ul id="membersList" >
    <?php
    /* First we set how we'll want to sort the user list.
    * ID - User ID number.
    * user_login - User Login name.
    * user_nicename - User Nice name ( nice version of login name ).
    * user_email - User Email Address.
    * user_url - User Website URL.
    * user_registered - User Registration date.
    */
    $szSort = "user_nicename";
    /*
    Now we build the custom query to get the ID of the users.
    */
    $aUsersID = $wpdb->get_col( $wpdb->prepare( "SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC" , $szSort ));
    /*
    Once we have the IDs we loop through them with a Foreach statement.
    */
    foreach ( $aUsersID as $iUserID ) :
    /*
    We use get_userdata() function with each ID. */ $user = get_userdata( $iUserID );
    /*
    Here we finally print the details wanted. Check the description of the database tables linked above to see all the fields you can retrieve. To echo a property simply call it with $user->name_of_the_column. In this example I print the first and last name.
    */
    echo '< li >' . ucwords( strtolower( $user->first_name . ' ' . $user->last_name ) ) . '< /li >';
    /*
    The strtolower and ucwords part is to be sure the full names will all be capitalized. */
    endforeach; // end the users loop. ?> < /ul >

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get Images Attached to a Post

    use this in your function.php file

    function bdw_get_images() {
    // Get the post ID
    $iPostID = $post->ID;
    // Get images for this post
    $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
    // If images exist for this page
    if($arrImages) {
    // Get array keys representing attached image numbers
    $arrKeys = array_keys($arrImages);
    // Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
    foreach($arrImages as $oImage) { $arrNewImages[] = $oImage; }
    // Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
    for($i = 0; $i < sizeof($arrNewImages) - 1; $i++) { for($j = 0; $j < sizeof($arrNewImages) - 1; $j++) { if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order) { $oTemp = $arrNewImages[$j]; $arrNewImages[$j] = $arrNewImages[$j + 1]; $arrNewImages[$j + 1] = $oTemp; } } }
    // Reset arrKeys array
    $arrKeys = array();
    // Replace arrKeys with newly sorted object ids
    foreach($arrNewImages as $oNewImage) { $arrKeys[] = $oNewImage->ID; }
    // Get the first image attachment
    $iNum = $arrKeys[0];
    // Get the thumbnail url for the attachment
    $sThumbUrl = wp_get_attachment_thumb_url($iNum);
    // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL //
    $sImageUrl = wp_get_attachment_url($iNum);
    // Build the string
    $sImgString = '< a href= "' . get_permalink() . '">' . '' . '< /a >'; // Print the image
    echo $sImgString; } }

    call the function

    bdw_get_images();

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    A Custom Read More

    <?php $custommore = get_post_meta($post->ID, 'custom_more', true); ?> <?php if (!$custommore) { $custommore = 'Read More »'; } ?> <?php the_content($custommore); ?>

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Thumbnailed Recent Posts

    create a thumbnail for each of your last 5 posts (and every subsequent post going forward). Upload it and then paste it's location into a new custom field with a key called thumb.

    create a new Loop somewhere in one of your template files that pulls this thumbnail in and displays it in an unordered list

    < ul class="thumb_recent" >
    <?php query_posts('showposts=5'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php $thumbnail = get_post_meta($post->ID, 'thumb', true); ?>
    < li > < a href ="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    " alt="<?php the_title(); ?>" /> <span>
    <?php the_title(); ?></span>< /a > < /li >
    <?php endwhile; endif; ?>
    < /ul >;

    style it via css:

    ul.thumb_recent {
    list-style:none;
    margin:30px 0 0 0;
    padding:0;
    }

    ul.thumb_recent li {
    float:left;
    margin:0 5px 0 0;
    position:relative;
    }

    ul.thumb_recent li img {border:3px solid #7BA2C7;}
    ul.thumb_recent li a:hover img {border:3px solid #666;}
    ul.thumb_recent li span {
    text-decoration:none;
    display:none;
    text-align:left;
    position:absolute;
    top:-15px;
    left:0px;
    width:400px;
    color:#666;
    text-transform:uppercase;
    font-family:arial;
    font-size:11px;
    }

    ul.thumb_recent li a:hover span {display:block;}

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    Post Specific CSS Overrides

    create a custom field called my-css. For its value, paste your new CSS: simple example:
    body { background:#000 !important; }

    insert that custom field CSS into the header.php file. In that template file, find the line that declares your CSS stylesheet, and paste this code below it.

    <?php if (is_single()) { ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $newcss = get_post_meta($post->ID, 'my-css', true); ?>
    <style type="text/css">
    <?php echo $newcss; ?>
    </style>
    <?php endwhile; ?>
    <?php rewind_posts(); ?>
    <?php } ?>

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    .htaccess File for WordPress sites

    # protect the htaccess file
    <files .htaccess>
    order allow,deny
    deny from all
    </files>

    # disable the server signature
    ServerSignature Off

    # limit file uploads to 10mb
    LimitRequestBody 10240000

    # protect wpconfig.php
    <files wp-config.php>
    order allow,deny
    deny from all
    </files>

    #who has access who doesnt
    order allow,deny
    #deny from 000.000.000.000
    allow from all

    #custom error docs
    ErrorDocument 404 /notfound.php
    ErrorDocument 403 /forbidden.php
    ErrorDocument 500 /error.php

    # disable directory browsing
    Options All -Indexes

    #redirect old to new
    Redirect 301 /old.php http://www.yourdomain.com/new.php

    #block referring domains
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} digg\.com [NC]
    RewriteRule .* - [F]

    #disable hotlinking of images with forbidden or custom image option
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
    #RewriteRule \.(gif|jpg)$ - [F]
    #RewriteRule \.(gif|jpg)$ http://www.yourdomain.com/stealingisbad.gif [R,L]

    # php compression - use with caution
    <ifmodule mod_php4.c>
    php_value zlib.output_compression 16386
    </ifmodule>

    # set the canonical url
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

    # protect from spam comments
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
    RewriteCond %{HTTP_REFERER} !.*yourdomain.com.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    Show Category Images

    <?php foreach((get_the_category()) as $cat)
    {
    $catname =$cat->category_nicename;
    echo "< a href \"/category/";
    echo $catname;
    echo "/\">";
    echo "<img src\"/wp-content/cat-icons/";
    echo $catname;
    echo ".png\" alt=\"$catname category image\" border=\"0\" />< /a >\n"; } ?>

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    Exclude posts or pages from search results

    paste the following code on the functions.php file from your theme. In this example, posts with IDs 8 and 15 will be excluded from your blog’s search results:

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set('cat','8,15');
    }
    return $query;
    }

    add_filter('pre_get_posts','SearchFilter');

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    Retrieve images in post content

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $szPostContent = $post->post_content;
    $szSearchPattern = '~]*\ />~';

    // Run preg_match_all to grab all the images and save the results in $aPics preg_match_all( $szSearchPattern, $szPostContent, $aPics );

    // Check to see if we have at least 1 image
    $iNumberOfPics = count($aPics[0]);
    if ( $iNumberOfPics > 0 ) {

    // Now here you would do whatever you need to do with the images
    // For this example the images are just displayed
    for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
    echo $aPics[0][$i]; }; };

    endwhile; endif; ?>

  14. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get posts having a specific custom field

    <?php if (have_posts()) : while (have_posts()) : the_post(); $customField = get_post_custom_values("img"); if (isset($customField[0])) {
    //Custom field is set, display post info
    the_title();
    the_excerpt(); }
    endwhile; endif; ?>

    where "img" is the custom key

  15. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display a list of most commented posts from the year 2008:

    <h2 >Most commented posts from 2008 /h2>
    <ul >
    <?php
    $result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2008-01-01' AND '2008-12-31' 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) {
    ?>
    <li >< a href ="<?php echo get_permalink($postid); ?>">
    <?php echo $title ?>< /a>< /li>
    <?php } } ?>
    < /ul>

  16. rss milo

    moderator


    rss Posted 3 years ago
    #

    Style author comments in WordPress 2.7

    use this class in your CSS file

    li.bypostauthor { /* CSS styles for author comments */ }
    li.byuser { /* CSS styles for registered users comments */ }

    then add your preferred style

  17. rss milo

    moderator


    rss Posted 3 years ago
    #

    query_posts() Pagination

    <?php if (have_posts()) : ?> <?php query_posts("category_name=somecat"); ?> <?php while (have_posts()) : the_post(); ?>

    replace with

    <?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(); ?>

  18. rss milo

    moderator


    rss Posted 3 years ago
    #

    Loading CSS stylesheet with dynamic values

    /*** file: header.php ******************************/

    <link rel="stylesheet" type="text/css" media="screen" href="<?php //Import stylesheet for the different pages if ( is_frontpage() || is_page('index') || preg_match( "#/index/#i" , $_SERVER['REQUEST_URI']) ) { print getdir().'/home.css'; } else { echo getdir().'/sub.css.php?p='; if ( is_home() || is_single() || is_archive() || preg_match("#/blog/#i",$_SERVER['REQUEST_URI']) ) print 'blog'; else { $post_parent = get_post($post->post_parent); print $post_parent->post_name; } } ?>" />

    /*** file: sub.css.php ******************************/

    <?php header('Content-type: text/css'); ?> div#sidebar_photo { background-image: url("images/sidebar_photo<?php if ( isset($_REQUEST['p']) ) { switch ($_REQUEST['p']) { case 'about': print '4'; break; case 'blog': print '5'; break; case 'team': print '1'; break; case 'products': print '6'; break; case 'howtobuy': print '3'; break; case 'spreadtheword': print '2'; break; case 'join': print '7'; break; case 'share': print '8'; break; default: print '1'; break; } } ?>.jpg"); }

  19. rss milo

    moderator


    rss Posted 3 years ago
    #

    most commented post code

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts 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) { ?> < li>< a href ="<?php echo get_permalink($postid); ?>"><?php echo $title ?>< /a>< /li> <?php } } ?>

  20. rss milo

    moderator


    rss Posted 3 years ago
    #

    Reverse comment order

    <?php $comments = array_reverse($comments, true); ?>
    <?php foreach ($comments as $comment) : ?>
    content here
    <?php endforeach; ?>

  21. rss milo

    moderator


    rss Posted 3 years ago
    #

    Reverse post order

    check your index.php file, look for this bit of code:

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    Right before that line, add this code:

    <?php query_posts($query_string . “&order=ASC”) ?>

  22. rss milo

    moderator


    rss Posted 3 years ago
    #

    Conditional Tags

    <?php
    if ( is_front_page() ) { include (TEMPLATEPATH . '/home.php'); }
    else { include (TEMPLATEPATH . '/other.php'); }
    ?>

    Some other Conditional Tags:

    is_home(), is_category(), is_archive(), is_search(), is_single(), is_date(), is_404(), etc...

  23. rss milo

    moderator


    rss Posted 3 years ago
    #

    By default, WordPress category permalinks are displayed that way:

    http://yoursite.com/category/yourcatname

    how to remove it, backup your current .htaccess file. Then, open it and append the following line:

    RewriteRule ^category/(.+)$ http://www.yoursite.com/$1 [R=301,L]
    Once saved, your categories pages will be displayed like this:

    http://yoursite.com/yourcatname

    ATTENTION: won't work on hosts like bluehost or similar due to their
    installment

  24. rss milo

    moderator


    rss Posted 3 years ago
    #

    show privates pages auto in navigation menus

    <ul > <?php 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</li > <?php endif; ?> </ul >

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.