How to get first category name or id from wordpress 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.

if we want first category than, we will get result in php array format. Using that array we can easily get first category name or id from wordpress post.

How to get first category name or id from wordpress 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

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
global $post;
$categories = get_the_category($post->ID);

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

same like that.. cat name

 <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
 global $post;
 $categories = get_the_category($post->ID);

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

</pre>
<pre>
How to get first category name or id from post
How to get first category name or id from post

How to set different layout for each category in WordPress

In this article we given full steps and code for How to set different layout for each category in WordPress. Which you need to use in your wordpress theme.

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

In this tutorial we are going to show you, how to use conditional tags in wordpress. We given code sample for wordpress theme. 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.

how to use conditional tags in wordpress

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;&nbsp;&nbsp;&nbsp;&nbsp; // 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;&nbsp;// load details about this page

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

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

return $parentID;&nbsp;// return the ID

} else {&nbsp;// there is no parent so...

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

};

};

?>

how to use of conditional tags in wordpress

The Conditional Tags  are most useful in creating the wordpress themes and plugins. use of conditional tags in wordpress is necessary in custom themes. 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.

how to use of conditional tags in wordpress

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 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 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 create custom wordpress theme than functions file important. functions file is important in wordpress theme. Here is information about functions file.

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.

functions file is important in wordpress theme

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 a 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. In this article we shown to set Media Icons in wordpress theme.

set Media Icons in wordpress theme

  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 recent posts from category

In custom themes, we choose category to show the posts from which will be a good trick. In this article we given code for display recent posts from category.

How to display recent posts from 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>php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></h3>
<pre>
<?php the_content(); ?>
<?php endwhile; ?>

You can replace the category_name to your category name

How to remove category word from your WordPress URL

This pretty much old article and trick, shown, How to remove category word from your WordPress URL. Now these days many wordpress plugins aviable which will remove cateogry word from wordpress url.

How to remove category word from your WordPress URL

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.purabtech.in/$1 [R=301,L]

Don’t forget to replace the purabtech.in/files/ to your site name.

Have fun!

Refer this Remove category slug wordpress from URL and Remove short words from wordpress permalink URL