Sunday, 17 July 2016

Register a Custom Menus Location In WordPress

To register a custom Menu location you must use the following function for registration.

register_nav_menu( $location, $description );
$location - The location where you want to display the menu in your theme (e.g.) - Primary,Secondary,Main Header etc.,
$description - The Description about the menu Location and the Menu.

Step: 1 Add the following lines in the function.php file.
function register_menu() {
    register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'register_menu');

Step: 2 Add the following lines in the file where you want the menu to display.

if ( has_nav_menu( 'primary-menu' ) ) { /* if menu location 'primary-menu' exists then use custom menu */
      wp_nav_menu( array( 'theme_location' => 'primary-menu') );
}

Saturday, 16 July 2016

Register and Display a Sidebar in Wordpress

To register and display the Sidebar in WordPress you have to follow the two steps.

Step: 1 You have to add this in the Function.php file in your theme
Below the "wpb alone should be replaced by your theme name"

function wpb_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'wpb' ),
        'id' => 'sidebar-1',
        'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' =>__( 'Front page sidebar', 'wpb'),
        'id' => 'sidebar-2',
        'description' => __( 'Appears on the static front page template', 'wpb' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    }
add_action( 'widgets_init', 'wpb_widgets_init' );

Step: 2 Display it Dynamically in the Page where ever you want by adding the below code

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
    <div id="secondary" class="widget-area" role="complementary">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </div>
<?php endif; ?>

Note: " is_active_sidebar( 'sidebar-1' )" - The sidebar-1 is the id of the registered sidebar in the function.php file. If the id is not used properly the Sidebar will not be displayed even if the registration is correct.

Custom Post Type Loop in Wordpress

In order to get posts from our custom post type you can follow the loop that is shown below by replacing the post type with your custom post type.

$query = array(
        'post_type'=> 'your_custom_post_type',
        'orderby'    => 'ID',
        'post_status' => 'publish',
        'order'    => 'DESC',
        'posts_per_page' => -1
    );
    $result = new WP_Query( $query );
    if ( $result-> have_posts() ) : ?>
        <?php while ( $result->have_posts() ) : $result->the_post(); ?>
            <?php
            // to get the post featured image as thumbnail
            the_post_thumbnail('thumbnail');
             ?>
            <?php
           // to get the Post title
           the_title();
            ?>
            <?php
           // to get the post content
            the_content();
           ?>
        <?php endwhile; ?>
    <?php endif; wp_reset_postdata(); ?>

This query will get the posts that are created under your custom post type and i have displayed the post featured image, post title and the post content.

Email Code PHP

This is a Simple Code which is written in the PHP for Sending Emails from One Server to all the Other Domains. It is very easy and little C...