Search This Blog

Monday 25 December 2017

Register nav menu in wordpress

Add in functions.php


<?php
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
  register_nav_menu( 'test', __( 'Test Menu', 'theme-slug' ) );
}
?>
Register nav menu in wordpress

Sunday 3 December 2017

Create page templates in wordpress.

<?php

/**
 * Template Name: PHP-AJAX-HTML
 *
 */

get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

Yes It's working fine.

</main><!-- .site-main -->
</div><!-- .content-area -->

<?php get_footer(); ?>


Create page templates in wordpress

Thursday 30 November 2017

Get Sidebar in wordpress.

Get Sidebar in wordpress.

<?php dynamic_sidebar( 'sidebar-1' ); ?>

Wednesday 29 November 2017

Register sidebar in wordpress.

Register sidebar in wordpress.

add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'theme-slug' ),
        'id' => 'sidebar-1',
        'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget'  => '</li>',
'before_title'  => '<h2 class="widgettitle">',
'after_title'   => '</h2>',
    ) );
}

Tuesday 28 November 2017

Get custom post type in wordpress.

Get custom post type in wordpress.

$args = array( 'post_type' => 'my_custom_post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;

Monday 27 November 2017

Insert new custom post type in wordpress.

Insert new custom post type in wordpress.

// Create post object
$my_post = array(
  'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
  'post_content'  => $_POST['post_content'],
  'post_status'   => 'publish',
  'post_type'     => 'my_custom_post',
  'post_author'   => get_current_user_id()

);

// Insert the post into the database
wp_insert_post( $my_post );

Sunday 26 November 2017

Register post type in wordpress.

Register post type in wordpress.

function create_posttype() {
  register_post_type( 'my_custom_post',
    array(
      'labels' => array(
        'name' => __( 'My custom post' ),
        'singular_name' => __( 'My custom post' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'my_custom_post'),
    )
  );
}
add_action( 'init', 'create_posttype' );

Saturday 25 November 2017

How to add discount to cart total using woocommerce ?

How to add discount to cart total using woocommerce ?

add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart');
function discount_when_produts_in_cart( ) {
    global $woocommerce;
    if( $woocommerce->cart->cart_contents_count > 3 ) {
        $coupon_code = 'maryscode';
        if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>You have more than 3 items in your cart, a 10% discount has been added.';
    }
}

Check product if already exist in to cart using woocommerce.

Check product if already exist in to cart using woocommerce.



add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );

function check_product_added_to_cart( $passed, $product_id, $quantity) {

foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['product_id'] == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
return $passed;
}