Important Php Functions for Web Development
<?php echo "Sorry, Nothing Found"; ?>
<?php language_attributes(); ?>
<?php bloginfo('charset'); ?>
<?php bloginfo('name'); ?>
<?php bloginfo('description'); ?>
<?php echo home_url(); ?>
<?php get_template_part(); ?>
<?php get_sidebar(); ?>
<?php wp_head(); ?>
<?php wp_footer(); ?>
<?php body_class(); ?>
<?php the_post_thumbnail(); ?>
<?php get_search_form(); ?>
<?php the_search_query(); ?>
<?php wp_nav_menu(); ?>
<?php comments_template(); ?>
<?php (have_posts()) ?>
<?php the_post(); ?>
<?php the_permalink(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php wp_nav_menu(); ?>
<?php function(); ?>
<?php add_action(); ?>
<?php add_theme_support(); ?>
<?php add_theme_support('post-thumbnails'); ?>
<?php wp_enqueue_style(); ?>
<?php bloginfo('stylesheet_url'); ?> // this is old function
<?php wp_nav_menu(); ?>
<?php dynamic_sidebar('sidebar1'); ?>
N.B: Sidebar1 is the name of sidebar. you can type any name here
<?php comments_popup_link();?>
How to show comment in post?
Do not use comments_popup_link code. Use below code. this code is better than comments popup link: Example: */
<?php comments_popup_link('0 Comment', '1 Comment', '% Comments', 'comment_class', 'Comments Off'); ?>
<?php echo get_template_directory_uri(); ?>
Use of img tag in php:
<img src="<?php echo get_template_directory_uri(); ?>/img/picture.jpg" alt=" " />
functions.php code for stylesheet:
How to add stylesheet with index.php file. Here is the function: (type without closing end php in functions.php file (new method).
// stylesheet code start:
function style_resources(){
wp_enqueue_style('style', get_stylesheet_uri(), '', '1.0');
}
add_action('wp_enqueue_scripts', 'style_resources');
// stylesheet code end:
/* N.b: style_resources is the function name only. you can type any name here.*/
//Content or post:
// Post php code for index.php
<?php
if(have_posts()):
while(have_posts()): the_post(); ?>
<h2> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a> </h2>
<p><?php the_content(); ?></p>
<?php endwhile;
else:
echo "Sorry, Nothing Found";
endif;
?>
//post meta code:
<div class="post_meta">
Posted by: <?php the_author_posts_link(); ?> | Date: <?php the_time('M d, Y'); ?> | Category: <?php the_category(', '); ?> | <?php comments_popup_link('0 Comment', '1 Comment', '% Comments', 'comment_class', 'Comments Off'); ?>
</div>
// Top nav menu php code
// nav menu php code for header.php
<?php
$args = array(
'theme_location' => 'primary');
wp_nav_menu( $args );
?>
//nav menu php code for functions.php code
<?php
function nav_theme_setup(){
register_nav_menus(array(
'primary' => __( 'Primary Menu' ),
));
}
add_action('after_setup_theme' , 'nav_theme_setup');
?>
// footer menu php code for functions.php file:
'footer' => ('Footer Menu')
// footer menu php code for header.php file:
<?php
$menu = arrray (
'theme_location' => 'footer');
wp_nav_menus( $menu );
?>
// the excerpt php code for index.php file
<?php echo excerpt('30'); ?>
// the excerpt php code for functions.php file
<?php
function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(' ', $excerpt)." <a href='" .get_permalink($post-> ID) ."' class='readmore'>[Read More]</a>";
echo $excerpt;
}
?>
// feature or thumbnails images php code for index.php file:
<?php the_post_thumbnail(); ?>
// feature or thumbnails images php code for functions.php file:
add_theme_support('post-thumbnails');
// menu php code for header.php
<?php
$args = array(
'theme_location' => 'primary'
);
wp_nav_menu( $args ); ?>
// footer menu php code for header.php file:
<?php
$menu = arrray (
'theme_location' => 'footer');
wp_nav_menus( $menu );
?>
// menu function php code for functions.php
<?php
function nav_theme_setup(){
// Register Nav
register_nav_menus(array(
'primary' => __( 'Primary Menu' ),
'footer' => __( 'Footer Menu' ),
));
// Thumbnail Image function for featured images
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme' , 'nav_theme_setup');
?>
/* sidebar/widget index.php code:
sidebar1 is the name of sidebar or widget. you can type any name here.
*/
<?php dynamic_sidebar('sidebar1'); ?>
// sidebar/widget functions.php code:
function right_sidebar(){
register_sidebar(array(
'name' => 'right_sidebar',
'id' => 'sidebar1',
'before_widgets' => 'sidebar_item',
'after_widgets' => '</div>',
));
}
add_action('widgets_init', 'right_sidebar');
// Excerpt php code for functions.php file:
function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(' ', $excerpt)." <a href='" .get_permalink($post-> ID) ."' class='readmore'>[Read More]</a>";
echo $excerpt;
}
// excerpt php code for index.php file: you can change any word no in 30.
<?php echo excerpt('30'); ?>
/* Slider add code for functions.php file: ad this code before: add_action('after_setup_theme' , 'nav_theme_setup'); */
register_post_type('customslider' , array(
'labels' => array(
'name' => 'Slider',
'add_new_item' => 'Add New Slider',
),
'public' => true,
'supports' => array(
'title', 'thumbnail'
),
));
/* Slider add code for index.php file use: <img class="slide" before <?php the_post_thumbnail(); ?> */
<?php
$custslider = new WP_Query(
array(
'post_type' => 'customslider',
)
);
while($custslider->have_posts()): $custslider->the_post(); ?>
<?php the_post_thumbnail(); ?>
<?php endwhile;
?>
/*
one of the slider image source code:
<img src="<?php the_post_thumbnail_url('full'); ?>" title="#slidecaption<?php echo esc_attr( $i ); ?>" />
*/
/* Slider index.php code will be as like below: */
<?php
$custslider = new WP_Query(
array(
'post_type' => 'customslider',
)
);
while($custslider->have_posts()): $custslider->the_post(); ?>
<img class="slide" <?php the_post_thumbnail(); ?>
<?php endwhile;
?>
/* page number pagination code for index.php */
<?php if (function_exists("pagination")) { pagination($additional_loop->max_num_pages);} ?>
/* Page Number- Pagination code for functions.php */
function pagination($pages = '', $range = 4){
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){$pages = 1;}
}
if(1 != $pages){
echo "<div class=\"pagination\"><span class=\"page_no\">Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages)
echo "<a href='".get_pagenum_link(1)."' class=\"first\">« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."' class=\"previous\">‹ Previous</a>";
for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages)
echo "<a href=\"".get_pagenum_link($paged + 1)."\" class=\"next\">Next Page ›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."' class=\"last\">Last Page »</a>";
echo "</div>\n";
}}
/* pagination code end*/
/*
Archive code: Copy index.php file and name it archive.php and then paste below code from:
if (have_posts()):?> to while(have_posts()) : the_post(); ?>
*/
if(have_posts()) : ?>
<h2 class="archive_heding">
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<?php _e('Archive For'); ?> '<?php echo single_cat_title(); ?>' <?php _e('Category'); ?>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<?php _e('Archive For'); ?> <?php single_tag_title(); ?> <?php _e('Tag'); ?>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<?php _e(' Archive For '); ?> <?php the_time('F jS, Y'); ?>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<?php _e('Archive For'); ?> <?php the_time('F, Y'); ?>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<?php _e('Archive For'); ?> <?php the_time('Y'); ?>
<?php /* If this is a search */ } elseif (is_search()) { ?>
<?php _e('Search Results'); ?>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<?php _e('Author Archive For'); ?> '<?php echo get_the_author(); ?>'
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<?php _e('Blog Archives'); ?>
<?php } ?>
</h2>
<?php
while(have_posts()) : the_post(); ?>
/* archive code end*/
<!-- favicon icon link -->
<link rel="icon" href="img/favicon.png" type="image/png" />