mytechead

Android & Wordpress Help center

Archive for the month “May, 2012”

WordPress : get customised widget area

At times we want our own custom widget area, which we can customise according to our needs (rather than relying on the stock WP widget areas).

This tutorial helps you to create your own custom widget area.

Here is how it is done :

1. Register The Widget

To register the new widget you need to open the functions.php file in your WordPress themes editor.

Add the following code to the functions file, making sure you place it in between php tags:

if ( function_exists(‘register_sidebar’) ){
register_sidebar(array(
‘name’ => ‘my_mega_menu’,
‘before_widget’ => ‘<div id=”my-mega-menu-widget”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ”,
‘after_title’ => ”,
));
}

Give the widget area a name – e.g. my_mega_menu and in the before/after options you can place code which you wish to appear wrapped around either the widget itself or the title.

Since we are wanting to use one of the menu widget themes, which come with plugins we dont want to wrap it in the standard widget tags, which may interfere with the CSS.

For this example we have just wrapped it in a div tag with the id – my-mega-menu-widget.

2. Add The Widget Code To Your Theme

Now we can add the code that will call the widget to the correct location in our theme files. Since we are creating a horizontal mega menu we obviously want to display this in the header.

In your WordPress themes editor open the header.php file.

Find the location where you want to place the menu and add the following code to the header.php file:

<?php /* Widgetized sidebar */
    if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('my_mega_menu') ) : ?><?php endif; ?>

Make sure that you change the “my_mega_menu” text to be the same as the name you gave to your widget area in the functions file.

Save the changes to the header.php file

If you now go to the widget area in WordPress admin you should see a new widget area in the right hand column with the name of your widget.

You can now add your mega menu, accordion menu or any other widget to this area.

Advertisements

How to get proper HTML formatting with get_post method.

I was coding a custom excerpt function in functions.php and for that i had to use the get_post() method which fetches every details associated with a particular post. I used it to get content of the post id being passed as a param to my custom post excerpt function.

I noticed a very subtle behaviour of get_post() method while using the object that get_post() method returned.  When i used the object to get the post_content, the html formatting that it was providing me was not similar to the one i was getting (for the same post) when i used the_content() method. I was really astonished as to why this was happening !!!

Googling a bit i found that it is some editor filters that the two methods follow to throw the html, which was causing a mismatch in the HTML (post_content) that they were throwing.

 

Now to get around this situation & to get the same html post_content as the one provided by the_content() method you can do something like this.

In your code instead of doing this :

// ID of your post 

$post_id = 100;

// Get every information about the post

$post_details = get_post($post_id);

// Get the post_content to use it somewhere 

$post_content = $post_details->post_content;

Change the last line where you are fetching the post_content to something like this :

// Get the post_content to use it somewhere 

$post_content = apply_filters(‘the_content’, $post_details->post_content);

Applying the_content() filter will help you get post_content’s HTML formatted the way you would have got when using the_content() method.

 

Hope it helps some one!


 

How to show all the posts of a category.

Hi Folks!!!

Its been quite some time since i have posted on this blog.

Was working on wordpress and just came across a scenario where i had to show all the posts belonging to a particular category. 

So here it goes..

Background ::

WordPress uses category.php as a template file  to show category pages. category.php can be modified to show all the posts belonging to that particular category. Now the catch here is that THE WHILE loop used to show all the posts belonging to that category limits the number of posts for a single page to be 10 (by default) & if the category has more than 10 pages, then it adds pagination links at the top and bottom of your page.

How to get rid of this :: 

Now if you wan to show all the posts for that category on the single page itself, without any pagination links just do this :

Before the while loop (the one used to loop through all the posts), somehting like this <?php while (have_posts()) : the_post(); ?> , add the following lines –

<?php

global $query_string;
query_posts( $query_string . ‘&posts_per_page=-1’ );

?>

and after the while loop ends, add –

<?php

wp_reset_query();

?>

What we are doing here is, we are modifying the $query_string  which the WP was using and changing its $post_per_page param to  “-1” (-1 for all the posts), additionally we can specify the value of $post_per_page to whatever number of posts we want & add support for pagination.

To know more about WP pagination stay tuned for my next post.

Disable HOME KEY in android

In order to disable the home button or the home key on your android device you can use the following piece of code.

 

@Override
public void onAttachedToWindow()
{

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
 return true;
}
return super.onKeyDown(keyCode, event);
}

 

This disables the home button.

 

Hope it helps some one!!

Post Navigation