Term | Description |
---|---|
Theme basic | /* |
Show blog post / content of single post / content of page. | if (have_posts()) : while (have_posts()) : the_post(); //Content endwhile; else : echo 'No content found'; endif; |
the_title() | Prints the title of the blog post/page |
the_content() | Prints the content of the blog post/page |
the_permalink() | Prints the link of the blog post/page |
get_header() | Includes header.php |
get_footer() | Includes footer.php |
bloginfo('charset') | Prints character set of the website |
bloginfo('name') | Prints name of the website |
wp_head() | Includes CSS and others of plugin at the head section. |
language_attributes() | Includes language attribute. |
body_class() | Prints css class for body. |
bloginfo('description') | Prints the tagline/description of the website. |
home_url() | Returns home pages url. |
wp_footer() | Includes JavsScript links in footer. |
get_stylesheet_uri() | Retrieves stylesheet URI ie(http...../style.css). |
Include style sheet and script |
function learningWordPress_resources() { wp_enqueue_style('style', get_stylesheet_uri()); wp_enqueue_script('my-script', get_template_directory_url().'/js/script.js', NULL /*Dependency like jQuery*/, 1.0, true/*before closing body tag*/); wp_localize_script("my-script", "myData", array( 'sky' => 'blue', 'grass' => 'green', 'nonce' => wp_create_nonce('wp_rest') )); } add_action('wp_enqueue_scripts', 'learningWordPress_resources'); |
add_action('wp_enqueue_scripts', 'unique function name') | Writtern in functions.php. Includes style.css at the header |
Register two navigation menu locations. |
register_nav_menus(array( 'primary' => __( 'Primary Menu'), 'footer' => __( 'Footer Menu'), )) |
Primary menu position |
$args = ['theme_location' => 'primary']; wp_nav_menu($args); |
li.current-menu-item | Active navigation menu item class |
is_page(id)/is_page(slug) | Returns true or false depending on id |
is_page(id)/is_page(slug) | Returns true or false depending on id |
Special Template | /* Template Name: Special Layout */ |
wp_list_pages() | Prints all the pages titles as unordered list |
Prints child pages | $args = array('child_of' => write_function_to_get_top_ancestor_id, 'title_li' => '');
wp_list_pages($args);
|
get_the_title(id) | Returns the title of a page of id |
get_the_permalink(id) | Returns the permalink of a page of id |
li.current-page-item | Active page class. Used in child page of a menu |
li.current-page-ancestor | Active page's parent's class. Used in menu page |
the_time('F j, Y g:i a') | Prints date and time within a loop |
the_author() | Prints the name of the author |
get_author_posts_url(get_the_author_meta('ID')) | Returns author link |
get_the_category() | Returns all the categories of this post. Consider, each item is $category. Where $category->term_id returns id and $category->cat_name returns name. |
Archive | if ( is_category() ) { single_cat_title(); } elseif ( is_tag() ) { single_tag_title(); } elseif ( is_author() ) { the_post(); echo 'Author Archives: ' . get_the_author(); rewind_posts(); } elseif ( is_day() ) { echo 'Daily Archives: ' . get_the_date(); } elseif ( is_month() ) { echo 'Monthly Archives: ' . get_the_date('F Y'); } elseif ( is_year() ) { echo 'Yearly Archives: ' . get_the_date('Y'); } else { echo 'Archives:'; } |
the_excerpt()/get_the_excerpt() | Used to get excerpt content |
Featured Image support | add_theme_support('post-thumbnails'); add_image_size('small-thumbnail', 180, 120, true); add_image_size('square-thumbnail', 80, 80, true); add_image_size('banner-image', 920, 210, array('left', 'top')); |
the_post_thumbnail('small-thumbnail') / the_post_thumbnail('banner-image') | Prints the html code for thumbnail image |
get_search_form() | Prints the html code for search form |
the_search_query() | Prints the search query |
get_template_part('filename') / get_template_part('filename', 'part') | Includes filename.php / filename-part.php |
get_template_part('content', get_post_format()) | If the post has a format ie. aside, then content-aside.php will be included otherwise content.php. This function is used in blog page to design each post differently. |
Add a widget Location or sidebar | In functions.phpfunction ourWidgetsInit() { register_sidebar( array( 'name' => 'Sidebar', 'id' => 'sidebar1', 'before_widget' => '<div class="widget-item">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widget-title">', 'after_title' => '</h2>', )); } add_action('widgets_init', 'ourWidgetsInit');In sidebar.php if (is_active_sidebar('sidebar1')) : dynamic_sidebar('sidebar1'); endif; |
WP_Query() to get the post from category 1 | In functions.php$posts = new WP_Query('cat=1&posts_per_page=2'); if ($posts->have_posts()) : while ($posts->have_posts()) : $posts->the_post(); get_template_part('content', get_post_format()); endwhile; else : echo 'No content found'; endif; |
Customize color picker for link | In functions.phpfunction learningWordPress_customize_register( $wp_customize ) { $wp_customize->add_section('lwp_standard_colors', array( 'title' => __('Standard Colors', 'LearningWordPress'), 'priority' => 30, )); $wp_customize->add_setting('lwp_link_color', array( 'default' => '#006ec3', 'transport' => 'refresh', )); $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'lwp_link_color_control', array( 'label' => __('Link Color', 'LearningWordPress'), 'section' => 'lwp_standard_colors', 'settings' => 'lwp_link_color', ) ) ); } add_action('customize_register', 'learningWordPress_customize_register');Then add the following code to output customize CSS <?php function learningWordPress_customize_css() { ?> <style type="text/css"> a { color: <?php echo get_theme_mod('lwp_link_color'); ?>; } </style> <?php } add_action('wp_head', 'learningWordPress_customize_css'); ?> |
WP_Customize_Color_Control() |
|
wp_get_attachment_url(($id) | Returns attachment URL from attachment ID |
wpautop($text) | Replace single new line with br and double new line with p. Outputs html. Used to show the text of textarea of dashboard |
get_avatar($id) | Returns avatar |
WordPress pagination |
|
REST API endpoints |
https://wordpress.ict4today.com/wp-json/wp/v2/posts?per_page=2&categories=3 https://wordpress.ict4today.com/wp-json/wp/v2/pages posts, pages, comments, users, media etc |
current_user_can('administrator'); | Check if the current user is logged in an administrator |
REST API |
add_action( 'rest_api_init', function () { //wp-json/wp/v1/hello register_rest_route('wp/v2', '/hello', array( 'methods' => 'GET', 'callback' => 'my_custom_route_callback', )); }); |
WordPress REST API Basic Auth |
Create Password > wp-admin>Users>ProfileBasic Auth |
WordPress Plugin Basics | Basic Plugin development |
WooCommerce DB info |
WooCommerce products and orders are stored in wp_posts table. For product post type is product and for Order shop_order .
Taxonomy (in wp_term_taxonomy ) for product category is product_cat and for tags product_tag .
So the actual product category names are stored in wp_terms table.
|