Custom Post type shortcodes

时间:2019-08-24 作者:ruby

我使用高级自定义字段在我的主题中创建自定义帖子类型(功能)。我想知道如何创建自定义帖子类型的短代码。因此,我只使用任何页面的自定义帖子类型的短代码来显示我在自定义帖子类型(功能)中添加的信息。

1 个回复
SO网友:gaurav mishra

对于shortcode,您可以使用Wordpress主题函数的add\\u shortcode函数。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\');
现在您可以使用[custom_video_slider] 无论您想在何处显示帖子。

相关推荐

Shortcode is not working

通过阅读教程,我创建了一个简单的快捷代码:<?php /* Plugin Name: GT NoTranslate Shortcode Description: Shortcode to wrap a span with the \"notranslate\" span around text. Version: 0.1 BETA Author: Andrew Truckle Author URI: http://www.trucklesoft.co.