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.hongkiat.com/blog/30-more-most-wanted-wordpress-tips-tricks-and-hacks/
http://loneplacebo.com/wordpress-tips-and-tricks/
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
* 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.
- 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') ?>Now we edit them so the code echos the image equivalent:
< ?php the_time('M') ?>
< ?php the_time('j') ?>
< ?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.
![:D](http://www.yugatech.com/blog/wp-includes/images/smilies/icon_biggrin.gif)
Friday, November 12, 2010
Creating a Submenu in WordPress
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
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
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
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() ) { ?>if there is a user currently 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 } ?>
Wordpress user information show...
<?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
Tuesday, October 26, 2010
some wp function code
<?php the_post_thumbnail(array(327,218));?>
<?php the_content_rss('', TRUE, '', 50); ?>
Monday, October 11, 2010
Here's an example of a correctly-formatted HTML5 compliant head area:(WP)
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head> <meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title(); ?> <?php bloginfo('name'); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
<?php if ( is_singular() && get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' ); ?>
<?php wp_head(); ?>
</head>
Tuesday, August 17, 2010
Author Templates
<p>Written by: <?php the_author_posts_link(); ?></p>
<h2>List of authors:</h2> <ul> <?php wp_list_authors(); ?> </ul>
<ul> <?php wp_list_authors('exclude_admin=0'); ?> </ul>
<ul> <?php wp_list_authors('exclude_admin=0&hide_empty=0'); ?> </ul>
Thursday, July 15, 2010
Post Pic Add a login form on your WordPress Theme
Nothing hard at all. Simply paste the following code where you'd like to display your login form. (For example, on your blog sidebar, or on a page template)
<?php if (!(current_user_can('level_0'))){ ?>
<h2>Login</h2>
<form action="<?php echo get_option('home');
?>/wp-login.php" method="post">
<input type="text" name="log"
id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>"
size="20" />
<input type="password" name="pwd" id="pwd" size="20" />
<input type="submit" name="submit" value="Send" class="button" />
<p>
<label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox"
checked="checked" value="forever" /> Remember me</label>
<input type="hidden" name="redirect_to"
value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
</p>
</form>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">
Recover password</a> <?php } else { ?>
<h2>Logout</h2>
<a href="<?php echo wp_logout_url(urlencode($_SERVER['REQUEST_URI'])); ?>">
logout</a><br />
<a href="http://XXX/wp-admin/">admin</a>
<?php }?>
Once saved, your theme will display a login form. Definitely easier and quicker for login!
Post Pic Display all thumbs related to a specific page on a media page in WordPress
Simply paste the following function into your functions.php file:
function wallthumb($id=false,$beforelist='Once done, you just have to call the function:
<ul class="gallerythumb">',$afterlist='</ul>',$beforeitem='<li>',$afteritem='</li>')
{
global $wp_query;
$goquery = $wp_query->post;//contenu de la requète
if(is_attachment()){
$ptitre = $goquery->post_title;//le titre de l'image
$idparent = $goquery->post_parent;//l'id de la page parente
if(!$id){//si pas d'id en argument, on tente de la recuperer
$id = $goquery->ID;
}
if($idparent == null || $idparent == '' || $idparent == 0){
return;//si l'image est orpheline (sans page parente) on stop la fonction
}
//$twice = get_posts('post_type=attachment&post_mime_type=image&numberposts=-1&order=ASC&post_status=null&post_parent='.$idparent);//recup des infos des pièces jointes a la page parente
$twice = get_children('post_type=attachment&post_mime_type=image&order=ASC&post_parent='.$idparent);//recup des infos des pièces jointes a la page parente
$stocklienimage = array();//pour stocker les liens images
if($twice){//si pièces jointes
foreach ($twice as $value) {//boucle
$classthumbactu = '';
if($value->ID == $id){
//detection de l'image courante dans la boucle pour ajout d'une classe pour la differencier
$classthumbactu=' thumbactu';
}
$stocklienimage[$value->ID] = $beforeitem.'<a class="wallthumb'.$classthumbactu.'" href="'.get_attachment_link($value->ID).'" title="'.wp_specialchars( get_the_title($value->ID), 1 ).'" rel="attachment">'.wp_get_attachment_image( $value->ID, 'thumbnail' ).'</a>'.$afteritem;
}
}
else{
return;
}
echo $beforelist.implode('', $stocklienimage).$afterlist;//affichage de la liste
}
}
<?php wallthumb() ?>
WordPress hack : Automatically output the content in two columns
![](http://www.wprecipes.com/wp-content/uploads/2009/11/columns.jpg)
Printed magazines often display text in columns, so why blogs shouldn’t be able to do the same? Juste read on to find out how to easily and automatically display your post content in columns
This code is poweful but definitely easy to implement. Just paste it on your functions.php file and it will automatically output your post content in columns.
Your post content will be splitted on tags.
function my_multi_col($content){
$columns = explode('<h2>', $content);
$i = 0;
foreach ($columns as $column){
if (($i % 2) == 0){
$return .= '<div class="content_left">' . "\n";
if ($i > 1){
$return .= "<h2>";
} else{
$return .= '<div class="content_right">' . "\n <h2>";
}
$return .= $column;
$return .= '</p></div>';
$i++;
}
if(isset($columns[1])){
$content = wpautop($return);
}else{
$content = wpautop($content);
}
echo $content;
}
}
add_filter('the_content', 'my_multi_col');
Don't forget to add the following styles to your style.css file
.content_right, .content_left{
float:left;
width:45%;
}
.content_left{
padding-right:5%;
}
Display the number of tweets for each page or post
To apply this hack, you have to make sure that the SimpleXML PHP extension is loaded. If you're using WpWebHost or HostGator, it is.
The first step is to place the following piece of code in your functions.php file:
function tweetCount($url) {
$content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
$element = new SimpleXmlElement($content);
$tweets = $element->story->url_count;
echo $tweets." tweets!";
}
Once done, open your single.php file and call the function like this:
<?php tweetCount($post->permalink); ?>
WordPress tip: Send article to a friend by email
![](http://www.wprecipes.com/wp-content/uploads/2009/11/send-by-email.jpg)
Post Pic
WordPress tip: Send article to a friend by email
In order to create more traffic on your blog, it can be a good idea to let your readers send your posts to their friends by email. A few month ago, I already shown you a function to do that, here is an improved version for today.
To apply this recipe to your blog, simply paste the following function into your functions.php file, and that's all. Hard to do something simpler!
function direct_email($text="Send by email"){
global $post;
$title = htmlspecialchars($post->post_title);
$subject = 'Sur '.htmlspecialchars(get_bloginfo('name')).' : '.$title;
$body = 'I recommend this page : '.$title.'. You can read it on : '.get_permalink($post->ID);
$link = '<a rel="nofollow"
href="mailto:?subject='.rawurlencode($subject).'&body='.rawurlencode($body).'"
title="'.$text.' : '.$title.'">'.$text.'</a>';
return $link;
}
Customize WordPress login logo without a plugin
![](http://www.wprecipes.com/wp-content/uploads/2009/12/change-wordpress-login-logo.jpg)
WordPress login logo looks nice, but sometimes you may want to change it, for example when building a site for a client. In that case, you can use a plugin, or simply take advantage of this cool hack
Nothing hard with this recipe. The only thing you have to do is to copy the following piece of code, and paste it on your functions.php file:
function my_custom_login_logo() {
echo '<style type="text/css">
h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; }
</style>'; }
add_action('login_head', 'my_custom_login_logo');
How to remove “private” and “protected” from the post title
The only thing you have to do is to paste the following piece of code in your functions.php file. Once you'll save the file, the hack will be applied to your your posts.
function the_title_trim($title) {
$title = attribute_escape($title);
$findthese = array(
'#Protected:#',
'#Private:#' );
$replacewith = array(
'', // What to replace "Protected:" with
'' // What to replace "Private:" with );
$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
add_filter('the_title', 'the_title_trim');
Post Pic How to display an incrementing number next to each published post
function updateNumbers()
{ global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach; endif;
}
add_action ( 'publish_post', 'updateNumbers' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );
Once done, you can display the post nimber by pasting the following on
your theme file, within the loop:
<?php echo get_post_meta($post->ID,'incr_number',true); ?>
Display list of wordpress pages in two columns
<?php
$page_s = explode("</li>",wp_list_pages('title_li=&echo=0&depth=1&style=none'));
$page_n = count($page_s) - 1;
$page_col = round($page_n / 2);
for ($i=0;$i<$page_n;$i++){
if ($i<$page_col){
$page_left = $page_left.''.$page_s[$i].’</li>’;
}
elseif ($i>=$page_col){
$page_right = $page_right.”.$page_s[$i].’</li>’;
}
}
?>
<ul class=”left”>
<?php echo $page_left; ?>
</ul>
<ul class=”right”>
<?php echo $page_right; ?>
</ul>
.right {float:left; width:200px;}
.left {float:left; width:200px;}
.right {float:left; width:200px;}
.left {float:left; width:200px;}
Display your categories in two columns
<?php
$cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
$cat_n = count($cats) - 1;
$cat_col = round($cat_n / 2);
for ($i=0;$i<$cat_n;$i++){
if ($i<$cat_col){
$cat_left = $cat_left.'<li>'.$cats[$i].’</li>’;
}
elseif ($i>=$cat_col){
$cat_right = $cat_right.’<li>’.$cats[$i].’</li>’;
}
}
?>
<ul class=”left”>
<?php echo $cat_left;?>
</ul>
<ul class=”right”>
<?php echo $cat_right;?>
</ul>
.right {float:left; width:200px;}
.left {float:left; width:200px;}
WordPress and Conditional Comment CSS
<!--[if condition]>
(what to output if the condition is true) <![endif]-->
Specific Examples
<!--[if IE]>[...]<![endif]-->
If the browser is Internet Explorer (any version)
<!--[if IE 7]>[...]<![endif]-->
If the browser is Internet Explorer 7
<!--[if lt IE 7]>[...]<![endif]-->
If the browser is less than Internet Explorer 7
<!--[if lte IE 7]>[...]<![endif]-->
If the browser is less than, or equal to, Internet Explorer 7
<!--[if gte IE 6]>[...]<![endif]-->
If the browser is greater than, or equal to, Internet Explorer 6
<!--[if gt IE 6]>[...]<![endif]-->
If the browser is greater than Internet Explorer 6
<!--[if IE]>
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/ie.css"
media="screen" type="text/css" />
<![endif]-->
<!--[if lte IE 7]> <link rel="stylesheet"
href="<?php bloginfo('template_directory'); ?>/ie7.css"
media="screen" type="text/css" /><![endif]-->
<!--[if lte IE 7]>
<script src="<?php bloginfo('template_directory'); ?>/focus.js"
type="text/javascript"></script><![endif]-->
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"
type="text/css" media="screen" /><!--[if IE 7]>
<link rel="stylesheet"
href="<?php bloginfo('template_directory'); ?>/ie7.css" media="screen"
type="text/css" /><![endif]-->
http://wordpress.org/support/topic/417213
http://www.code-styling.de/english/development/wordpress-plugin-page-columnist-en
Wednesday, July 14, 2010
image is within your theme
Feed:
http://codex.wordpress.org/WordPress_Feeds
Tuesday, July 6, 2010
Dynamic Menu Highlighting
![](http://codex.wordpress.org/images/c/c7/Dynamic_menu_highlighting-00.jpg)
if you use the Pages sidebar widget (that comes with wordpress) to display your menu, it already has a CSS class current_page_item, which you can use to achieve the same effect. You can access it like this in your CSS:
.widget_pages li.current_page_item a{
background-image:url(images/activelink.gif);
}<ul id="menu">
<!-- To show "current" on the home page -->
<li<?php if (is_home())
{
echo " id=\"current\"";
}?>>
<a href="<?php bloginfo('url') ?>">Home</a>
</li>
<!-- To show "current" on the Archive Page (a listing of all months and categories),
individual posts, but NOT individual posts in category 10 -->
<li<?php if (is_page('Archive') || is_single() && !in_category('10'))
{
echo " id=\"current\""; }?>>
<a href="<?php bloginfo('url') ?>/archive">Archive</a> </li>
<!-- To show "current" on any posts in category 10, called Design -->
<li<?php if (is_category('Design') || in_category('10') && !is_single())
{ echo " id=\"current\""; }?>>
<a href="<?php bloginfo('url') ?>/category/design">Design</a> </li>
<!-- To show "current" on the About Page -->
<li<?php if (is_page('About'))
{
echo " id=\"current\""; }?>>
<a href="<?php bloginfo('url') ?>/about">About</a>
</li> </ul>
#current
{
background-color: #336699;
}
Friday, July 2, 2010
Applying different formatting to just the first post on the first page
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $c++; if( $c == 1) :?>
<h1>The first post on the main index page</h1>
<?php the_title(); ?> <?php the_excerpt(); ?>
<?php else :?> <h2><?php the_title(); ?></h2>
<?php the_content(); ?> <?php endif;?>
<?php endwhile; ?> <!-- page nav -->
More -->
http://wordpress.org/support/topic/302408?replies=6
show me more than the post really in the current category
if ( is_single() ) {
$cats = wp_get_post_categories($post->ID);
if ($cats) { $first_cat = $cats[0];
$args=array( 'cat' => $first_cat, //cat__not_in wouldn't work
'post__not_in' => array($post->ID), 'showposts'=>5,
'caller_get_posts'=>1 ); $my_query = new WP_Query($args);
if( $my_query->have_posts() ) { echo 'Related Posts';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></p>
<?php endwhile; } //if ($my_query) } //if ($cats)
wp_reset_query(); // Restore global post data stomped by the_post().
} //if (is_single())
?>
More--->
http://wordpress.org/support/topic/310930
Tuesday, June 29, 2010
display display something certain page
<div class="sidebars sidebarright">
<div class="sidebar2">
<?php if (is_page('contact-us')) { ?>
<?php echo do_shortcode('[singlepic id=97]'); ?>
<?php } else { ?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Wide Sidebar') ) : ?>
<div class="block">
<h3>This is the wide sidebar</h3>
<p>This is a great spot to put some information or a promotion. Use a "Text" widget in your
"Wide Sidebar" sibebar options in Wordpress. Whatever you put in the title will show above
and whatever you put as text (or html) will show right here!</p>
<p>There are a total of 7 widget areas: Wide, Left and Right sidebars, along with footer widgets
Footer1, Footer2, Footer3 and Footer4..</p>
</div>
<?php endif; ?>
<?php } ?>
</div>
Highlighting in main Nav using is_page
<ul id="nav" role="navigation"> <li<?php if ( is_page('home') )
{ echo ' class="current"'; } ?>><a href="/">Home</a></li>
<li<?php if ( is_page('about') ) { echo ' class="current"'; } ?>><a href="/about/">About</a></li>
<li<?php if ( is_page('work') ) { echo ' class="current"'; } ?>><a href="/work/">Work</a></li>
<li<?php if ( is_page('blog') ) { echo ' class="current"'; } ?>><a href="/blog/">Blog</a></li>
<li<?php if ( is_page('contact') ) { echo ' class="current"'; } ?>><a href="/contact/">Contact</a></li>
</ul><?php if (is_category('News')) : else :
if (is_page('Cakes') || is_category('')) {
echo 'id="selected"'
}
endif; ?>
Thursday, May 13, 2010
Add page
<?php wp_list_pages('exclude=3,8,12,18,19,20,21,22,23&title_li=' ); ?>
Wednesday, May 12, 2010
show particular page in home page
<?php // retrieve one post with an ID of 5 query_posts('p=5');
global $more; // set $more to 0 in order to only get the first part of the post $more = 0;
// the Loop while (have_posts()) : the_post();
// the content of the post the_content('Read the full post »');
endwhile; ?>
Monday, May 3, 2010
How to show post from a particular category widget
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a><br /><?php the_content(); ?><?php endwhile; ?>
===================================
<?php $args=array( 'cat' => 1, 'posts_per_page'=>5, 'caller_get_posts'=>1 );
$my_query = new WP_Query($args); if( $my_query->have_posts() ) {
echo '5 recent Posts from category 1'; while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></p>
<?php endwhile; } //if ($my_query) wp_reset_query();
// Restore global post data stomped by the_post(). ?>
Create a Widget Function Wordpress
Create a Widget Function
Paste this code to the theme
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('photos') ) : else : ?>
-------------------------------
<?php endif; ?>
Add the to the functions.php
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'photos',
'before_widget' => '<div id="%1$s" class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="sidebartitle">',
'after_title' => '</h2>',
));
?>
Get 5 new posts for my homepage Wordpress
<div class="recent-five-posts">
<?php query_posts("orderby=desc&showposts=5"); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="individual-posts">
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read More</a></h3>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
Wednesday, April 28, 2010
Create a plugin in WordPress
Plugin Name: Just Published!
Plugin URI: http://forumone.com/
Description: Notify a user once a post has been published.
Author: Matt Gibbs
Version: 1.0.0
Author URI: http://forumone.com/
*/
function jp_email_user() {
mail('some@email.com', 'A new post has been published!', 'Some content');
}
add_action('publish_post', 'jp_email_user');
?>