在循环中$ post为null

时间:2013-10-13 17:41:13

标签: php wordpress

根据WP文件,这不可能发生,但它是:

http://codex.wordpress.org/The_Loop

  

您可以使用以下方式显示有关每个帖子的其他信息   通过访问适当的模板标签或(对于高级用户)   $ post变量,使用当前帖子的信息设置   循环正在运行。

以下所有代码示例都使用$ post来访问当前帖子,但我不能。即使它进入循环,$ post也为null:

$args = array('post_type' => 'carousel_bootstrap', 'posts_per_page' => 10);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
    var_dump($post->ID);//=NULL
    $title=the_title(null,null,false);
    var_dump($title);//string(4) "NOPE"
....
endwhile;
wp_reset_postdata();

因此$ post不应该为null或者var_dump不应该在循环中执行。我需要访问当前帖子的自定义字段,但如果$ post为null则无法执行此操作。使它更混乱;在下一行中,$ title变量设置为当前帖子的标题。

我将如何解决这个问题?使用word press 3.6.1

[UPDATE]

我做了一个名为carousel的自定义帖子,我以为我会用它来包含twitter bootstrap轮播的html代码。在标题中,我正在调用一个函数来检查是否存在具有特定名称的自定义轮播帖子(由页面中的自定义值提供)。

在标题$ post是页面,在函数中检查自定义帖子,$ post为null,退出函数并继续在标题$ post中是自定义帖子(不再是页面)。

在我开始时看起来像这样做是个好主意,但看起来不能这样做。

3 个答案:

答案 0 :(得分:1)

让它做它的功能,但认为代码可以使用一些改进:

(在blankslate上添加)

在/wp-content/themes/blankslate/functions.php

//only using single carousel per page now
$vars=array('carouselHtml'=>'<div style="h'.
  'eight:100px"><h1>Working, no Carousel</h1></div>',
    'css'=>[],
    'js'=>[]
);
function getInits(){
  global $vars;
  return $vars;
}
function setInits() {
  global $post;
  global $vars;
  //see if the page specified a carousel (if value it's the title)
  $carousel = get_post_meta($post->ID, 'carousel', true);
  //collect all custom fields named "js" usually to fix screen
  //re sizing like hiding images or big sliders
  $scriptItems = get_post_meta($post->ID, 'js', false);
  if ($scriptItems) {
    foreach ($scriptItems as $item) {
      $vars["js"][]=$item;
    }
  }
  //load all custom fields of the page named css
  //can be used for specific styles for the page
  $cssItems = get_post_meta($post->ID, 'css', false);
  if ($cssItems) {
    foreach ($cssItems as $item) {
      $vars["css"][]=$item;
    }
  }
  //page has a custom field value named carousel
  //it contains the title of the custom post that
  //should contain the html, css and js
  if ($carousel) {
    //nice and intuitive query, gotta love it
    $args = array('post_type' => 
      'carousel_bootstrap',
      'title' => $carousel,
      'posts_per_page' => 100);
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
      $title=the_title(null,null,false);
      if($title===$carousel){
        //found the custom carousel post, load html
        //and add css js not to the page because css
        //goes in the top and js goes in the bottom
        $tmp=get_post_meta($post->ID, 'js', true);
        if($tmp){$vars["js"][]=$tmp;}
        $tmp=get_post_meta($post->ID, 'css', true);
        if($tmp){$vars["css"][]=$tmp;}
        $vars["carouselHtml"]=$post->post_content;
        break;
      }
    endwhile;
    //$post was the page at the start of the function
    //now it could be the carousel post, not sure
    //if the following is needed but works either
    //with or without it
    wp_reset_postdata();
  }
}

//creating custom post named carousel
add_action('init', 'create_carousel_post_type');
function create_carousel_post_type() {
  register_post_type( 'carousel_bootstrap', array(
      'labels' => array(
          'name' => __('Carousels'),
          'singular_name' => __('Carousel')
      ),
      'public' => false,
      'has_archive' => true,
      'rewrite' => array('slug' => 'carousels'),
      'show_ui' => true,
      'menu_position' => 5,
      'supports' => array('title','editor','author',
        'thumbnail','custom-fields','revisions')
  ));
}

在/wp-content/themes/blankslate/page.php

setInits();
get_header(); 
....
<body <?php body_class(); ?> id="top">

<!-- Carousel
    based on http://getbootstrap.com/2.3.2/examples/carousel.html
    ================================================== -->
    <div id="myCarousel" class="carousel slide">
      <div class="carousel-inner">
<?php 
$vars=getInits();
echo $vars["carouselHtml"];
?>

在/wp-content/themes/blankslate/header.php

$vars=getInits();
//output collected css
foreach ($vars["css"] as $item) {
  echo '<link rel="stylesheet" type="text/css" href="'.$item.'" />';
}

页脚具有相同类型的代码,但适用于JavaScripts。

答案 1 :(得分:1)

在自定义WP_QUERY中获取帖子ID的正确方法是:$loop->post->ID

$args = array('post_type' => 'carousel_bootstrap', 'posts_per_page' => 10); 

$loop = new WP_Query($args); 
global $post;

while ($loop->have_posts()) : $loop->the_post();

       $loop->post->ID; //This is how you get the id $post will not work unless you
       setup_postdata( $loop->post->ID );
       print_r($post->ID); //now works. 

endwhile; 
wp_reset_postdata();

答案 2 :(得分:0)

$args = array('post_type' => 'carousel_bootstrap', 'posts_per_page' => 10);
$loop = new WP_Query($args);
if($loop->have_posts()): while($loop->have_posts()): $loop->the_post();
   //echo the title
   the_title();
   the_content();
   //get the ID
   $postId = get_the_ID();
endwhile;
endif;