mytechead

Android & Wordpress Help center

Archive for the month “March, 2012”

WordPress: get category list (with lots of customization options)

 Description

Displays a list of Categories as links. When a Category link is clicked, all the posts in that Category will display on a Category Page using the appropriate Category Template dictated by the Template Hierarchy rules.

NOTE: wp_list_categories() works in much the same way as the two template tags replaced in WordPress 2.1, list_cats() and wp_list_cats() (both deprecated).

If you need a function that does not format the results, try get_categories()

Usage

 <?php wp_list_categories( $args ); ?> 

Default Usage

<?php $args = array(
'show_option_all'    => ,
'orderby'            => 'name',
'order'              => 'ASC',
'show_last_update'   => 0,
'style'              => 'list',
'show_count'         => 0,
'hide_empty'         => 1,
'use_desc_for_title' => 1,
'child_of'           => 0,
'feed'               => 
,
'feed_type'          => ,
'feed_image'         => 
,
'exclude'            => ,
'exclude_tree'       => 
,
'include'            => ,
'hierarchical'       => true,
'title_li'           => __( 'Categories' ),
'show_option_none'   => __('No categories'),
'number'             => NULL,
'echo'               => 1,
'depth'              => 0,
'current_category'   => 0,
'pad_counts'         => 0,
'taxonomy'           => 'category',
'walker'             => 'Walker_Category' ); ?>

By default, the usage shows:

  • No link to all categories
  • Sorts the list of Categories by the Category name in ascending order
  • Does not show the last update (last updated post in each Category)
  • Displayed in an unordered list style
  • Does not show the post count
  • Displays only Categories with posts
  • Sets the title attribute to the Category Description
  • Is not restricted to the child_of any Category
  • No feed or feed image used
  • Does not exclude any Category and includes all Categories
  • Displays the active Category with the CSS Class-Suffix ‘ current-cat’
  • Shows the Categories in hierarchical indented fashion
  • Display Category as the heading over the list
  • No SQL LIMIT is imposed (‘number’ => 0 is not shown above)
  • Displays (echos) the categories
  • No limit to depth
  • All categories.
  • The list is rendered using the Walker_Category class

Parameters

show_option_all 
(string) A non-blank value causes the display of a link to all categories if the style is set to list. The default value is not to display a link to all.
orderby 
(string) Sort categories alphabetically, by unique Category ID, or by the count of posts in that Category. The default is sort by category name. Valid values:

  • ID – Default
  • name
  • slug
  • count
  • term_group
order 
(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:

  • ASC – Default
  • DESC
show_last_updated 
(boolean) Should the last updated timestamp for posts be displayed (TRUE) or not (FALSE). Defaults to FALSE.

  • 1 (True)
  • 0 (False) – Default
style 
(string) Style to display the categories list in. A value of list displays the categories as list items while none generates no special display method (the list items are separated by <br> tags). The default setting is list (creates list items for an unordered list). See the markup sectionfor more. Valid values:

  • list – Default
  • none
show_count 
(boolean) Toggles the display of the current count of posts in each category. The default is false(do not show post counts). Valid values:

  • 1 (True)
  • 0 (False) – Default
hide_empty 
(boolean) Toggles the display of categories with no posts. The default is true(hide empty categories). Valid values:

  • 1 (True) – Default
  • 0 (False)
use_desc_for_title 
(boolean) Sets whether a category’s description is inserted into the title attribute of the links created (i.e. <a title=”<em>Category Description</em>” href=”…). The default is true(category descriptions will be inserted). Valid values:

  • 1 (True) – Default
  • 0 (False)
child_of 
(integer) Only display categories that are children of the category identified by this parameter. There is no default for this parameter.
feed 
(string) Display a link to each category’s rss-2 feed and set the link text to display. The default is no text and no feed displayed.
feed_type 
(string)
feed_image 
(string) Set a URI for an image (usually an rss feed icon) to act as a link to each categories’ rss-2 feed. This parameter overrides the feed parameter. There is no default for this parameter.
exclude 
(string) Exclude one or more categories from the results. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example. The child_of parameter is automatically set to false.
exclude_tree 
(string) Exclude category-tree from the results. This parameter added at Version 2.7.1
include 
(string) Only include the categories detailed in a comma-separated list by unique ID, in ascending order. See the example.
hierarchical 
(boolean) Display sub-categories as inner list items (below the parent list item) or inline. The default is true(display sub-categories below the parent list item). Valid values:

  • 1 (True) – Default
  • 0 (False)
title_li 
(string) Set the title and style of the outer list item. Defaults to “Categories”. If present but empty, the outer list item will not be displayed. See below for examples.
number 
(integer) Sets the number of Categories to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT.
echo 
(boolean) Show the result or keep it in a variable. The default is true (display the categories organized). This parameter added at Version 2.3Valid values:

  • 1 (True) – Default
  • 0 (False)
depth 
(integer) This parameter controls how many levels in the hierarchy of Categories are to be included in the list of Categories. The default value is 0 (display all Categories and their children). This parameter added at Version 2.5

  • 0 – All Categories and child Categories (Default).
  • -1 – All Categories displayed in flat (no indent) form (overrides hierarchical).
  • 1 – Show only top level Categories
  • n – Value of n (some number) specifies the depth (or level) to descend in displaying Categories
current_category 
(integer) Allows you to force the “current-cat” to appear on uses of wp_list_categories that are not on category archive pages. Normally, the current-cat is set only on category archive pages. If you have another use for it, or want to force it to highlight a different category, this overrides what the function thinks the “current” category is. This parameter added at Version 2.6
pad_counts 
(boolean) Calculates link or post counts by including items from child categories. If show_counts and hierarchical are true this is automatically set to true. This parameter added at Version 2.9Valid values:

  • 1 (true)
  • 0 (false) – default
taxonomy 
(string) Taxonomy to return.

This parameter added at Version 3.0 Valid values:

  • category – default
  • taxonomy – or any registered taxonomy
walker 
(object) Walker class to render the list with. Valid values:

  • object – an instance of a class that extends either Walker_Category or Walker

Examples

Include or Exclude Categories

To sort categories alphabetically and include only the categories with IDs of 16, 3, 9 and 5, you could write the following code:

<ul>
<?php wp_list_categories('orderby=name&include=3,5,9,16'); ?> 
</ul>

References : http://codex.wordpress.org/Function_Reference/wp_list_categories

WordPress: Add meta box in edit post screen.

Description

The add_meta_box() function was introduced in Version 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages.

Usage

 <?php
add_meta_box( $id, $title, $callback, $post_type,

 $context, $priority, $callback_args );
?> 

Parameters

$id
(string) (required) HTML ‘id’ attribute of the edit screen section

Default: None
$title
(string) (required) Title of the edit screen section, visible to user

Default: None
$callback
(callback) (required) Function that prints out the HTML for the edit screen section. Pass function name as a string. Within a class, you can instead pass an array to call one of the class’s methods. See the second example under Examplebelow.

Default: None
$post_type
(string) (required) The type of Write screen on which to show the edit screen section ('post', 'page', 'link', or 'custom_post_type'where custom_post_type is the custom post type slug)

Default: None
$context
(string) (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side'). (Note that ‘side’ doesn’t exist before 2.7)

Default: ‘advanced’
$priority
(string) (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low')

Default: ‘default’
$callback_args
(array) (optional) Arguments to pass into your callback function. The callback will receive the $post object and whatever parameters are passed through this variable.

Default: null

References: http://codex.wordpress.org/Function_Reference/add_meta_box

get wordpress themes directory path

While coding for my wordpress blog i came across a scenario where i had to use the directory path of my WP themes folder. It was basically required to use the php method ‘include()’ which requires directory path of the file to be included.

To obtain the directorypath you can do something like this :

$template_url = get_bloginfo("template_url");
$theme_name = explode("wp-content/themes/",$template_url);
$theme_file_path = get_theme_root() ."/". $theme_name[1];

Here the first line obtains the ‘href’ for the template or themes directory of the WP installation.

In the next line we explode the url at ”wp-content/themes/”, so the path of SELECTED /ACTIVATED theme on your blog comes in the 1st location of the array $theme_name ( $theme_name[1] ) .

get_theme_root() methods gives us the directory path till ‘wp-content/themes’ folder [without the trailing slash]

In the 3rd line we concatenate get_theme_root() with the activated theme path to obtain a full path to our themes directory [activated themes directory to be precise]

Hope it helps!!

Android Splash screen: No history when back key is pressed

Splash screens are generally used to show the loading screen of an application or it generally contains the Name of an application. Keeping the Splash screen in history stack of android and displaying it if user keeps on pressing back button until the stack reaches Splash screen is not a good practise.

To prevent Splash screen from appearing in the history stack we can use ‘android:noHistory’ tag in AndroidManifest.xml of our application.

Add something like this in your AndroidManifest.xml file of your application :

<activity android:name=”.yourActivityNameGoesHere” android:noHistory=”true” ></activity>

Enable HTML5 Localstorage in WebView

By default android WebView doesn’t allows us to use the HTML5 local storage feature. So if any of you are trying to create a WebView and want to use HTML5 localstorage feature as well then you have to explicitly enable the DOM STORAGE using the following code :

WebView mWebview = (WebView) findViewById(R.id.my_webview);
....
....
....

mWebView.getSettings().setDomStorageEnabled(true);

this ensures that HTML5 local storage works in your WebView.

Resubmit POST DATA in a webview

After working for nearly 15 hours and googling with almost all the possible combination of keywords, finally i came across a WORKING method to re-submit POST DATA contained in a webview.

Background :
I am working on an app that contains webview. This webview has a ‘refresh’, ‘go-forward’ & ‘go-backward’ buttons in the toolbar. Now the webview also has a query box that submits POST DATA to a url.

Now the issue that i was facing was when the user submits POST DATA to the url and then hits the refresh button on the results screen i was getting no data, the reason was pretty simple, the data that user has entered previously was POST DATA so there was no way that the normal WebView.reload() is going to detect that post data.

Solution:
Create a nested class inside your activity which extends WebViewClient class of android and Override a method in that class, called – onFormResubmission (WebView view, Message dontResend, Message resend)

Inside the method onFormResubmission() make a call to sendToTarget() mehtod like this :
resend.sendToTarget()

Code snippet:

private class mWebViewClient extends WebViewClient{
@Override
public void onFormResubmission (WebView view, Message dontResend, Message resend){

L.v("### FORM ","## FORM RE SUBMIT");
resend.sendToTarget();
Toast.makeText(getApplicationContext(), "RESUBMIT DATA", Toast.LENGTH_LONG).show();
}
}

Post Navigation