WordPress Code Snippets

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

WordPress Code Snippets

WordPress Code Snippets

WordPress Code Snippets

Hello Friends..!! Here, I wrote the article about some wordpress code snippets which will help you to customize wordpress theme and features. There are many customization requirement from client which I have to do. So I have list out some of code snippets which will you for your client requirement. WordPress is easy to customize the code as per requirement and it is flexible so I love wordpress.

For not Login User

1
2
3
4
5
6
7
	<?php get_currentuserinfo();
		global $user_ID;
		if ($user_ID == '')
		{
			header('Location: wp-login.php');
		}
	?>

Restrict to access authors only

1
2
3
4
5
6
7
<?php
/* Template Name: Restricted to Authors only */
 
if ( !current_user_can('author')) {
	wp_die('You don’t have access to this page.')
}
?>

For Permalink

1
<?php echo get_permalink(11); ?>

Home Page URL

1
<?php echo bloginfo("url");?>

Template URL (theme URL)

1
<?php echo bloginfo("template_url");?>

To Show Menu

1
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

Get Widget

1
<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>

For Is Template Page

1
<?php if ( is_page_template('media-template.php') ) { ?>

For Home page Feautured Page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<? php  
if ( is_page(5) ) : ?>
    <div id="header">
<?php  else :?>
    <div id="innerpage-header">
<?php endif; ?>
----------------------------------
<?php if (is_front_page()) : ?>   
  <div id="header">
 <?php  else :?>
<div id="innerpage-header">
<?php endif; ?>
 
-----------------------------------
<?php
if ( is_home() ) : ?>
    <div id="header">
<?php  else :?>
    <div id="innerpage-header">
<?php endif; ?>
 
--------------
<?php the_content(); ?>

To show Subcategory page of Parent Page in Sidebar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
if($post->post_parent) {
	$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0$sort_column=menu_order");
	$title_heading = get_the_title($post->post_parent);
}
else {
	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
	$title_heading = get_the_title($post->ID);
}
if ($children) { 
?>
 
	<h2> <? echo $title_heading ?> </h2>
	<ul>
		<?php echo $children; ?>
	</ul>
<?php } ?>

To show all pages with Subcategory pages of Parent Page in Sidebar

1
2
3
4
5
6
7
8
9
10
<?php 
	$Pages = wp_list_pages('title_li=&echo=0&depth=1');
	$InnerPages = wp_list_pages('child_of='.($post->post_parent != false ? $post -> post_parent : $post->ID).'&title_li=&echo=0');
	$Title  = ($post->post_parent != false) ? trim(get_the_title($post->post_parent)) : trim(wp_title('', false));
	if($Title != '')
		$Pages = str_replace($Title.'</a></li>', $Title.'</a>'.'<ul>'.$InnerPages.'</ul></li>',$Pages);
 
	echo $Pages;
	unset($Pages, $InnerPages);
?>

To Show Excerpt and Title of Childpages (Subpages)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---------------------------------------------------------------------
For Custom Field Plugins
 
<?php  if( get_field('page_title') ):	?>
 <?php the_field('page_title'); ?>
<?php endif;?>
----------------------------------------------------------------------
 
<?php
   $pages = get_pages('child_of='.$post->ID.'&sort_column=post_title');
   $count = 0;
   foreach($pages as $page)  { ?>
   <h4><a href="<?php echo get_page_link($page->ID) ?>">
	<?php echo $page->post_title ?></a>
   </h4>
<?php 	} ?>
--------------------------------------------------------------------------
 
<?php query_posts("page_id=22"); if(have_posts()) : the_post(); ?>
<?php the_excerpt();  ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
--------------------------------------------------------------------------

Add Page Excerpt Code in function . php

1
2
3
4
add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
     add_post_type_support( 'page', 'excerpt' );
}

To Add Custom Post Type like Portfolio paste code in WordPress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
add_action('init', 'portfolio_register');
 
function portfolio_register() {
 
	$labels = array(
		'name' => _x('My Portfolio', 'post type general name'),
		'singular_name' => _x('Portfolio Item', 'post type singular name'),
		'add_new' => _x('Add New', 'portfolio item'),
		'add_new_item' => __('Add New Portfolio Item'),
		'edit_item' => __('Edit Portfolio Item'),
		'new_item' => __('New Portfolio Item'),
		'view_item' => __('View Portfolio Item'),
		'search_items' => __('Search Portfolio'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		//'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title','editor','thumbnail')
	  ); 
 
	register_post_type( 'portfolio' , $args );
}

Code to Change Read More text for excerpt in function . php

1
2
3
4
5
6
function twentyten_continue_reading_link() {
//	return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
 
    return '<br /><br /> <a href="'. get_permalink() . '" class="readmore_btn">' . __( '<img src="' . get_bloginfo('template_directory'). '/images/readmore_btn.png" style="float:right"/>', 'twentyten' ) . '</a>';
 
}

Get Featured Images

1
2
3
4
5
<?php if (has_post_thumbnail( $post->ID ) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src='<?php echo $image[0]; ?>')/>
</div>
<?php endif; ?>

For Dynamic Sidebar (Widget)

1
<?php dynamic_sidebar( 'header-text' ); ?>

For Current User Info

1
2
3
4
<?php global $user_login , $user_email;      
	get_currentuserinfo();
      echo $user_email ;
?>

Add comment


 

Pin It on Pinterest

Share This