首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何只包含选定的帖子(按id) - Wordpress快捷方式

如何只包含选定的帖子(按id) - Wordpress快捷方式
EN

Stack Overflow用户
提问于 2014-12-31 21:39:53
回答 2查看 341关注 0票数 0

这个快捷代码显示了我主页上的最后X个帖子(从最新到最旧的排序)。我想选择哪些是我想要通过ID显示的。怎么做?

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

以下是输入内容:

代码语言:javascript
运行
复制
if(!empty($blog)){
        echo do_shortcode('[blog item_number=4]'); 
           }

这是这个短码的一个代码。

代码语言:javascript
运行
复制
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

EN

Stack Overflow用户

发布于 2015-01-01 00:08:26

将函数beau_blog()更改为如下所示,只有最初的几行其他代码与原来相同

代码语言:javascript
运行
复制
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);
    }
    ?>

.

现在,当您想要显示it为1、2和3的帖子时,只需执行以下操作

代码语言:javascript
运行
复制
 echo do_shortcode('[blog item_number=4&post_in=array(1,2,3)]');
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27721934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档