Thursday 26 September 2024

Create testimonials custom post type in wordpress

function custom_testimonial_post_type() {

    $labels = array(

        'name'               => 'Testimonials',

        'singular_name'      => 'Testimonial',

        'menu_name'          => 'Testimonials',

        'name_admin_bar'     => 'Testimonial',

        'add_new'            => 'Add New',

        'add_new_item'       => 'Add New Testimonial',

        'new_item'           => 'New Testimonial',

        'edit_item'          => 'Edit Testimonial',

        'view_item'          => 'View Testimonial',

        'all_items'          => 'All Testimonials',

        'search_items'       => 'Search Testimonials',

        'not_found'          => 'No Testimonials found.',

        'not_found_in_trash' => 'No Testimonials found in Trash.',

    );

    

    $args = array(

        'labels'             => $labels,

        'public'             => true,

        'publicly_queryable' => true,

        'show_ui'            => true,

        'show_in_menu'       => true,

        'query_var'          => true,

        'rewrite'            => array( 'slug' => 'testimonials' ),

        'capability_type'    => 'post',

        'has_archive'        => true,

        'hierarchical'       => false,

        'menu_position'      => null,

        'supports'           => array( 'title', 'editor', 'thumbnail' ),

    );

    

    register_post_type( 'testimonial', $args );

}

add_action( 'init', 'custom_testimonial_post_type' );


add_shortcode( 'testimonials', 'wpdocs_testimonials_func' ); 


function wpdocs_testimonials_func( $atts ) {

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    $args = array(

        'post_type' => 'testimonial',

        'posts_per_page' => 5, // Number of testimonials per page

        'paged' => $paged

    );

    $testimonial_query = new WP_Query( $args );


    if ( $testimonial_query->have_posts() ) :

        while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post();


$image_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); 

     

 endwhile;


        // Pagination

        $total_pages = $testimonial_query->max_num_pages;

$total_posts = $testimonial_query->found_posts; 

$current_post_count = $testimonial_query->post_count; 

        if ( $total_pages > 1 ) {

echo '<div class="paginationcustom manjeet">';

            $current_page = max( 1, get_query_var( 'paged' ) );

    echo '<ul class="custom-pagination">';


    // Generate pagination links

    $pagination_links = paginate_links( array(

        'base'      => get_pagenum_link( 1 ) . '%_%',

        'format'    => 'page/%#%',

        'current'   => $current_page,

        'total'     => $total_pages,

        'prev_text' => '‹', // Previous arrow

        'next_text' => '›', // Next arrow

        'type'      => 'array', // Output as array to customize

    ));


    // Loop through the links and add custom class

    foreach ( $pagination_links as $link ) {

        if ( strpos( $link, 'current' ) !== false ) {

            echo '<li class="active">' . $link . '</li>'; // Add active class

        } else {

            echo '<li>' . $link . '</li>';

        }

    }


    echo '</ul>';

    echo '<div class="results-count">';

    echo 'Showing ' . $current_post_count . ' of ' . $total_posts . ' results';

    echo '</div>';

echo '</div>';

        }


        wp_reset_postdata();

    else :

        echo '<p>no.testimonial found</p>';

    endif;

    ?>

  </div>

</div>

<?php }


How can i put "posted x minutes ago on my posts?

 Just place this code in your functions.php

function time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';

    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');

}

To use it anywhere in your theme (for example, on single.php), place this code where you want the "time ago" feature displayed:

<?php echo time_ago(); ?>