微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用我的代码在数组中查询

我使用滑块为我的wordpress特色文章.它选择一个自定义类别并返回一定数量的帖子.

如何将显示的第一个帖子设为自定义帖子?我可以直接在滑块代码添加特定帖子的ID以使该帖子首先显示,然后是原始查询返回的其他帖子吗?

例如,在页面上,第一个帖子是ID 6(手动编写在源代码中),第二个,第三个和第四个帖子是原始代码查询返回的帖子.这怎么可能?

有人建议这很简单,我需要做的就是在此之前再做一个查询获取自定义帖子,然后关闭查询,重新打开它以获取一个查询(即滑块代码中)并最终推送将项目放入一个数组中.我对PHP的知识非常有限,这使我无法理解如何做到这一点.

我知道如何获得自定义帖子,此代码有效:

<?PHP
$post_id = 6;
$queried_post = get_post($post_id);
?>

但是,我不知道如何将此和原始查询添加到数组中.你呢?

以下是滑块的完整代码

<?PHP 
    $responsive = 'on' != get_option('henrik_responsive_layout') ? false : true;
    $featured_auto_class = '';
    if ( 'on' == get_option('henrik_slider_auto') ) $featured_auto_class .= ' et_slider_auto et_slider_speed_' . get_option('henrik_slider_autospeed');
?>
<div id="featured" class="<?PHP if ( $responsive ) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>">
    <a id="left-arrow" href="#"><?PHP esc_html_e('PrevIoUs','henrik'); ?></a>
    <a id="right-arrow" href="#"><?PHP esc_html_e('Next','henrik'); ?></a>

<?PHP if ( $responsive ) { ?>
    <ul class="slides">
<?PHP } else { ?>
    <div id="slides">
<?PHP } ?>
        <?PHP global $ids;
        $ids = array();
        $arr = array();
        $i=0;

        $featured_cat = get_option('henrik_feat_cat'); 
        $featured_num = (int) get_option('henrik_featured_num'); 

        if (get_option('henrik_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
        else {
            global $pages_number;

            if (get_option('henrik_feat_pages') <> '') $featured_num = count(get_option('henrik_feat_pages'));
            else $featured_num = $pages_number;

            query_posts(array
                            ('post_type' => 'page','orderby' => 'menu_order','order' => 'ASC','post__in' => (array) get_option('henrik_feat_pages'),'showposts' => (int) $featured_num
                        ));
        } ?>
        <?PHP if (have_posts()) : while (have_posts()) : the_post();
        global $post; ?>
        <?PHP if ( $responsive ) { ?>
            <li class="slide">
        <?PHP } else { ?>
            <div class="slide">
        <?PHP } ?>
                <?PHP
                $width = $responsive ? 960 : 958;
                $height = 340;
                $small_width = 95;
                $small_height = 54;
                $titletext = get_the_title();

                $thumbnail = get_thumbnail($width,$height,'',$titletext,false,'Featured');

                $arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'Small');
                $arr[$i]['titletext'] = $titletext;

                $thumb = $thumbnail["thumb"];
                print_thumbnail($thumb,$thumbnail["use_timthumb"],$width,''); ?>
                <div class="featured-top-shadow"></div>
                <div class="featured-bottom-shadow"></div>  
                <div class="featured-description">
                    <div class="feat_desc">
                        <p class="meta-info"><?PHP esc_html_e('Posted','henrik'); ?> <?PHP esc_html_e('by','henrik'); ?> <?PHP the_author_posts_link(); ?> <?PHP esc_html_e('on','henrik'); ?> <?PHP the_time(esc_attr(get_option('henrik_date_format'))) ?></p>
                        <h2 class="featured-title"><a href="<?PHP the_permalink(); ?>"><?PHP the_title(); ?></a></h2>
                        <p><?PHP truncate_post(410); ?></p>
                    </div>
                    <a href="<?PHP the_permalink(); ?>" class="readmore"><?PHP esc_html_e('Read More','henrik'); ?></a>
                </div> <!-- end .description -->
        <?PHP if ( $responsive ) { ?>
            </li> <!-- end .slide -->
        <?PHP } else { ?>
            </div> <!-- end .slide -->
        <?PHP } ?>
        <?PHP $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?PHP if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?PHP } else { ?>
    </div> <!-- end #slides -->
<?PHP } ?>
</div> <!-- end #featured -->

<div id="controllers" class="clearfix">
    <ul>
        <?PHP for ($i = 0; $i < $featured_num; $i++) { ?>
            <li>
                <div class="controller">
                    <a href="#"<?PHP if ( $i == 0 ) echo ' class="active"'; ?>>
                        <?PHP print_thumbnail( $arr[$i]['thumbnail']['thumb'],$arr[$i]['thumbnail']["use_timthumb"],$arr[$i]['titletext'],$small_width,$small_height ); ?>
                        <span class="overlay"></span>
                    </a>
                </div>  
            </li>
        <?PHP } ?>
    </ul>
    <div id="active_item"></div>
</div> <!-- end #controllers -->

如果您选择回复,请详细说明代码示例,谢谢.

解决方法

不确定我的解决方案因为我没有WP主机来测试它,但是rty类似的东西:

第39行:用下面这一个代码替换该行

<?PHP if (have_posts()) : while (have_posts()) : 
    global $post;
    if (!$first_time)  
    {
        $post_id = 6;
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post();
     ?>

解决方案想法很简单:

Check for first-time loop
   :firt loop - simply get needed post with "get_post()" function
   :other loops - get posts from original query by "the_post()" function.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐