$post在循环中为空


$post is null while in the loop

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

http://codex.wordpress.org/The_Loop

您可以使用 适当的模板标签或(对于高级用户)通过访问 $post变量,它是用当前帖子的信息设置的,而 循环正在运行。

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

$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为空,则无法访问。使其更加混乱;在下一行中,$title变量设置为当前帖子的标题。

我将如何解决这个问题?使用文字新闻 3.6.1

[更新]

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

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

当我开始时这样做似乎是一个好主意,但看起来不能这样做。

在自定义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();

让它做它所做的事情,但认为代码可以使用一些改进:

(添加在空白板上)

在/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.'" />';
}

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

$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;