用于标签的wordpress短代码

时间:2014-04-24 03:15:16

标签: wordpress shortcode

我通过自定义帖子类型添加了这个。但我想通过短代码方法和条件语句来创建它。这该怎么做 ?请问这个人吗?

            <?php
                $args = array( 'posts_per_page' => 4, 'post_type'=> 'tab-items');
                $myposts = get_posts( $args );
            ?>
            <!--nab section date content area-->
            <div class="date_section_area">
                <!-- Nav tabs -->
                <div class="date_section_list">
                    <ul>
                        <?php foreach( $myposts as $post ) : setup_postdata($post); ?>
                            <li><a href="#<?php the_ID(); ?>" data-toggle="tab"><?php the_title(); ?></a></li>
                        <?php endforeach; 
                        wp_reset_postdata(); ?>
                    </ul>
                </div>
                <!-- Tab panes -->
                <div class="date_section_content">
                    <?php foreach( $myposts as $post ) : setup_postdata($post); ?>
                    <div class="single_date_section_content tab-pane fade" id="<?php the_ID(); ?>">
                        <?php the_post_thumbnail('tab-image'); ?>
                        <?php the_content(); ?>
                    </div>
                    <?php endforeach; 
                        wp_reset_postdata(); ?>
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

据我了解你的问题,

我知道你可以这样做: -

add_shortcode('my_post_shorcode', 'my_shortcode_function');
function my_shortcode_function($attr, $content)
{
    extract(
        shortcode_atts( array(
            'posts_per_page' => '4',
            'post_type' => 'tab-items',
            'style' => 'default'
        ), $atts)
    );

    if($style == 'style-1')
    {
        //Add Your Style-1
    } else if($style == 'style-2') {
        //Add Your Style-2
    } else {
        $args = array( 'posts_per_page' => $posts_per_page, 'post_type'=> $post_type);
        $myposts = get_posts( $args );
        $tab_title = "";
        $tab_content = "";
        foreach ($myposts as $post)
        {
            setup_postdata($post);

            $tab_title .= '<li><a href="#tab-post-'.the_ID().'" data-toggle="tab">'.the_title().'</a></li>';
            $tab_content .= '<div class="single_date_section_content tab-pane fade" id="tab-post-'.the_ID().'>
            ' . the_post_thumbnail('tab-image') . the_content() . '
            </div>';

            wp_reset_postdata();
        }
        ?>
        <!--nab section date content area-->
        <div class="date_section_area">
            <!-- Nav tabs -->
            <div class="date_section_list">
                <ul>
                    <?php echo $tab_title;?>
                </ul>
            </div>
            <!-- Tab panes -->
            <div class="date_section_content">
                <?php echo $tab_content;?>
            </div>
        </div>
        <?php 
    } 
}

短代码将是这样的: -

[my_post_shorcode posts_per_page="4" post_type="tab-items" style="default"]

希望这会对你有所帮助。