Showing posts with label ID. Show all posts
Showing posts with label ID. Show all posts

Nov 14, 2011

Wordpress Multiple Category Search

Since when I started wordpress, I had a question in my mind, why wordpress doesn’t give multiple search option? I googled a lot, but couldn’t find a plugin or code which exactly works.

So finally decided to go more into deep of wordpress and php, and came up with a code which works.

Functioning of this code:  Searches all posts using a custom search box where 2 drop-down box will be displayed with values of categories in it.

Have you ever played with search URL? Or tags URL or Categories URL?

Very few might have knowledge that wordpress supports multiple tags or multiple categories. Don’t believe me, in your URL, type this

http://yourblog.com/tag/tag1+tag2

Or

http://yourblog.com/tag/tag1,tag2

Now what does this means? Simple when you type tag1+tag2 it will show you posts with having both the tags. And when you type tag1, tag2 it will show you all the posts having either tag in it.

Same works for categories.

http://yourblog.com/category/cat1+cat2
http://yourblog.com/category/cat1,cat2

Now I tried this same functionality in search URL of Wordpress.

http://yourblog.com/?s=html&cat=114

And woila it worked!!!!

Here “s” is your search term and “cat=114” is your category ID. This will work and display all the posts with the search term only in category ID 114.

But the only problem in this is that it will just take one category at a time.

Wordpress doesn’t supports this URL

http://yourblog.com/?s=html&cat=114+115

Here comes a big problem. After a lot of search I found that “+” is considered as a space in URL. So we need to write a function which will make this space act as a + operator.



[php]
add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {
if ( ! isset( $query->query_vars[ 'cat' ] ) )
return $query;
// split cat query on a space to get IDs separated by '+' in URL
$cats = explode( ' ', $query->query_vars[ 'cat' ] );
if ( count( $cats ) > 1 ) {
unset( $query->query_vars[ 'cat' ] );
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}

function check()
{
$url = $_SERVER["REQUEST_URI"];
$query = str_replace('&cats=', '+', $url);
if(isset($query) && ($query != $url))
{
header('Location:'.$query);
}
}

add_action('parse_query', 'check');
[/php]

After getting this function in our functions.php file, the multiple categories URL with search will work.

So it’s time to design a simple search form.

Paste this anywhere in your theme.



[html]
<!-- Search Form -->
<div style="margin:10px 0 10px 0;">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<table cellspacing="5">
<tr>
<td>
<label for="s" style="margin:10px;">Search</label>
</td>

<td>
<input type="text" class="field" name="s" style="width: 100px;"/>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1&name=cats'); ?>
</td>
<td>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" style="display: block; position: relative; top: 7px;"/>
</td>
</tr>
</table>
</form>
</div>
<!-- Search Form -->


[/html]

That’s it, now just go and try your wordpress site will be having a multiple category search option. No plugin and no hundred lines of code, and best part, it’s fully flexible to use. If you want one more category drop down then just copy paste the category php code one more time.




Sep 2, 2011

Wordpress Show Excerpt Of Child Page | How To Show Excerpt Of Child Pages On Parent Page Of Wordpress

Today in this tutorial I am going to share a wordpress hack, which will show your child pages excerpts with title on the parent page.

OK, it was confusing, let me explain you.
Suppose you have  5 pages, which has a single parent page. If you want to show all the 5 sub pages on the Parent page, then you have to use this code.

Open your page.php file and paste this code after the loop.

[php]
<?php
$child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div style="border:1px solid #e7e7e7;margin: 10px;padding: 10px;">
<h2><a href="<?php echo  get_permalink($pageChild->ID); ?>" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
?>
<?php the_excerpt();?>
<p style="text-align:right;"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>">Read More...</a></p>
</div>
<?php endforeach;
endif;
?>
[/php]

Jul 14, 2011

Wordpress Make A Featured Post Jquery Ticker | Wordpress Make featured Posts

Now a days blogging is very common, every human being wants to have their own blog or website where they can put some useful stuffs, but out of that blogs, there are some important or hot blogs, which we say as featured blogs, if you are wordpress user then why to adopt plugin, where you can create a simple one.
So lets get started.

First decide where you want to display the featured post section, usually it is on the home page. So considering it on home page, we will edit two files in this tutorial.
index.php
header.php

So lets get started with header.php, open that file, and in the head section put this small piece of code

[css]
<style>
#posts-container{ }
#posts-container ul li div{
border: 1px solid #aaaaaa;
background: #ffffff;}
</style>
[/css]

You can see that the style is not fully complete, cause I have left it upto you, so that you can have your own style, but if you leave this also it doesn't matter, cause this will also give the blog a simple and neat looks to featured posts

After styling we will add some javascript code to head section.
Be very careful while adding javascript code, as it may conflict with other js files.

[javascript]
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.vticker-min.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('#posts-container').vTicker({
speed: 600,
pause: 3000,
animation: 'fade',
mousePause: true,
showItems: 3
});
});
</script>
[/javascript]

As you can see the $ is been replaced by jQuery just to avoid the conflict.
So the final header code will look something like this:

[html]
<style type="text/css" media="all">
#posts-container{ }
#posts-container ul li div{
border: 1px solid #aaaaaa;
background: #ffffff;}
</style>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.vticker-min.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('#posts-container').vTicker({
speed: 600,
pause: 3000,
animation: 'fade',
mousePause: true,
showItems: 3
});
});
</script>
[/html]

In the above javascript, you can change the attribut as per your need, like speed, animation, on-mouse-over-pause etc etc. You can change it anytime. I have kept the js file into the js folder of the theme, if you are keeping it in any other folder then be sure to edit the code.

This part completes the main jquery.
But the question is how will you select the posts as featured?
For this we can use custom fields.
Add a new custom field in your Add-new or Edit posts page, and replicate the same as in below image
How TO Add Custom Field

Tip: Instead Of Manually Adding one-one custom field, I recommend that you use More Fields Plugin. Which Will help you to have a custom field for all post.

Do the above step for only those posts which you want to display as featured posts.

Now open index.php file and just paste this code just above the if ( have_posts() ) code

[php]
<?php
$key = 'featured';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta == 'no') {
echo 'No Featured Posts';
}
elseif($themeta == 'yes'){
?>
<?php query_posts('meta_key=featured&meta_value=Yes');  ?>
<div style="border:4px solid #666666; -moz-border-radius:5px; -webkit-border-radius:5px; padding:12px; color:#333333;font-weight:bold; background-color:#FFFFE0;">
<h1 style="color:#880000; font-weight:bold; font-size:18px; text-decoration:underline;">Featured Posts:</h1>
<div id="posts-container">
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" style="color:#333333;"><?php echo the_title() ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>

<?php wp_reset_query();

}?>
[/php]

In this code we are taking the posts which has custom field value set to yes for featured, and displaying it in a list format, here I have given the css into the style tag of a div, you can change it any time as per your theme design.  I am also using wp_reset_query just to be sure that it doesn't conflicts with other posts.

This above tutorial is just going to display posts title, but if you also want to display posts content, then just make a small change to the above code, just add the 3 line to the code.

[php]
<li>
<a href="<?php the_permalink() ?>" style="color:#333333;"><?php echo the_title() ?></a>
<br><em><php echo the_excerpt(); ?></em>
</li>
[/php]

You can see the demo on my websites homepage
Demo | Download [download id="3"]

Jul 10, 2011

Add links to WordPress admin bar

So if you want custom links on your admin bar, then just copy the following code to your function.php file.
You can modify the code as per your need.

[php]
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'new-content', // use 'false' for a root menu, or pass the ID of the parent menu
'id' => 'new_media', // link ID, defaults to a sanitized title value
'title' => __('Media'), // link title
'href' => admin_url( 'media-new.php'), // name of file
'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
));
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );[/php]

Wordpress Disable Theme Changing

So you want that your client should not change the theme. OK, just copy the following snippet to your functions.php file.

[php]
add_action('admin_init', 'slt_lock_theme');
function slt_lock_theme() {
global $submenu, $userdata;
get_currentuserinfo();
if ($userdata-&gt;ID != 1) {
unset($submenu['themes.php'][5]);
unset($submenu['themes.php'][15]);
}
}[/php]

Once saved, your client will not be able to switch or change themes anymore.

Jun 28, 2011

Wordpress-Add custom taxonomies tags to permalinks

Hello everyone, today we are going to discuss with you how to add wordpress custom taxonomies to permalinks.
You can refer custom taxonomies or taxonomies from here

Ok first of all, you need to register custom taxonomy with setting "rewrite=true", doing this will enable %custom_taxonomy_tag%
If you want to add custom taxonomies without coding, i prefer this plugin GD CPT tools

After doing all these things, you need to copy paste this function, to your theme's function.php file
[php]add_filter('post_link', 'taxo_permalink', 10, 3);
add_filter('post_type_link', 'taxo_permalink', 10, 3);

function taxo_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxo%') === FALSE) return $permalink;

//To Get post
$post = get_post($post_id);
if (!$post) return $permalink;

//To Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'taxo');
if (!is_wp_error($terms) &amp;&amp; !empty($terms) &amp;&amp; is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-taxo';

return str_replace('%taxo%', $taxonomy_slug, $permalink);
}[/php]
Explanation
Line 5 – If the permalink does not contain the %taxo% tag, then we don’t need to translate anything.
Line 12 – Get the taxo terms related to the current post object.
Line 13 – Retrieve the slug value of the first taxo custom taxonomy object linked to the current post.
Line 14 – If no taxo terms are retrieved, then replace our taxo tag with the value no-taxo.
Line 16 – Replace the %taxo% tag with our custom taxonomy slug.

Now in wordpress, dashboard-settings-permalinks, you can write
[php]%postname%/%taxo%[/php]
This way you can add more custom taxonomies, just by adding more functions.
So Simple!!!!!!!