Skip to content

Instantly share code, notes, and snippets.

@subrotoice
Last active September 4, 2023 13:23
Show Gist options
  • Save subrotoice/4a56b482853882f4a69e1468b13c5767 to your computer and use it in GitHub Desktop.
Save subrotoice/4a56b482853882f4a69e1468b13c5767 to your computer and use it in GitHub Desktop.
1. ইন্টেক্স পেজে আগে সব কাজ করে নিতে হবে মেনু, উইগেট, থিম অপশন সব,
2. তারপর ভাঙতে হবে header.php, footer.php, sidebar.php
3. তারপর index.php থেকে single.php, page.php, archive.php তৈরি করতে হবে ( প্রথমে ভার্সন কম করতে হবে, যেমনঃ সাইডবারে একটা ভার্সন রাখলেই হবে পরে চাইলে করা যাবে )
WP Theme Development (Create PHP Files)
header.php
==================================
<?php bloginfo('name'); ?>
<?php bloginfo('description'); ?>
<?php bloginfo('home'); ?> // Home Page link
<?php echo get_template_directory_uri(); ?>/
<?php bloginfo('stylesheet_url'); ?>
<?php wp_head(); ?> // Place before closing head tag
<?php wp_footer()?> // Place before closing body tag
index.php
==================================
<?php get_header(); ?>
<?php get_footer(); ?>
<?php get_sidebar(); ?> // include sidebar.php
<?php get_template_part('file_name'); ?> // without extention file name ie. loop.php you can use 'loop'
functions.php
==================================
// Register widget
function subroto_widget_areas() {
register_sidebar( array(
'name' => __( 'Left Menu', subroto ),
'id' => 'left_sidebar',
'before_widget' => '<div class="single_sidebar">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action('widgets_init', 'subroto_widget_areas');
Sidebar.php (Call)====================================================
<?php dynamic_sidebar('left_sidebar'); ?> // you have to use sidebar id
Multiple Widget----
/* Better way to add multiple widgets areas */
function widget_registration($name, $id, $description,$beforeWidget, $afterWidget, $beforeTitle, $afterTitle){
register_sidebar( array(
'name' => $name,
'id' => $id,
'description' => $description,
'before_widget' => $beforeWidget,
'after_widget' => $afterWidget,
'before_title' => $beforeTitle,
'after_title' => $afterTitle,
));
}
function multiple_widget_init(){
widget_registration('Footer widget 1', 'footer-sidebar-1', 'test', '', '', '', '');
widget_registration('Footer widget 2', 'footer-sidebar-2', 'test', '', '', '', '');
// ETC...
}
add_action('widgets_init', 'multiple_widget_init');
==================================================================================================
WP Theme Development (Blog Page Query)
Post Query in index.php
=====================================
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Your Post Query here -->
// var_dump($post); // See post data in raw format; // Get ACF fields: $acf_fields = get_fields(); var_dump($acf_fields);
<?php endwhile; ?>
<?php endif; ?>
Post Information Query in index.php
=====================================
<?php the_title(); ?>
<?php the_permalink(); ?>
<?php the_time('M d, Y') ?> // There are a lots of format in wp directory
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php the_category(', '); ?>
<?php comments_popup_link('No Comment', '1 Comment', '% Comments'); ?> // Link and number of post comment
Pagination in index.php
=====================================
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts') ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>') ); ?></div>
WP Theme Development (Single Post & Archive Page)
single.php
================================================================
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('404 Error&#58; Not Found'); ?></h3>
<?php endif; ?>
archive.php
================================================================
for Archive & Archive Post List
<h1>
<?php if (have_posts()) : ?>
<?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 the'); ?> '<?php echo single_cat_title(); ?>' <?php _e('Category'); ?>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<?php _e('Archive for the'); ?><?php single_tag_title(); ?> 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'); ?>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<?php _e('Blog Archives'); ?>
<?php } ?>
<?php endif; ?>
</h1>
For archive post query
<?php get_template_part( 'post-excerpt' ); // Post Excerpt (post-excerpt.php) ?>
Or, <?php get_template_part('post-loop'); ?>
If no post in archive or 404 // না দিলে archive.phpকোন কাজ করবে না।
<?php else : ?>
<h3><?php _e('404 Error&#58; Not Found'); ?></h3>
<?php endif; ?>
404.php
<h2>404 Error&#58; Not Found</h2>
<p>Sorry, but the page you are trying to reach is unavailable or does not exist.</p>
page.php
================================================================
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('404 Error&#58; Not Found'); ?></h3>
<?php endif; ?>
WP Theme Development (Stylize Comments)
================================================================
commet.css
http://pastebin.com/VaxfkmMe
functions.php
For enable comment
function comment_scripts(){
if ( is_singular() ) wp_enqueue_script( 'comment-reply' );
}
add_action( 'wp_enqueue_scripts', 'comment_scripts' );
Feature Image & Image Size=========================
//Default WordPress ----------------------------
the_post_thumbnail( 'thumbnail' ); // Thumbnail (150px square hard cropped)
the_post_thumbnail( 'medium' ); // Medium resolution (maximum 300px width and height)
the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' ); // Large resolution (maximum 1024px width and height)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
Recommended Image Sizes for WordPress Content------------------
Blog posts: 1200 x 630px
Hero images (full screen images): 2880 x 1500px
Landscape feature image: 900 x 1200px
Portrait feature image: 1200 x 900
Fullscreen slideshow: 2800 x 1500px
Gallery images: 1500px x auto width
For enable featured image------------------------------
add_theme_support( 'post-thumbnails', array( 'post' ) );
for enable crop feature-------
set_post_thumbnail_size( 200, 200, true );
add_image_size( 'post-image', 150, 150, true );//This size show in browser'post-image’ is the class which I use in post-loop.php
true is hard crop, here the outermost 50% (500px) cut off from the left and right sides. Most importantly, it won’t be in the 5 x 4 dimension you expected, but 5 x 2, that means dimention change
set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, resize mode
set_post_thumbnail_size( 50, 50, true ); // 50 pixels wide by 50 pixels tall, crop mode
add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
Here is an example of how to use this new Post Thumbnail size in theme template files.
<?php the_post_thumbnail( 'category-thumb' ); ?>
the_post_thumbnail(); // the_post_thumbnail( string|array $size = 'post-thumbnail', string|array $attr = '' )
the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large'); // Large resolution (default 640px x 640px max)
the_post_thumbnail('full'); // Original image resolution (unmodified)
the_post_thumbnail( array(100,100) ); // Other resolutions
post-loop.php
Using Featured Image
<?php the_post_thumbnail('post-image', array('class' => 'post-thumb')); ?>
For Dynamic Menu
================================================================
// past it in functions.php
function wpj_register_menu() {
if (function_exists('register_nav_menu')) {
register_nav_menu( 'main-menu', __( 'Main Menu') );
}
}
register_nav_menu( 'menu_footer', __( 'Footer Menu') );
register_nav_menu( 'side_manu', __( 'Side Menu') );
add_action('init', 'wpj_register_menu');
header.php
=======================================================
<?php wp_nav_menu( array( 'theme_location' => 'main-menu') ); ?>
<?php wp_nav_menu(array('theme_location' => 'main-menu','menu_class' => 'nav')); ?>
<?php wp_nav_menu(array('theme_location' => 'primary-menu', 'container_id' => 'navigation', 'menu_class' => 'nav')); ?>
-------Updated-----26/Sep/2017-----Subroto-------
<?php
$args = array(
'theme_location' => 'header_menu',
'menu_class' => 'menu',
'menu_id' => 'primary-menu',
'container' => 'false'
);
wp_nav_menu( $args );
?>
An amazing readmore function
functions.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;}
loop-post.php
==================================================================
<?php echo excerpt('15'); ?>
Using Custom Templates
<?php
/*
* Template Name: Custom Template Name
*/
get_header(); ?>
WP Theme Development (Custom Category Post Query & Register Custom Post)
Query Post from a specific category
================================================================
<?php query_posts('post_type=post&post_status=publish&category_name=CSS3 Tutorial&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php get_template_part('post-loop')?>
// পোস্ট কোয়েরি ব্যবহার না করলে সব পোস্ট দেখায় কিন্তু ব্যবহার করলে কোয়েরি সমর্থত পোস্ট দেখায়।
Register Custom Post
================================================================
Pastebin Code Link: http://pastebin.com/Fkgnu2KL
// Query Custom Post List
<?php query_posts('post_type=testimonial&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
// Query Custom Post Single
copy your single.php file & rename single-<custom-post>.php
<custom-post> = Custom Post
Wordpress Theme Development (Custom Field Tips & Tricks-1)
// Suppose we use custom field as 'url'
// Display Custom Field Data
<?php $key="url"; echo get_post_meta($post->ID, $key, true); ?>
// Display custom field if exists
<?php $image = get_post_meta($post->ID, 'url', true);
if($image) : ?>
<img src="<?php echo $image; ?>" alt="" />
<?php endif; ?>
// Conditional Custom Field
<?php
$url = get_post_meta( $post->ID, 'url', true );
if ( $url ) {
echo $url;
} else {
the_permalink();}?>
// Codestar Framework---
<?php $acf_fields = get_fields(); // all fields data
var_dump($post, $acf_fields); // display default post and all custom field data?>
// Conditional Codestar Framework---
<?php if(cs_get_option( 'slogan' )){
echo cs_get_option( 'slogan' ) ;
} else {
echo "Pour nous joindre";
} ?>
// SMOF Options Framework---
<?php global $data; ?>
<?php if($data['banner_overlap_text']): ?>
<?php echo $data['banner_overlap_text']; ?>
<?php else: ?>
<p>Nous offrons des</p>
<?php endif; ?>
// To take Background option.
function.php
<?php $class = array(
//custom background
'default-color' => '#000',
'default-image' => '',
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => ''
);
add_theme_support( 'custom-background', $class ); ?>
// header.php
<body <?php body_class(); ?>>
// All header.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title('&raquo;','true','right'); ?><?php if ( is_single() ) { ?> Blog Archive &raquo; <?php } ?><?php bloginfo('name'); ?></title>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_url'); ?>" media="screen" />
<?php wp_head(); ?>
</head>
screenshot.png(300 X 225)
style.css
==========================================================
/*
Theme Name: Bootstrap to WordPress
Theme URI: http://subroto.net/theme
Author: Subroto Biswas
Author URI: http://subroto.net
Description: A reuseable theme that can be start for example to convert Bootstrap to wordpress
Version: 1.0.0
License: General Public License V1 or later
License URI: http://subroto.net/license/bootstrap-to-wp
Text Domain: Bootstrap-to-WordPress
*/
Child Theme------Start--------
/*
Theme Name: Child Theme - Twenty Seventeen
Theme URI: http://www.edeves.com
Description: Twenty Seventeen child-theme with seriously good looks
Author: Subroto Biswas
Author URI: http://www.subroto.net
Template: twentyseventeen // Folder Name of Parent Theme, Not Theme name
Version: 1.0
*/
@import url('../twentyseventeen/style.css');
Child Theme------End--------
/* Move featured image box under title */
add_action('do_meta_boxes', 'change_image_box');
function change_image_box() {
remove_meta_box( 'postimagediv', carousel-image, 'side' );
add_meta_box('postimagediv', __('Upload Carousel Image'), 'post_thumbnail_meta_box', carousel-image, 'normal', 'high'); }
add this code on your functions.php change carousel-image to your post type
/* Some Advance features */
<?php
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories=get_categories($args); // <?php wp_list_categories( $args ); ?> Display in a formatted way.
echo '<ul class="dropdown-menu">';
foreach ( $categories as $category ) {
echo '<li><a href="' . get_category_link( $category) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>'.$category->name.'</a></li>';
}
echo '</ul>';
?>
Get all categories lists
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
?>
Or you can use this
<?php
wp_get_categories( array('orderby' => 'name','parent' => 0) );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
?>
List all archives as month
<?php
$args = array(
'type' => 'monthly',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => true,
'echo' => 1,
'order' => 'DESC'
);
echo '<ul class="dropdown-menu">';
wp_get_archives( $args ); //here this function does not return any array. But it print all.
echo '</ul>';
/*
* Register Custom Post type
*/
function custom_post_type_subroto() {
// Set UI labels for Custom Post Type Event
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'SubrotoNews' ),
'singular_name' => _x( 'Event', 'Post Type Singular Name', 'SubrotoNews' ),
'menu_name' => __( 'Events', 'SubrotoNews' ),
'parent_item_colon' => __( 'Parent Event', 'SubrotoNews' ),
'all_items' => __( 'All Event', 'SubrotoNews' ),
'view_item' => __( 'View Event', 'SubrotoNews' ),
'add_new_item' => __( 'Add New Event', 'SubrotoNews' ),
'add_new' => __( 'Add New', 'SubrotoNews' ),
'edit_item' => __( 'Edit Event', 'SubrotoNews' ),
'update_item' => __( 'Update Event', 'SubrotoNews' ),
'search_items' => __( 'Search Event', 'SubrotoNews' ),
'not_found' => __( 'Not Found', 'SubrotoNews' ),
'not_found_in_trash' => __( 'Not found in Trash', 'SubrotoNews' ),
);
// Set other options for Custom Post Type
$args_event = array(
'label' => __( 'Event', 'SubrotoNews' ),
'description' => __( 'Event news and reviews', 'SubrotoNews' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'Event', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres', 'category', 'post_tag' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'query_var' => true,
'menu_position' => 6,
'can_export' => true,
'capability_type' => 'post',
'has_archive' => event,
'hierarchical' => false,
'rewrite' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'map_meta_cap' => true,
);
// Registering your Custom Post Type
register_post_type( 'event', $args_event );
//Here you can add more custom post using this ie.
//register_post_type( 'testmonial', $args_testmonial );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type_subroto' );
//End Custom Post type
// Display Custom post type
<?php
$args = array(
'post_type' => 'event',
'category_name' => $category,
'posts_per_page' => 10,
'paged' => $paged
);
query_posts( $args );
if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Content goes here
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment