Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    Allow shortcodes in sidebar widgets, simply edit the functions.php file from your them and add the following code:

    add_filter('widget_text', 'do_shortcode');

    Once you saved the file, you can now add as many shortcodes as you want in sidebar widgets.

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    Simply paste the following code on the functions.php file from your theme. Create that file if it doesn’t exists by default.

    function insertRss($content) { if(is_feed()){ $content = 'text before content'.$content.'
    < a href = "//3oneseven.com"">Did you visited milo317 today?</ a >'; } return $content; } add_filter('the_content', 'insertRss');

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    To get a permalink outside of the loop, you have to use the get_permalink() function. That function takes a single argument, which is the ID of the post you’d like to get the permalink:

    < a href ="<?php echo get_permalink(10); ?>" >Read the article< /a >

    You can also use this function with the $post global variable:

    < a href = "<?php echo get_permalink($post->ID); ?>" >Read the article< /a >

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display the total number of trackbacks
    , first create a function. Paste the following code on the functions.php file from your theme:

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

    Once the file is saved, you can call the function anywhere you want:

    <?php tb_count(); ?>

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    Automatically remove code mistakes in posts

    create the function which will takes your post content, clean it and print or return it.
    Copy the code below to the functions.php file from your theme. Create that file if it doesn’t exists.

    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;
    }

    Proceed as the following to display your clean post content:

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

    The clean_bad_content() only takes one argument, a boolean to specify if you’d like to print the result (true) , or get it for further use in php (false).

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    New posts stands out

    Edit your index.php file and look for the loop. Replace it with that one:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    $currentdate = date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
    $postdate = get_the_time('Y-m-d');
    if ($postdate==$currentdate) {
    echo '<div class="post new">';
    } else {
    echo '<div class="post">';
    } ?>
    < a href ="<?php the_permalink() ?>" rel="bookmark">
    <?php the_title(); ?>< /a >
    <?php the_time('j F Y'); ?>
    </div>
    <?php endwhile; ?>
    <?php endif; ?>

    The above code will add the css class new if the post was published less than 24 hours ago. Then, you just have to modify your stylesheet a bit:

    .post{
    /* CSS style for "normal" posts */
    }

    .post.new {
    /* CSS style for newer posts */
    }

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    Integrate files on your blog header

    Create a function that will simply print the required files. Then, you got to hook it to the wp_head() WordPress function, by using the add_action() function.

    function GetLastPostName_head()
    {
    echo '<script type="text/javascript" src="'.get_settings('siteurl').'/wp-content/plugins/slidelastposttitle/mootools.js"></script>';
    echo '<script type="text/javascript" src="'.get_settings('siteurl').'/wp-content/plugins/slidelastposttitle/blackbox.js"></script>';
    }
    add_action('wp_head', 'GetLastPostName_head');

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Access post data outside the loop

    paste the following function on the functions.php file from your theme.
    This function takes a singles argument, which is the ID of the post you’d like to access data. It will return an array, containing post title, date, content, author id, post id, etc.

    function get_post_data($postId) {
    global $wpdb;
    return $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$postId");
    }

    To use the function, use the following anywhere on your theme files:

    <?php
    $data = get_post_data(10);
    echo $data[0]->post_title; //Print post title
    echo $data[0]->post_date; //Print post date
    echo $data[0]->comment_count; //Print number of comments
    echo $data[0]->post_content; /Print post content
    ?>

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    To be able to use html in user profiles, simply append the following line of code to the functions.php file from your theme. (Create that file if it doesn’t exists)

    remove_filter('pre_user_description', 'wp_filter_kses');

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get the value of a custom field

    paste it on your theme functions.php file. If your theme doesn’t have a file named functions.php, create one.

    function get_custom_field_value($szKey, $bPrint = false) {
    global $post;
    $szValue = get_post_meta($post->ID, $szKey, true);
    if ( $bPrint == false ) return $szValue; else echo $szValue;
    }

    Now, to call the function and get your custom field value, use the following code:

    <?php if ( function_exists('get_custom_field_value') ){
    get_custom_field_value('featured_image', true);
    } ?>

    First, we use the php function_exists() function to make sure the get_custom_field_value function is defined on our theme. If it is, we use it. The first argument is the custom field name (here, featured_image) and the second let you echo the value (true) or get it for further use in php (false).

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    Create a meta description function for your WordPress site

    Open your header.php file. Paste the following code anywhere within the <head> and </head> tags:

    <meta name="description" content="
    <?php if ( (is_home()) || (is_front_page()) ) {
    echo ('Your main description goes here');
    } elseif(is_category()) {
    echo category_description();
    } elseif(is_tag()) {
    echo '-tag archive page for this blog' . single_tag_title();
    } elseif(is_month()) {
    echo 'archive page for this blog' . the_time('F, Y');
    } else {
    echo get_post_meta($post->ID, "Metadescription", true);
    }?>">

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display how many times a post has been saved to Delicious

    To get your Delicious save count in plain text, simply open the single.php file from your theme and paste the following code where you’d like the count to be displayed.

    Delicous.com saves: <span id='del'>0</span>

    <script type='text/javascript'>
    function displayURL(data) {
    var urlinfo = data[0];
    if (!urlinfo.total_posts) return;
    document.getElementById("del").innerHTML = urlinfo.total_posts;
    }
    </script>

    <script src='http://badges.del.icio.us/feeds/json/url/data?url=<?php the_permalink() ?>&callback=displayURL'></script>

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    Hide WordPress version

    open the functions.php file from your theme and add the following line of code:

    remove_action('wp_head', 'wp_generator');

  14. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display only one sticky post on your homepage

    Open your index.php file, and replace your current loop by this one (replacing the stickies category name by the category of your choice) by :

    <?php if (have_posts()) :
    $my_query = new WP_Query('category_name=stickies&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

  15. rss milo

    moderator


    rss Posted 3 years ago
    #

    List WordPress category feeds

    paste the following code anywhere on your theme. It will output a list of your categories with a link to the category rss feed.

    <?php wp_list_categories('feed_image=http://www.myblog.com/image.gif&feed=XML Feed&optioncount=1&children=0'); ?>

    The two parameters used here are:

    * feed_image: The url of the image to display as a link to your feed.
    * feed: The feed format

  16. rss milo

    moderator


    rss Posted 3 years ago
    #

    List recent post on a non-WordPress site

    Simply paste the following code on your non WordPress site. You may need to upload the wp-config file from the blog you want to get the posts on the same directory.

    <?php
    $how_many=1; //how many posts to display
    require('blog1/wp-config.php'); //the path to the wp-config file of the blog I want to use
    $news=$wpdb->get_results("SELECT 'ID','post_title','post_content' FROM $wpdb->posts
    WHERE 'post_type'=\"post\" AND 'post_status'=\"publish\" ORDER BY post_date DESC LIMIT $how_many");

    foreach($news as $np){
    printf ("<div class='normalText'>%s</div>", $np->post_content);
    }?>

  17. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display adsense to search engines visitors only

    paste the code below in your theme functions.php file. Create that file if it doesn’t exists.

    function scratch99_fromasearchengine(){
    $ref = $_SERVER['HTTP_REFERER'];
    $SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
    foreach ($SE as $source) {
    if (strpos($ref,$source)!==false) return true;
    }
    return false;
    }

    The $SE array is where you specify search engines. You can easily ad new search engines by adding new elements to the array.

    Then, paste the following code anywhere on your template where you want your adsense ads to appear. They’ll be displayed only to visitors comming from search engines results.

    if (function_exists('scratch99_fromasearchengine')) {
    if (scratch99_fromasearchengine()) {
    INSERT YOUR CODE HERE
    }
    }

  18. rss milo

    moderator


    rss Posted 3 years ago
    #

    Display a “welcome back” message to your visitors

    paste the following code wherever you want on your template.

    <?php
    if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
    $lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
    echo "Welcome Back ". $lastCommenter ."!";
    } else {
    echo "Welcome, Guest!";
    }
    ?>

  19. rss milo

    moderator


    rss Posted 3 years ago
    #

    List latest registered users

    paste the following code anywhere on your template. It will list your 5 latest registered users. If you want to get more or less users, simply change the 5 in the SQL query.

    <h2>Latest registered users</h2>
    < ul >
    <?php
    $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5");

    foreach ($usernames as $username) {
    echo '< li >< a href ="'.$username->user_url.'">'.$username->user_nicename."</ a >< /li >";
    }
    ?>
    < /ul >

  20. rss milo

    moderator


    rss Posted 3 years ago
    #

    Create a “Save to Delicious” button

    Paste the following code within the loop, on your single.php file.

    < a href = "http://del.icio.us/post?url=<?php the_permalink();?>">Save this link to Delicious< /a >

  21. rss milo

    moderator


    rss Posted 3 years ago
    #

    Get posts published exactly one year ago

    simply paste the following code where you want posts published exactly one year ago to appear.

    <?php
    $current_day = date('j');
    $last_year = date(‘Y’)-1;
    query_posts('day='.$current_day.'&year='.$last_year);
    if (have_posts()):
    while (have_posts()) : the_post();
    the_title();
    the_excerpt();
    endwhile;
    endif;
    ?>

  22. rss milo

    moderator


    rss Posted 3 years ago
    #

    List scheduled posts

    paste the following code anywhere on your template where you want your scheduled posts to be listed. You can change the max number or displayed posts by changing the value of showposts in the query.

    <?php
    $my_query = new WP_Query('post_status=future&order=DESC&showposts=5');
    if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

  23. <?php the_title(); ?>
  24. <?php endwhile;
    }
    ?>

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    display private posts to logged users in the loop

    define a custom field named private, with true as a value, to each private post will have.

    Replace your current WordPress loop with that one:

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

  • rss milo

    moderator


    rss Posted 3 years ago
    #

    Display recently updated posts and pages

    Paste this code anywhere you’d like to display a list of recently updated posts and pages. You can specify how much item to show by editing the value of the $howMany variable.

    <?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 >< a href= '".get_permalink($post->ID)."'>";
    the_title();
    echo '< /a >< /li >';
    }
    ?>
    </ ul >
    <?php endif; ?>

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