0

根据 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 不应该为空,要么 var_dump 不应该在循环中执行。我需要访问此当前帖子的自定义字段,但如果 $post 为空,则无法访问。使其更加混乱;在下一行中,$title 变量设置为当前帖子的标题。

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

[更新]

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

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

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

4

3 回答 3

1

得到它做它做的事,但认为代码可以使用一些改进:

(添加在空白板上)

在 /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。

于 2013-10-13T19:14:59.373 回答
1

The right way to get the post id in a custom WP_QUERY is: $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();
于 2013-10-14T18:33:29.447 回答
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;
于 2013-10-13T17:57:58.843 回答