Friday, September 14, 2012

Get post content by ID in Wordpress

When we building a custom theme in wordpress, sometimes we need to get the title or content from a specific post in wordpress. Also sometimes, using a custom loop is the better option for us, but its hard when we need to get information from a specific post only,follow the instruction to overcome that



Just use the get_post_field() function. You can get the post content from it. Fine the following example which will display the content of the post with id $post_id:

echo get_post_field('post_content', $post_id);

Also, the function can be used to get other fields from the post. See the codex page for reference in wordpress.

Tuesday, August 21, 2012

How to create a dropdown menu of tags in wordpress

How to create a dropdown menu of tags in wordpress

Like wp_dropdown_categories() function which display the categories in a dropdown menu, there’s no built in function to display dropdown menu of tags in wordpress. But to display tags in a dropdown as well, Let’s use wp_dropdown_categories() and modify it in order to be able to display tags in a dropdown menu. Just paste the following code where you want the dropdown menu to be displayed. Here we can use our own taxonomy: Just modify the code on line 5 according to your needs.


<h2><?php _e('Posts by Tags'); ?></h2>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$select = wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div></form>

Sunday, July 22, 2012

How to execute shortcodes inside custom fields in wordpress

How to execute shortcodes inside custom fields in wordpress

WordPress by default do not allows shortcodes to be executed inside custom fields. If you want to display something with the help of shortcode inside a specific custom field, then you have to follow the simple steps

Put the following code into whatever page where you are displaying the results of the shortcode, and then change the my_wp_custom_field_here to the name of your own custom field.

<?php echo apply_filters('the_content', get_post_meta($post->ID, 'my_wp_custom_field_here', true)); ?>

Saturday, June 16, 2012

How to Remove the Width and Height Attributes From WP Image Uploader

How to Remove the Width and Height Attributes From WP Image Uploader


When we upload images in WordPress using image uploader and insert that image into wordpress posts, WordPress always automatically include the image width and height attributes in the html <img> tag. But sometimes that can give us some problem, because that may not fit in our layout . So using the following code we can get rid of those height and width attributes.

Just paste the following code into  functions.php file under theme folder.

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

Tuesday, June 5, 2012

How to display RSS feeds on WordPress

How to display RSS feeds on WordPress

If you ever want to display any rss feed on your WordPress blog then follow the process. The simple code that will do the job without using the deprecated wp_rss() function in wordpress.


The following code you have to paste in template pages wherever you wanna display the rss feed.
Please  update the feed url on line 3. and the Number of items you want to display can be defined on line 6.

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://sandipdas.in/feed';
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
    $maxitems = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        echo "<ul>\n";
        foreach ( $rss_items as $item ) :
            echo '<li>';
            echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . "</a>\n";
            echo '<p>' . $item->get_description() . "</li>\n";
        endforeach;
        echo "</ul>\n";
    endif;
endif;
?>

Friday, May 18, 2012

How to automatically create meta description from content in wordpress

How to automatically create meta description from content in wordpress


WordPress by default don't add a "meta description" tag to into blog. Its not necessary, but some SEO experts insists that this tag is important for our website SEO purpose. So isn't it a great idea about generating a "meta description" using the post content? In below I have describe a useful code which can do this purpose easily.
For this please paste the following code into the functions.php file under theme folder

function create_meta_desc() {
global $post;
if (!is_single()) { return; }
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 125);
echo "< meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');
The code is ready now

Tuesday, May 8, 2012

How to disable plugin updates on WordPress


How to disable plugin updates on your WordPress blog

Wordpress By default automatically checks if the plugins updates are available so after checking if its find new update available it will ask you to install it. Plugin update is useful in most cases, but when you may not want them to updates plugins, like if you modified a plugin especially for perticular website. So I describe an easy way to disable plugin updates on any WordPress blog, Please check

So simple: Just paste the following code into functions.php and save it. Thats all

remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );

Thursday, April 5, 2012

Prevent WordPress to compress your jpg images

Prevent WordPress to compress your jpg images

Here I describe how you Prevent WordPress to compress your jpg images because by default, WordPress compress your uploaded jpg images

To do this just Open functions.php file from your theme folder and paste the following code below in the functions.php file:

add_filter('jpeg_quality', function($arg){return 100;});

Now Save the file.

 

Saturday, March 10, 2012

Replace excerpt ellipsis with post permalink

This is very useful tricks to replace the ellipsis [...] from the excerpt with a permalink to the post in wordpress.

Just paste the code below into your functions.php file in theme folder of wordpress. After saved, this tip will be applied to the wordpress blog.



function replace_excerpt($content) {
       return str_replace('[...]',
               '... <div class="more-link"><a href="'. get_permalink() .'">Continue Reading</a></div>',
               $content
       );
}
add_filter('the_excerpt', 'replace_excerpt');

Tuesday, March 6, 2012

Embed a page into another page in Wordpress

Embed a page into another page in Wordpress

Just paste the following code within the loop and make sure that you update page ID on line 1!


<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile; ?>

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off