如何仅包含选定的帖子(按 id) - WordPress 简码


How To Include Only Chosen Posts (by id) - Wordpress Shortcode

这个简码显示了我主页上最后的X(从最新到最旧排序)帖子。我想选择我想按 ID 显示哪些。怎么办?

我有一个简码,看起来像这样

这是输入的内容:

if(!empty($blog)){
        echo do_shortcode('[blog item_number=4]'); 
           }

这是此简码的代码。

function beau_blog($atts, $content = null, $code = '')
{
  
    global $imgsize;
    extract(shortcode_atts(array(
        'post_not_in' => '',
        'item_number' => '4',
	
    ), $atts));
    $loop = new WP_Query('&posts_per_page=' . $item_number);
    ?>
    <section class="beau-blog stretch-layout">
        <div class="blog-title"><?php echo get_post_meta(get_the_ID(), '_page_blog_text', TRUE); ?></div>
        <div class="blog-content"><?php echo get_post_meta(get_the_ID(), '_page_blog_des', TRUE); ?></div>
        <?php if ($loop->have_posts()) : ?>
            <div class="blog-lists">
                <?php $i = 0;
                while ($loop->have_posts()) : $loop->the_post();
                    $featuredID = wp_get_attachment_image_src(get_post_thumbnail_id(), 'single-post-thumbnail');
                    $date = strtotime( get_the_date() );
                    if ($i % 2 != 0) {
                        ?>
                        <div class="row-fluid">
                            <div class="span6 blog-odd hover-img beau-animated move-to-right">
                                
                                    <?php beau_resizer($featuredID[0], $imgsize['blog']['w'], $imgsize['blog']['h'], true, get_the_title(get_the_ID())); ?>
                                    
                                
                            </div>
                            <div class="span6 blog-odd-r beau-animated move-to-left">
                                <div class="odd-bg"></div>
                                <div class="content">
                                    
                                        <h3><?php the_title(); ?></h3>
                                    <div class="date"><?php echo date( 'l', $date ).' '.date( 'j', $date ).' '.date( 'F ', $date ).' '.date( 'Y ', $date ); ?></div>
                                    <div class="blog-description"><?php echo beau_excerpt(54); ?></div>
                                </div>
                            </div>
                        </div>
                    <?php
                    } else {
                        ?>
                        <div class="row-fluid bg-r">
                            <div class="span6 blog-even-l beau-animated move-to-right">
                                <div class="odd-bg"></div>
                                <div class="content">
                                    <h3><?php the_title(); ?></h3>
                                    <div class="date"><?php echo date( 'l', $date ).' '.date( 'j', $date ).' '.date( 'F ', $date ).' '.date( 'Y ', $date ); ?></div>
                                    <div class="blog-description"><?php echo beau_excerpt(54); ?></div>
                                </div>
                            </div>
                            <div class="span6 blog-odd hover-img beau-animated move-to-left">
                                --------
                                    <?php beau_resizer($featuredID[0], $imgsize['blog']['w'], $imgsize['blog']['h'], true, get_the_title(get_the_ID())); ?>
                                    
                                  
                                ----------
                            </div>
                        </div>
                    <?php
                    }
                    ?>
                    <?php $i++; endwhile; ?>
            </div>
            <?php
            wp_reset_postdata();
        endif;
        ?>
    </section>
    <?php
    $out = ob_get_contents();
    ob_end_clean();
    return $out;
}
add_shortcode('blog', 'beau_blog');
function beau_blog_style2($atts, $content = null, $code = '')
{
    global $imgsize;
    extract(shortcode_atts(array(
        'post_not_in' => '',
        'item_number' => '4'
    ), $atts));
    $loop = new WP_Query('&posts_per_page=' . $item_number);
    ?>
    <section class="beau-blog stretch-layout style2">
        <div class="blog-title"><?php echo get_post_meta(get_the_ID(), '_page_blog_text', TRUE); ?></div>
        <?php if ($loop->have_posts()) : ?>
            <div class="blog-lists blog-lists-full-layout">
                <?php $i = 0;
                while ($loop->have_posts()) : $loop->the_post();
                    $featuredID = wp_get_attachment_image_src(get_post_thumbnail_id(), 'single-post-thumbnail');?>
                    <div class="row-fluid">
                        <div class="blog-list-container">
                            <div class="span12 beau-animated fade-in">
                                <div class="full-content">
                                    <div class="row-fluid">
                                        <div class="span1">
                                            <div class="date"><span><?php echo get_the_time('d') ?></span></div>
                                            <div class="month-year">
                                                <span><?php echo substr(get_the_time('F'), 0, 3); ?></span></div>
                                        </div>
                                        <div class="span11">
                                            <a href="<?php echo get_permalink() ?>"><h3><?php the_title(); ?></h3></a>
                                            <div class="content style2">
                                                <?php beau_excerpt(30, true); ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row-fluid">
                                        <a href="<?php echo get_permalink() ?>">
                                            <div class="post-thumb hover-img">
                                                <!--                                                <img src="--><?php //echo $featuredID[0] ?><!--"-->
                                                <!--                                                     alt="--><?php //echo get_the_title(); ?><!--">-->
                                                <?php beau_resizer($featuredID[0], $imgsize['blog2']['w'], $imgsize['blog2']['h'], true, get_the_title()); ?>
                                               
                                               
                                            </div>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php $i++;  endwhile;
                ?>
            </div>
            <?php
            wp_reset_postdata();
        endif;
        ?>
    </section>
    <?php
    $out = ob_get_contents();
    ob_end_clean();
    return $out;
}
add_shortcode('blog_style2', 'beau_blog_style2');
function beau_attendingform($atts, $content = null, $code = '')
{
    ?>
   
 <section class="beau-attending-form">
        <h2 class="attending-title"><?php echo get_post_meta(get_the_ID(), '_page_attending_text', TRUE); ?></h2>
        <div class="attending-content"><?php echo get_post_meta(get_the_ID(), '_page_attending_des', TRUE); ?></div>
        <div class="row-fluid">
            <?php $rand = uniqid();
            ob_start();    ?>
            <script type="text/javascript">
                jQuery(function ($) {
                    $('#contact_<?php echo $rand; ?>').submit(function () {
                        $('#loading_contact_<?php echo $rand; ?>').fadeIn('fast');
                        $('#result_contact_<?php echo $rand; ?>').hide();
                        $.ajax({
                            type: 'POST',
                            url: $(this).attr('action'),
                            data: $(this).serialize(),
                            success: function (data) {
                                $('#result_contact_<?php echo $rand; ?>').html(data);
                                $('#result_contact_<?php echo $rand; ?>').fadeIn('fast');
                                $('#loading_contact_<?php echo $rand; ?>').hide();
                            }
                        })
                        return false;
                    });
                });
            </script>
            <form id="contact_<?php echo $rand; ?>"
                  action="<?php echo BEAU_BASE_URL . '/include/scripts/attending.php'; ?>" method="post"
                  class="span12 attending">
                <!--            <form id="contact_--><?php //echo $rand; ?><!--" action="-->
                <?php //echo BEAU_BASE_URL  .'/include/scripts/contact.php'; ?><!--" method="post">-->
		<input type="text" name="fullname" placeholder="Jak się nazywasz?" class="span12">
                <input type="text" name="email" placeholder="Email" class="span12">
		<input type="text" name="persons_attending" placeholder="Temat" class="span12">
                <textarea class="span12" name="wedding_ceremony" rows="8" placeholder="Tu wpisz wiadomość"></textarea>
               
                <input type="submit" class="btn" name="submit" value="<?php _e('Naciśnij by wysłać', 'beau'); ?>"
                       id="contact-submit"/>
                <input type="hidden" name="send_to" value="<?php echo $send_to; ?>"/>
                <!--                <a href="#" class="btn">I’M ATTENDING</a>-->
                <div id="result_contact_<?php echo $rand; ?>"></div>
                <div id="loading_contact_<?php echo $rand; ?>" style="margin-bottom: 20px; display: none;"></div>
            </form>
        </div>
    </section>
    <?php
    $out = ob_get_contents();
    ob_end_clean();
    return $out;
}
add_shortcode('attendingform', 'beau_attendingform');
function beau_attendingform_style2($atts, $content = null, $code = '')
{
    ?>
    <section class="beau-attending-form style2">
        <h2 class="attending-title"><?php echo get_post_meta(get_the_ID(), '_page_attending_text', TRUE); ?></h2>
        <div class="row-fluid">
            <?php $rand = uniqid();
            ob_start();    ?>
            <script type="text/javascript">
                jQuery(function ($) {
                    $('#contact_<?php echo $rand; ?>').submit(function () {
                        $('#loading_contact_<?php echo $rand; ?>').fadeIn('fast');
                        $('#result_contact_<?php echo $rand; ?>').hide();
                        $.ajax({
                            type: 'POST',
                            url: $(this).attr('action'),
                            data: $(this).serialize(),
                            success: function (data) {
                                $('#result_contact_<?php echo $rand; ?>').html(data);
                                $('#result_contact_<?php echo $rand; ?>').fadeIn('fast');
                                $('#loading_contact_<?php echo $rand; ?>').hide();
                            }
                        })
                        return false;
                    });
                });
            </script>
            <form id="contact_<?php echo $rand; ?>"
                  action="<?php echo BEAU_BASE_URL . '/include/scripts/attending.php'; ?>" method="post"
                  class="span12 attending">
                <div class="f-bg"></div>
                <div class="f-bg-left-top"></div>
                <div class="f-bg-right-top"></div>
                <div class="f-bg-left-bottom"></div>
                <div class="f-bg-right-bottom"></div>
                <input type="text" name="fullname" placeholder="Fullname" class="span12">
                <input type="text" name="email" placeholder="Email" class="span12">
                              
                <input type="submit" class="btn" name="submit" value="<?php _e('Wysyłam wiadomość!', 'beau'); ?>"
                       id="contact-submit"/>
                <input type="hidden" name="send_to" value="<?php echo $send_to; ?>"/>
                <div id="result_contact_<?php echo $rand; ?>"></div>
                <div id="loading_contact_<?php echo $rand; ?>" style="margin-bottom: 20px; display: none;"></div>
            </form>
        </div>
    </section>
    <?php
    $out = ob_get_contents();
    ob_end_clean();
    return $out;
}
add_shortcode('attendingform_style2', 'beau_attendingform_style2');
function beau_button($atts, $content = null)
{
    extract(shortcode_atts(array(
        'icon' => '',
        'size' => 'small',
        'color' => '',
        'url' => '#'
    ), $atts));
    $attstoclass = '';
    if (!empty($icon)) $attstoclass = '<i class="' . $icon . '"></i>';
    $out = '<a class="st-button ' . $size . ' ' . $color . '" href="' . $url . '">' . $attstoclass . strip_tags($content) . '</a>';
    return $out;
}

网站是: nikosis.mzonespace.co.uk

更改您的函数beau_blog(),如下所示,只有最初的几行其他相同

function beau_blog($atts, $content = null, $code = '')
{
    global $imgsize;
    extract(shortcode_atts(array(
        'post_not_in' => '',
        'post_in' => '', //you cannoot use post__not_in and post__in in same query so you have to use either one of that. I've modified it to give priority to post_in so if that argument is empty then only the post_not_in ids are used to exclude those posts.
        'item_number' => '4',
    ), $atts));
    if(empty($post_in)){
        $loop = new WP_Query('&posts_per_page=' . $item_number.'&post__not_in='.$post_not_in);
        }
    else{
        $loop = new WP_Query('&posts_per_page=' . $item_number.'&post__in='.$post_in);
    }
    ?>

.....

现在,当您要显示ids 1,2和3的帖子时,请仅执行以下操作

 echo do_shortcode('[blog item_number=4&post_in=array(1,2,3)]');

请尝试以下代码 - 这是简单的示例

function show_posts_by_id( $atts, $content = null, $code ) {
    $item_number = 4;
    $post_ids    = null;
    extract(
        shortcode_atts(
            array(
                'post_ids'    => '',
                'item_number' => 4,
            ),
            $atts
        ),
        EXTR_IF_EXISTS
    );
    $posts = get_posts(
        array(
            'post_type'      => 'page',
            'post__in'       => explode( ',', $post_ids ),
            'posts_per_page' => (int)$item_number,
            'offset'         => 0,
            'orderby'        => 'post_date',
            'order'          => 'DESC',
        )
    );
    // do what you need
}
add_shortcode( 'show_posts_by_id', 'show_posts_by_id' );

像这样使用简码:[show_posts_by_id post_ids=12,15,2176 item_number=4]