Create shortcode to display posts of custom post type in wordpress

I had to display custom post type posts using short code and for this I have made research and got very nice code in which made some modification.

If anybody need to display custom post type posts using shortcode then you can use this code:

customposttype

Just paste this code in functions.php


//custom post type function
function verdict_lists() {

register_post_type( 'verdicts',
// CPT Options
array(
'labels' => array(
'name' => __( 'Verdicts' ),
'singular_name' => __( 'Verdict' )
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'verdicts'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'verdict_lists' );

add_shortcode( 'custom_posts', 'verdict_custom_posts' );
function verdict_custom_posts( $atts ){
global $post;
$default = array(
'type' => 'post',
'post_type' => '',
'limit' => -1,
'status' => 'publish'
);
$r = shortcode_atts( $default, $atts );
extract( $r );

if( empty($post_type) )
$post_type = $type;

$post_type_ob = get_post_type_object( $post_type );
if( !$post_type_ob )
return '

No such post type ' . $post_type . ' found.

';

//$return = '

' . $post_type_ob->name . '

';

$args = array(
'post_type' => $post_type,
'numberposts' => $limit,
'post_status' => $status,
);

$posts = get_posts( $args );
if( count($posts) ):
$return .= '

    ';
    foreach( $posts as $post ): setup_postdata( $post );
    $return .= '

  • '. get_the_title().'

    '. get_the_excerpt().'

    Read more

  • ';

    endforeach; wp_reset_postdata();
    $return .= '

';
else :
$return .= '

No posts found.

';
endif;

return $return;
}

You can download code on word file also Click Here

Now on editor just paste this code:

[custom_posts type=’verdicts’]

Display category name without link in post list

I know this is very small things from technical point of view but many times we stuck to display category name without link so I have mention below the code which will display the category name without link.

Like this:

cat-name

Category name with link:


the_category(',');

Category name without Link:

$category = get_the_category();
echo $category[0]->cat_name;

How to make center bootstrap pop up box or modal

Today, I had need to display bootstrap pop up box in center of screen and after searching got a beautiful solution using javascripts.

Just you need add this JavaScript code and your bootstrap pop up or modal box will be center of the screen.

Like this:

screenshot

$(function() {
function reposition() {
var modal = $(this),
dialog = modal.find('.modal-dialog');
modal.css('display', 'block');

// Dividing by two centers the modal exactly, but dividing by three
// or four works better for larger screens.
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));
}
// Reposition when a modal is shown
$('.modal').on('show.bs.modal', reposition);
// Reposition when the window is resized
$(window).on('resize', function() {
$('.modal:visible').each(reposition);
});
});

Hope, it will help for some people.

How to highlighted category menu item when post details page is opened?

Today I need to highlight category menu item when post details page open. For this I have searched and get a solution but that is used for page so just little I have made change on the code and it works like a charm.

postpage

So anybody need to highlight category itme menu after click on post then use this code in footer :


if(is_single()){
<script type="text/javascript" src="/js/popup.js">

jQuery("li.current-post-ancestor").addClass('current-menu-item');

}

And if you want to page highlighted then use this code in footer :


if (is_page()) { // displaying a child page

jQuery("li.current-page-ancestor").addClass('current-menu-item');

}

Thanks, Cheers the code!

Solution for Fatal error: Call to undefined method WooCommerce::add_error()

As we know that wordpress and plugins are always updated that’s why old plugins sometime generate error after installation. Some functionality does work because on updated version some method has removed or update on another way.

Today I have installed Authorize.net AIM Payment Gateway For WooCommerce plugin for checkout by credit card and master card but when we go to check out then I got error like this :

error

Now, I have searched on google same issue many places but not get good solution.

But, After some time solve this problem just add this method on woocommerce.php after 178 line on this location :
wp-content/plugins/woocommerce/woocommerce.php


public function add_error( $error ) {
_deprecated_function( 'Woocommerce->add_error', '2.1', 'wc_add_notice' );
wc_add_notice( $error, 'error' );
}

Change label price “Free” to other text or blank in woocommerce

Well, When we enter the 0 price in woo commerce it display default “Free” text on products page. But We need to change text “Free” to other or It should return to blank.

So lets Follow this instruction:

Just paste this code in functions.php in your theme file.


add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price' );
function hide_free_price($price){
return 'Price Not Announced';
}

In return you set either blank or as per your need!

Enjoy the Code!

How to redirect #page url with another page?

As we know that some time we need to redirect some pages url to another page ulr for seo purpose but when your page url is start with hash symbol then that hashtag url is not working redirect using .htaccess for this you can use this solution. I have used in wordpress. Just add the code on functions.php of your theme and edit the url.

Here is the code which worked like charm:

download code

How to create widgets and call on front end in wordpress

widgets
Widgets is common things in wordpress but it is very use full to display short content and other information on content area or sidebar on front end area. If you create custom theme then you can create widgets with some code adding on themes functions.php file and call on frontend with one line code using name of widgets:

Just add this code on your functions.php file:


if (function_exists('register_sidebar')) {
register_sidebar(array(
'name'=> 'Home introduction',
'id' => 'top_tabs',
'before_widget' => '

  • ',
    'after_widget' => '

‘,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
));
register_sidebar(array(
‘name’=> ‘Top Sidebar’,
‘id’ => ‘top_sidebar’,
‘before_widget’ => ‘

  • ‘,
    ‘after_widget’ => ‘

‘,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
));
register_sidebar(array(
‘name’=> ‘Left Sidebar’,
‘id’ => ‘left_sidebar’,
‘before_widget’ => ‘

  • ‘,
    ‘after_widget’ => ‘

‘,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
));
register_sidebar(array(
‘name’=> ‘Right Sidebar’,
‘id’ => ‘right_sidebar’,
‘before_widget’ => ‘

  • ‘,
    ‘after_widget’ => ‘

‘,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
));
}

To call on front end where you want to display widgets using this code:


if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Home introduction') ) : endif;

Download code source file click on this link

Where ‘Home introduction’ is name of widgets which is used in functions.php.

Thanks, Cheers the code