Tuesday, November 23, 2010

Sub-page with Title With contain....



<?php

$mypages = get_pages('child_of=4');

$count = 0;

foreach($mypages as $page)

{

$content = $page->post_content;



?>

<div class="service-list">

<h3><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h3>

<p><?php echo trunck_string($page->post_content,43,true); ?></p>

</div>

<?php

}

?>

Saturday, November 13, 2010

Submit WordPress Posts From The Frontend

/**
 *
 * New Post Form for Custom Post Types for the Frontend of Your Site
 * By Jared Williams - http://new2wp.com
 *
 * Last Updated: 8/30/2010
 */
 
// Check if the form was submitted
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
 
 // Do some minor form validation to make sure there is content
 if (isset ($_POST['title'])) { 
  $title =  $_POST['title']; 
 } else { 
  echo 'Please enter a title';
 }
 if (isset ($_POST['description'])) { 
  $description = $_POST['description']; 
 } else { 
  echo 'Please enter the content'; 
 }
 $tags = $_POST['post_tags'];

 // Add the content of the form to $post as an array
 $post = array(
  'post_title' => $title,
  'post_content' => $description,
  'post_category' => $_POST['cat'],  // Usable for custom taxonomies too
  'tags_input' => $tags, 
  'post_status' => 'publish',   // Choose: publish, preview, future, etc.
  'post_type'  => $_POST['post_type']  // Use a custom post type if you want to
 );
 wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
       // http://codex.wordpress.org/Function_Reference/wp_insert_post
 wp_redirect( home_url() ); // redirect to home page after submit

} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post'); 
?>

<!-- New Post Form -->  
  <div id="postbox"> 
   <form id="new_post" name="new_post" method="post" action="">  
  <p><label for="title">Title</label><br /> 
   <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
    </p> 
   <p><label for="description">Description</label><br /> 
   <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea> 
   </p> 
   <p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p> 
   <p><label for="post_tags">Tags</label> 
   <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p> 
   <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p> 
   <input type="hidden" name="post_type" id="post_type" value="post" /> 
   <input type="hidden" name="action" value="post" /> 
   <?php wp_nonce_field( 'new-post' ); ?> 
   </form>
    </div> 
   <!--// New Post Form -->
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

Most Wanted WordPress Tips, Tricks and Hacks

http://www.wpbeginner.com/wp-tutorials/55-most-wanted-wordpress-tips-tricks-and-hacks/
http://www.hongkiat.com/blog/30-more-most-wanted-wordpress-tips-tricks-and-hacks/
http://loneplacebo.com/wordpress-tips-and-tricks/

TDO Mini Forms

http://wordpress.org/extend/plugins/tdo-mini-forms/

This plugin can add themed custom posting and editing forms to your website that allows your readers (including non-registered) to contribute.

WP Date Image Hack

I am posting the simple hack I made with using dynamic images to replace the date entries of my blog. Just follow the short tutorial here to implement the same with your WordPress blog:

* Create your own date image template in Photoshop. Make sure that you layout them so that the month, day and year are all distinct from each other and not overlapping. See my example here :
* Using the ruler guide in Photoshop, slice the template into three parts.
* You will need to determine the date format you use in your blog. Mine uses day of week, day and month. Now create individual images using the sliced template and name them accordingly. For day of week, the naming convention is mon.gif, tue.gif, … sun.gif; for the day — 1.gif,… 30.gif; for the month — jan.gif, feb.gif,… dec.gif.




    (Note: If you use full date names, you will need to name your GIF files similarly (i.e. Friday, February 22, 2005 will use “friday.gif”, “february.gif”, “22.gif” and “2005.gif”).
  • Now, we’re ready to hack the WP date function to replace text dates with images. Go to your WP theme folder and look for the post.php file and open it for editing. Note: depending on the WP theme you use, the file could also be index.php.
  • Look for the date function as such:
    < ?php the_time('D, M j') ?>
    You will need to break that into 3 parts like this:
    < ?php the_time('D') ?>

    < ?php the_time('M') ?>

    < ?php the_time('j') ?>
    Now we edit them so the code echos the image equivalent:
    < ?php $d = strtolower(get_the_time('D')); echo ("< img src= 'wp-images/{$d}.gif' > “); ?>

    < ?php $m = strtolower(get_the_time('M')); echo ("< img src= 'wp-images/{$m}.gif' > “); ?>

    < ?php $j = strtolower(get_the_time('j')); echo ("< img src = 'wp-images/{$j}.gif' > “); ?>
  • You will need to check on the date format parameter string if you have a different date format.
I hope that was fairly simple to understand. :D I’m not sure if this hack can be made into a plugin though.

Friday, November 12, 2010

Creating a Submenu in WordPress

For a project I’m currently working on I wanted to create a submenu that included the parent page as well as the supbages. I just wanted to display the submenu only if the parent page had subpages. Searching the WordPress Codex and googling for a solution I couldn’t quite find an example that took all these factors into consideration so I had to figure it out myself. Here’s the approach I came up with.

Checking if the page has subpages

I searched in vain for a method to determine if the current page has any subpages or not. I first assumed that there would be a has_subpages() method, but so far I haven’t found any. Lacking that, I came up with a very crude way of checking it. I explicitly had to try to fetch all the subpages with the function wp_list_pages() and then check if it returned anything. It’s not pretty but it works.

$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
// This page has subpages
}

Checking if it’s a parent page or a subpage

The next thing I had to figure out was how to check i the current page is a parent page or a subpage. That’s done with the following code.

if(is_page() && $post->post_parent) {
// This is a subpage
} else {
// This a parent page
}

Fetching the submenu

Now I needed a way to fetch the subpages. This is done with the wp_list_pages() function. The tricky part about this is that there’s no way to get both the parent page and the subpages in the same call. So therefor we have to call the function twice. The call also looks a little different depending on if we’re on the parent page or on the subpage.

if(is_page() && $post->post_parent) {
// This is a subpage
$children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
// This is a parent page that have subpages
$children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}

There are other ways of doing this, but the benefit of this approach is that we automatically get class="current_page_item" on the list-item that represents the page that we’re currently on. That’s handy if you want to style that item in any particular way.

Outputting the HTML

The last step is to output the actual HTML. I’ve chosen to output it as an unordered list.

<?php // Check to see if we have anything to output ?>

<?php if ($children) { ?>

<ul class="submenu">

<?php echo $children; ?>

</ul>

<?php } ?>




Putting it all together

Now it’s time to put all the pieces together. Just put this code in your page template one of the pages in your template, like for example page.php or sidebar.php and you’re good to go. These pages are located in /wp-content/themes/your-theme/.

<?php

$has_subpages = false;

// Check to see if the current page has any subpages

$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');

if($children) {

$has_subpages = true;

}

// Reseting $children

$children = "";


// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)

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

// This is a subpage

$children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");

$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");

} else if($has_subpages) {

// This is a parent page that have subpages

$children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");

$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");

}

?>

<?php // Check to see if we have anything to output ?>

<?php if ($children) { ?>

<ul class="submenu">

<?php echo $children; ?>

</ul>

<?php } ?>



I think that WordPress is an absolutely awesome CMS/Blog engine, but it do lack some handy methods. Fortunately it’s almost always possible to create workarounds. I hope that you will find this useful in your own WordPress Template. Don’t hesitate to tell me if you have a smarter way of doing this.

Wednesday, November 10, 2010

Wordpress Template Tags

http://codex.wordpress.org/Template_Tags

Custom Post Thumbnails

Along with the new post_thumbnail feature in WordPress 2.9., came the option to register a new post_thumbnail size



register a new post_thumbnail size.


add_image_size( $name, $width = 0, $height = 0, $crop = FALSE)

An Example


functions.php


if ( function_exists( 'add_image_size' ) ) add_theme_support( 'post-thumbnails' );

if ( function_exists( 'add_image_size' ) ) {

add_image_size( 'cat-thumb', 200, 200 );

add_image_size( 'search-thumb', 220, 180, true );

}

Using the New Image Sizes

<?php if ( has_post_thumbnail() ) the_post_thumbnail('cat-thumb'); ?>

add_image_size() is defined in wp-includes/media.php


Changing Headers with WordPress body_class()

WordPress 2.8 introduced a new function — body_class() — that attaches a list of classes to the element according to what type of page is being displayed. These classes can be used — in conjunction with your theme’s stylesheet — to display different headers on different page types.

Let’s assume your header markup looks something like:

<body <?php body_class(); ?>>
<div id="header">
<div id="headerimg">
<h1>
<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a>
</h1>
<div class="description"><?php bloginfo('description'); ?>
</div>
</div>
</div>

And your current CSS for the header looks like:

#header {background: #73a0c5 url(images/header.jpg) no-repeat left top;}

<body <?php if (function_exists('body_class')) body_class(); ?>>


#header { 
background-color:#73a0c5;
ackground-image:url(images/header.jpg;
background_repeat:no-repeat;
background-position:left top;
}

body.category #header url{background-image:url(images/header2.jpg;}

Tuesday, November 9, 2010

Display wordpress category name without link

When you’re using the the_category( ) function to display the category name of a post, a link to the category archive is automatically created by WordPress.
While this is a good thing in most cases, what if you don’t want to create a link? Here’s an easy way to do it.
To display the category name without haveing a link to the category archive being automatically created, simply open replace the_category( ) by the following code:


<?php

$category = get_the_category();

echo $category[0]->cat_name;

?>

<?php 
   $category = $wp_query->get_queried_object();
  $cat_name = $category->name;
 ?>

Saturday, November 6, 2010

How to change image with onclick?

To change the image using OnClick function i have found 2 ways.

1. Using jQuery.
2. Normal JavaScript.

Lets look one by one.


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#ChangeImage").click(function(){
$('#ChangeImage').attr('src','yahoo.gif');
});
});
</script>
<img src="bm_yahoo.gif" name="my_pic" id="ChangeImage">

====

<script type="text/javascript">
function changePic()
{
document.getElementById("Img1").src="bm_yahoo.gif";
}
</script>
<a href="#" onclick="changePic()";>
<img border="0" id="Img1" src="yahoo.gif" /></a>
</script>

Friday, November 5, 2010

wp_users table in the database

<?php get_userdata(userid); ?>

The call to get_userdata() returns the user's data, where it can be retrieved using member variables.

<?php $user_info = get_userdata(1);
echo('Username: ' . $user_info->user_login . "\n");
echo('User level: ' . $user_info->user_level . "\n");
echo('User ID: ' . $user_info->ID . "\n");
?>


Output:
Username: admin
User level: 10
User ID: 1
http://codex.wordpress.org/Function_Reference/get_userdata

if there is a user currently logged in

<?php if ( is_user_logged_in() ) { ?>
<!-- text that logged in users will see -->
<?php } else { ?>
<!-- here is a paragraph that is shown to anyone not logged in -->
<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>,
you can save your favorite posts for future reference.</p>
<?php } ?>
if there is a user currently logged in

Wordpress user information show...

The call to get_currentuserinfo() places the current user's info into $current_user, where it can be retrieved using member variables.

<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
?>

Output
Username: Zedd

User email: my@email.com
User first name: John
User last name: Doe
User display name: John Doe

User ID: 1