Showing posts with label Shortcode. Show all posts
Showing posts with label Shortcode. Show all posts

Saturday, 12 September 2020

How to Custom Pagination For Custom Post in WordPress?

 

There are very simple steps for Pagination for any type of Post in WordPress by Shortcode with Ajax.

  1.  Write this below code in you active WordPress theme's funcntion.php file.
    • ie: \wp-content\themes\twentytwenty\functions.php
  2.  Put this shortcode in your page where you want to display WordPress Custome Post or Post with Ajax Pagination.
    • <?php echo do_shortcode('[php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"]'); ?> OR
    • [php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"]
### Code: ###
<?php
function function_php_kishan_wp_portfolio_post_list($atts, $content = null) {
    ob_start();
    $atts = shortcode_atts(
    array(
        'per_page' => '',
        'total' => '',
        'post_type' => '',
    ), $atts);
    $per_page = $atts['per_page'] ? $atts['per_page'] : '9';
    $total_post = $atts['total'] ? $atts['total'] : '-1';
    $post_type = $atts['post_type'] ? $atts['post_type'] : 'post';

    global $wpdb;
    $post_type = $post_type; // define your custom post type slug here
    // A sql query to return all post titles
    $results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = '$post_type'  and post_status = 'publish' order by post_title ASC limit 0,$per_page");
.
.
.
.
return ob_get_clean();
}
add_shortcode('php_kishan_wp_portfolio_post_list', 'function_php_kishan_wp_portfolio_post_list');
#### [php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"] ####

Click here for download full code.

Thursday, 11 June 2020

I want to remove shortcode from content [vc_row][vc_column][vc_column_text]

$content = get_the_content(); 
$content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $content);
$content = wp_strip_all_tags($content);
echo $content; 

### OR ###

$content = get_the_content(); 
$content = do_shortcode($content);
echo $content;

Monday, 16 April 2018

How To Create Shortcode In WordPress with parameters?



<?php
### the shortcode code add in theme’s functions.php file  ###
// "do_shortcode" but without the wrapping <p> of Shortcodes
function do_shortcode_boc($content) {
 $array = array (
        '<p>[' => '[',
        ']</p>' => ']',
        ']<br />' => ']'
    );
$content = strtr($content, $array);
$content = do_shortcode($content);
    return $content;
}

function shortcode_shortcodename($atts, $content = null) {   
$atts = shortcode_atts(
array(
'title' => '',
'image' => '',
        ), $atts);

$title = $atts['title'] ? $atts['title'] : '';
$image = $atts['image'] ? $atts['image'] : '';

$str = '<li>
<div class="icon_item">
<img src="'.$image.'">
<p>'.html_entity_decode($title).'</p>
<div class="icon_overlay">
<p>'.do_shortcode($content).'</p>
</div>
</div>
</li>';
return $str;
}
add_shortcode('shortcodename', 'shortcode_shortcodename');

?>

##### Using below code to display shortcode detail  #####

[shortcodename title="my title" image="example.jpg"]
<div>
<p> Shortcode content </p>
</div>
[/shortcodename]

##### OR #####

<?php
echo do_shortcode('[shortcodename]');
?>