WordPress Get Page By ID返回奇怪的结果

时间:2012-09-25 01:46:11

标签: php wordpress function meta-boxes

我为客户端设置了CMS,其中包含三个相关内容的元框。所有客户要做的就是订购一个页面slug(每个一个),该网站将返回三个相关产品。

一切都工作正常,直到我的客户意外拼错其中一个slu .. WordPress不会返回任何内容,而是返回大约6个随机项。

IN FUNCTIONS.PHP:

function get_ID_by_page_name($page_name) {
global $wpdb;
$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_type = 'page'");
return $page_name_id;

}

在模板文件中:

$goesWith = get_post_meta($post->ID, 'goes_with', true);

if (($goesWith) or ($goesWith2) or ($goesWith3)) {

            echo "<div class='goes-with'>";

            echo  "<h2>Goes Great With:</h2>";                      

            // OPTION ONE

                $pageID = get_ID_by_page_name($goesWith);

                    if ($goesWith) { 

                        $args = array(
                            'post_type'      => 'page',
                            'page_id'     => $pageID,
                            'post_status' => 'publish'
                        );
                        query_posts( $args );

                        while(have_posts()) {

                            the_post(); // vital                                                        

                            echo "<div class='post-product'>";
                            echo "<a href=";
                            the_permalink();
                            echo ">";
                            thumbPath(140, 140);
                            echo "</a><a href=";
                            the_permalink();
                            echo "><p>";
                            the_title();
                            echo "</p></a></div><!--end post-product-->";

                       }



                    }

                    else {

                        echo "";

                    } 


            wp_reset_query();  

1 个答案:

答案 0 :(得分:0)

首先,您不需要任何自定义代码来执行您正在执行的操作:

$goes_with = get_post_meta(get_the_ID(), 'goes_with'); //Returns array when 3rd parameter is left empty or false
foreach($goes_with as $slug){
    $args = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
        'pagename'    => $slug
    );
    query_posts( $args ); //Bad way
    while(have_posts()){ the_post(); //Bad way

    //$query = new WP_Query($args); //Good way
    //while($query->have_posts()){ $query->the_post(); //Good way                                                         

        echo "<div class='post-product'>";
        echo "<a href='".get_permalink()."'>";
        thumbPath(140, 140);
        echo "</a><a href='".get_permalink()."'><p>".get_the_title()."</p></a></div><!--end post-product-->";
    } 
    wp_reset_query(); //Bad way
    //wp_reset_postdata(); //Good way
}

其次,你绝对应该避免使用query_posts()。由于您的主题已经发送到您的客户端,可能已经太晚了,但如果将额外的时间放入主题并不是问题,您应该考虑将所有内容从query_posts切换到WP Query

使用WP_Query可以显着提高性能,并且它不会改变任何Wordpress Globals。

如果你想看看它是如何实现的,可以随意注释掉标有“Bad way”的所有行,并取消注释标记为“Good way”的所有行。

此外,使用上面的代码,您可以在自定义元字段中重复使用相同的“go_with”键,以将所需的其他页面链接到给定的页面查询。由于您通过pagename而不是ID运行查询,因此必须在循环内进行此特定方法。

然而,对您的原始优势是,您依靠Wordpress的内置功能为您进行查询,而不是额外调用数据库以返回您已经可用的结果你。

此代码未经测试,可能需要进行一些重构才能使其与您的设置一起使用,但它应该让您入门。

希望这会帮助你,祝你好运。