Tag Archives: wordpress category

How to get the first category name or id from post

Many people want to know about post category and know more information current category.

Some time we need to use the first category of wordpress post. As we know we can define the multiple category to single article. So if we want retrive the wordpress category then we will get the result in simple php array format. Using that array we can easily extract the first category of post.

In this post I will show how we can achieve that. Using following code we can get the first category ID.

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
?>

Using following code we can extract the first category name from the wordpress post.

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_name;
?>

Above code we can use only in the loop but using the following code we can extract the category
outside the loop also

<?php
global $post;
$categories = get_the_category($post->ID);

$currentcat = $category[0]->cat_ID;
?>
<pre>

same like that.. cat name

 <?php
 global $post;
 $categories = get_the_category($post->ID);

$currentcat = $category[0]->cat_name;
 ?>

<pre>

How to set different layout for each category in WordPress

Using following code you can able to set different layout for each category.

Just create the category.php file or already existed then open that file and use following code in that file.

<?php

if (is_category(10)) {

// looking for category 10 posts

include(TEMPLATEPATH . ‘/layout2.php’);

// or you can use “get_cat_id(’category_name’)” instead of category id

} else {

// put this on every other category post

include(TEMPLATEPATH . ‘/layout1.php’);

}

?>

Here we are calling the layout 2 for category 10.

Have fun!

How to use conditional tags in WordPress themes and plugins.

How to check page is subpage or parent page.

We got this particular requirement of wordpress menus. For that we need many times parent page menu and sub pages as submenu.

Using following code we can able to find the parent page and sub pages from wordpress themes. Following code we can put any where in page or code. We can put this code in header.php or index.php or footer.php or anywhere, as per our requirement.

<?php

global $post;     // if outside the loop

if ( is_page() && $post->post_parent ) {

// This is a subpage

} else {

// This is not a subpage

}

?>

If you want use or add the is_subpage() method then you need to add the following code in functions.php file.

<?php

function is_subpage() {

global $post;                                 // load details about this page

if ( is_page() && $post->post_parent ) {      // test to see if the page has a parent

$parentID = $post->post_parent;        // the ID of the parent is this

return $parentID;                      // return the ID

} else {                                      // there is no parent so…

return false;                          // …the answer to the question is false

};

};

?>

How to use conditional tags in WordPress themes and plugins

The Conditional Tags  are most useful in creating the wordpress themes and plugins. The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches.

Conditional tag will return the TRUE OR FALSE. Based on return value we can set the conditions. Some useful and most important conditional tags as follows:

is_home()

When the main blog page is being displayed.

is_front_page()

When it is the front of the site displayed, whether it is posts or a Page. Returns true when the main blog page is being displayed

is_admin()

When the Dashboard or the administration panels are being displayed.

is_single()

When any single Post page is being displayed.

is_single('17')

When Post 17 is being displayed as a single Post.

is_single('Irish Stew')

When the Post with Title “Irish Stew” is being displayed as a single Post.

is_single('beef-stew')

When the Post with Post Slug “beef-stew” is being displayed as a single Post.

is_single(array(17,'beef-stew','Irish Stew'))

Returns true when the single post being displayed is either post ID 17, or the post_name is “beef-stew”, or the post_title is “Irish Stew”.

is_page()

When any Page is being displayed.

is_page('42')

When Page 42 (ID) is being displayed.

is_page('About Me And Joe')

When the Page with a post_title of “About Me And Joe” is being displayed.

is_page('about-me')

When the Page with a post_name (slug) of “about-me” is being displayed.

is_page(array(42,'about-me','About Me And Joe'))

Returns true when the Pages displayed is either post ID 42, or post_name “about-me”, or post_title “About Me And Joe”.

How To Use Template Tags In WordPress Theme

What is meaning of a Template Tag in WordPress

A template tag is code that instructs WordPress to “do” or “get” something. In the case of the header.php template tag for your WordPress site’s name, it looks like this:

<?php bloginfo(‘name’); ?> // this will print the blog name

Following are the more public methods.

name <?php bloginfo('name'); ?>

As mentioned, this displays the name of the site and is set by the administrator in the Options > General SubPanel by default.

description <?php bloginfo('description'); ?>

This is called the “Tagline” for your blog which is usually some kind of descriptive sentence that says “My blog is about….”. It is set by the administrator in the Options > General SubPanel.

url <?php bloginfo('url'); ?>

When you want to display the URL or website address for your WordPress site, you can use URL and it will show up. This also comes from the Options > General SubPanel.

admin_email <?php bloginfo('admin_email'); ?>

If you want to display the email of the administrator, you don’t have to type it into the template files. By doing so, it may be open to email harvesters who use sophisticated software to come in and grab email addresses to use for spam. By using bloginfo('admin_email'), the email is displayed on the page for the viewers, but the actual email address is disguised from the harvesters. Nice, huh? The administrator’s email address is set in the Options > General SubPanel.

version <?php bloginfo('version'); ?>

Sometimes you’d like to show off which version of WordPress you are using. The Themes that come with WordPress by default include this information in the footer template. It simply displays the version of WordPress your blog uses.

Functions.php file is important in wordpress theme

WordPress is most powerful tool for SEO and bloggers. We all need the custom theme for our projects. When we are creating the custom wordpress theme then functions.php file is so important.

Here is the detailed information about functions.php file. Using this file we can override the wordpress functions or methods and create custom method for our theme requirement.

A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages). Suggested uses for this file:

  • Define functions used in several template files of your theme
  • Set up an admin screen, giving users options for colors, styles, and other aspects of your theme

The “Default” WordPress theme contains a functions.php file that defines functions and an admin screen, so you might want to use it as a model. Since functions.php basically functions as a plugin, the Function_Reference list is the best place to go for more information on what you can do with this file.

Basic CSS rules for development of WordPress themes

Whenever you are developing the new wordpress theme, you need to put some css in your style.css file. If you are not adding that css in your theme then there is lot of changes to break your theme view.

Some classes which are needful for image and alignment purpose. You need to just copy paste following CSS code in your style.css file.

.aligncenter,

div.aligncenter {

display: block;

margin-left: auto;

margin-right: auto;

}

.alignleft {

float: left;

}

.alignright {

float: right;

}

.wp-caption {

border: 1px solid #ddd;

text-align: center;

background-color: #f3f3f3;

padding-top: 4px;

margin: 10px;

/* optional rounded corners for browsers that support it */

-moz-border-radius: 3px;

-khtml-border-radius: 3px;

-webkit-border-radius: 3px;

border-radius: 3px;

}

.wp-caption img {

margin: 0;

padding: 0;

border: 0 none;

}

.wp-caption p.wp-caption-text {

font-size: 11px;

line-height: 17px;

padding: 0 4px 5px;

margin: 0;

}

You can edit some CSS as per theme requirement also.

Following CSS classes are also useful for creating the WordPress theme. You can create these CSS classes as per your theme requirement.

.categories {…}

.cat-item {…}

.current-cat {…}

.current-cat-parent {…}

.children {…}

.pagenav {…}

.page_item {…}

.current_page_item {…}

.current_page_parent {…}

.current_page_ancestor {…}

.widget {…}

.widget_text {…}

.blogroll {…}

.linkcat{…}

How to set Media Icons in wordpress theme

Just put some jpg, png or gif files in your theme images folder.

  1. your_theme/images/audio.jpg
  2. your_theme/images/audio.gif
  3. your_theme/images/audio.png
  4. your_theme/images/mpeg.jpg
  5. your_theme/images/mpeg.gif
  6. your_theme/images/mpeg.png
  7. your_theme/images/audio_mpeg.jpg
  8. your_theme/images/audio_mpeg.gif
  9. your_theme/images/audio_mpeg.png

Whenever you are putting the media files in post above icons will appear in your theme.

How to display most recent post from a category

Use following code for showing the recent post from category:

<?php
query_posts(‘showposts=1&cat= get_cat_id(‘category_name’)’);
while(have_posts()) : the_post();
?>
<h3><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h3>

<?php the_content(); ?>
<?php endwhile; ?>

You can replace the category_name to your category name

How to remove category word from your URL- WordPress and WordPressMU

Open your .htaccess file. This file you will find in your root directory.

Just copy and past following code in that file.

RewriteRule ^category/(.+)$ http://images.wordpressapi.com/$1 [R=301,L]

Don’t forget to replace the wordpressapi.com/files/ to your site name.

Have fun!