0

I create a custom post type (features) in my theme with Advanced Custom Field. I want to know how i can create a shortcodes of my custom post type. so that i just used a shortcode of that custom post type of any page to display a informations that i added in my custom post type (features).

0

1 Answer 1

0

For shortcode, you can use add_shortcode function of Wordpress theme`s function.php.

function shortcode_video_slider()
{
    ?>
<div class="cr-carousel">
    <?php
    $loop = new WP_Query( array( 'post_type' => 'video_slider') ); //Change video_slider with your custom post name.
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div>
                <?php if ( has_post_thumbnail() ) { ?>
                    <div class="pimage">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                <?php } ?>
                    <div class="cr-slider-content">
                <div class="ptitle">
                    <?php
                    $terms = get_the_terms( get_the_ID() , 'video_slider_tex' );
                    // Loop over each item since it's an array
                    if ( $terms != null ){
                    foreach( $terms as $term ) {
                    ?>
                    <h2><a href="<?php the_permalink(); ?>"><?php echo $term->name; ?></a></h2>
                    <?php
                    unset($term); } }?>
                </div>
                <div class="cr-video-link">
                <?php 
                    $cr_video_link = get_post_meta( get_the_ID(), 'video_link', true );
                    echo '<a target="_blank" href="' . $cr_video_link . '" class="wplightbox" title="WordPress Carousel Plugin" data-width="1024" data-height="576">YouTube Lightbox</a>';
                ?>
                </div>
                <div class="cr-video-cat">
                    <div class="cr-slide-category"><span> For</span> <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></div>
                </div>
                    </div>
                </div>

        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            </div>
        <?php endif;
    endif;
    wp_reset_postdata();
    ?>
    </div>
<?php
}
add_shortcode('custom_video_slider','shortcode_video_slider');

Now you can use [custom_video_slider] where ever you want to display the posts.

8
  • I used this code at the top of the section of feature.php where i set the custom post type(features) <?php function shortcode_features() { ?> and add this at the end of section :<?php } add_shortcode('custom_shortcode_features','shortcode_features'); ?> but it is not working
    – ruby
    Commented Aug 24, 2019 at 6:56
  • Paste this code in your child themes function.php and replace post_type with your custom post type name. Commented Aug 24, 2019 at 7:07
  • I create custom post type through the Advanced custom field ,pls let me know how can i used it in functions.php
    – ruby
    Commented Aug 24, 2019 at 7:18
  • You should mention this in your question, that you have used ACF for the custom post type. Commented Aug 24, 2019 at 8:37
  • Sorry i forget to mention this , i want to edit my questions but a canntot found the edit button so i send my next questions to mention it and add code .
    – ruby
    Commented Aug 24, 2019 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.