2015-12-08 29 views
0

我在这里疯了 - 我找不出为什么这不起作用!为什么我无法将自定义字段变量传递给Wordpress数组中的'numberposts'变量?

我有一个wordpress模板与多个查询来显示不同的帖子类型。

我能够做到这一点没有问题:

$showfeatposts = "1"; 
$args = array(
'numberposts'  => $showfeatposts, 
'orderby'   => 'post_date', 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

其中passses的 “1” 的值到 'numberposts' 变量。

我需要做的是将值“1”替换为从管理面板中的自定义字段传递的值,以便管理员可以输入他们想要显示的帖子数。

当我改变我的代码,而不是这样:

$showarticleposts = the_field('articles-posts-to-show'); 
$args = array(
'numberposts'  => $showarticleposts, 
'orderby'   => "post_date", 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '5, -58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

我得到的所有帖子恢复,似乎正在进入的值是“-1”

我已经验证,在实际的价值通过在页面上声明自定义字段为'2'。

我在做什么错?当然这应该是可能的?

仅供参考:Here's the page

ETA:我也试着从ACF教程这种方法 - 仍然不能正常工作:

$args = array(
'numberposts'  => get_field('showarticleposts'), 
'orderby'   => "post_date", 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '5, -58', 
'post_status'  => 'publish', 
'suppress_filters' => true 

ETA2:针对下面的答案 - 我也试过这个选项:

$showarticleposts = get_field('showarticleposts'); 

$args = array(
'numberposts'  => $showarticleposts, 
'orderby'   => "post_date", 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '5, -58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

如果我把这个代码在页面上 - 它呼应了数字“2”:

<?php echo get_field('showarticleposts'); ?> 

每请求 - 他re是代码的完整页面 - 我试图尽可能清理它 - 您将毫无疑问地注意到报告部分的怪异编码,我从使用类型来创建自定义字段和自定义文章类型。但是我使用的ACF的自定义字段我尝试添加到选择的每一类型后的显示在主页上的能力:

section id="content" role="main" class="clearfix animated"> 

    <?php 
    /** 
    * If Featured Image is uploaded set it as a background 
    * and change page title color to white 
    **/ 
    if (has_post_thumbnail()) { 
     $page_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'big-size'); 
     $page_bg_image = 'style="background-image:url(' . $page_image_url[0] . ');"'; 
     $title_with_bg = 'title-with-bg'; 
    } else { 
     $title_with_bg = 'wrapper title-with-sep'; 
    } ?> 

    <!--<header class="entry-header page-header"> 
    <div class="page-title <?php echo isset($title_with_bg) ? $title_with_bg : ''; ?>" <?php echo isset($page_bg_image) ? $page_bg_image : ''; ?>> 
      <div class="wrapper"> 
       <h1 class="entry-title"><?php the_title(); ?></h1> 
      </div> 
     </div> 
    </header>--> 

    <div class="wrapper"> 
    <div class="grids"> 
      <div class="grid-8 column-1"> 
     <?php 
     // Enable/Disable sidebar based on the field selection 
     if (! get_field('page_sidebar') || get_field('page_sidebar') == 'page_sidebar_on'): 
     ?> 

     <?php endif; ?> 

      <?php 
      if (have_posts()) : while (have_posts()) : the_post(); 
      ?> 
      <div class="page-content"> 
      <?php the_content(); ?> 
      </div> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

<?php $showfeatposts = "1"; 
$args = array(
'numberposts'  => $showfeatposts, 
'orderby'   => 'post_date', 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$featposts = get_posts($args); 

foreach($featposts as $post) : //setup_postdata($ppost); 
setup_postdata($post); ?> 

    <div class="col-md-12 recent feat" id="recent"> 
    <figure class="entry-image inview"> 
     <?php if (has_post_thumbnail()) { ?> 
     <a href="<?php the_permalink(); ?>"> 
     <?php the_post_thumbnail('rectangle-size-large'); ?></a> 
    <?php } ?> 
</figure> 
     <header class="entry-header"> 
     <div class="entry-category"> 
      <?php the_author_posts_link(); ?>/<?php the_time('F jS, Y'); ?> </div> 
      <h2 class="entry-title" itemprop="headline"> 
      <a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a> 
      </h2> 
     </header> 
<div class="entry-content"> 
    <?php echo apply_filters("the_content", $post->the_excerpt) ; ?> 
</div> 
<div class="entry-category"> 
<span class="posted-on">By 
<span class="author vcard"><?php the_author_posts_link(); ?></span></span> 
</div> 
</div> 
<?php 
$pnum++; 
endforeach; ?> 
    </article> 

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    <div class="col-md-12 homehdr"> 
    <header class="section-header"> 
    <div class="title-with-sep"> 
    <h2 class="title">Recent Articles</h2> 
    </div> 
    </header> 
    </div> 

    <?php $showarticleposts = "4"; 

$args = array(
'numberposts'  => $showarticleposts, 
'orderby'   => "post_date", 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '5, -58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$artposts = get_posts($args); 

foreach($artposts as $post) : //setup_postdata($ppost); 
setup_postdata($post); ?>     

     <div class="col-md-6 recent arts" id="arts"> 
      <figure class="entry-image inview"> 
       <?php if (has_post_thumbnail()) { ?> 
        <a href="<?php the_permalink(); ?>"> 
         <?php the_post_thumbnail('rectangle-size'); ?> 
        </a> 
      <?php } ?></figure> 
      <header class="entry-header"> 
     <div class="entry-category"> 
     <?php the_author_posts_link(); ?>/<?php the_time('F jS, Y'); ?></div> 
     <h2 class="entry-title" itemprop="headline"> 
      <a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a> 
     </h2> 
    </header> 
    <div class="entry-content"> 
<?php if ($post->post_excerpt) the_excerpt(); else { ?> 
<?php $content = apply_filters("the_content", $post->post_content); 
$content = strip_tags($content); 
echo substr($content, 0, 100); } 
    ?></div> 
<div class="entry-category"> 
<span class="posted-on">By 
<span class="author vcard"><?php the_author_posts_link(); ?></span></span> 
</div> 
</div> 
<?php 
$pnum++; 
endforeach; ?> 
</article> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    <div class="col-md-12 homehdr"> 
    <header class="section-header"> 
    <div class="title-with-sep"> 
    <h2 class="title">Recent Videos</h2> 
    </div> 
     </header> 
     </div> 
     <?php $showvideoposts = "2"; 

$args = array(
'numberposts'  => $showvideoposts, 
'orderby'   => 'post_date', 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '3, -58', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$vidposts = get_posts($args); 


foreach($vidposts as $post) : //setup_postdata($ppost); 
setup_postdata($post); ?> 

        <div class="col-md-6 recent vids"> 
    <?php $video_embed = wp_oembed_get(get_post_meta($post->ID, 'add_video_url', true)); echo '<figure class="video-wrapper">' .$video_embed. '</figure>'; ?> 

     <header class="entry-header"> 
     <div class="entry-category"> 
      <?php the_author_posts_link(); ?>/<?php the_time('F jS, Y'); ?> </div> 
     <h2 class="entry-title" itemprop="headline"> 
      <a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a> 
     </h2> 
    </header> 
    <div class="entry-content"> 

<?php echo $post->the_content; ?> 
</div> 
    <div class="entry-category"> 
<span class="posted-on">By 
<span class="author vcard"><?php the_author_posts_link(); ?></span></span> 
</div> 
</div> 
<?php 
$pnum++; 
endforeach; ?> 
       </article> 


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
<div class="col-md-12 homehdr"> 
    <header class="section-header"> 
    <div class="title-with-sep"> 
     <h2 class="title">Recent Notes</h2> 
    </div> 
     </header> 
     </div> 
       <?php 
       $shownoteposts = "2"; 
$args = array(
'numberposts'  => $shownoteposts, 
'orderby'   => 'post_date', 
'order'   => 'DESC', 
'post_type'  => 'post', 
'cat'    => '42', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$noteposts = get_posts($args); 

foreach($noteposts as $post) : //setup_postdata($ppost); 
setup_postdata($post); ?> 

     <div class="col-md-6 recent notes"> 

     <header class="entry-header"> 
     <div class="entry-category"> 
     <?php the_author_posts_link(); ?>/<?php the_time('F jS, Y'); ?> 

</div> 
    <h2 class="entry-title" itemprop="headline"> 
    <a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a> 
    </h2> 
    </header> 
    <div class="entry-content"> 

<?php echo apply_filters("the_excerpt", $post->the_excerpt) ; ?> 
</div> 
    <div class="entry-category"> 
<span class="posted-on">By 
<span class="author vcard"><?php the_author_posts_link(); ?></span></span> 
</div> 
</div> 
<?php 
$pnum++; 
endforeach; ?> 
       </article> 


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
       <div class="col-md-12 homehdr"> 
       <header class="section-header"> 
     <div class="title-with-sep"> 
     <h2 class="title">Recent Reports</h2> 
    </div> 
     </header> 
     </div> 
       <?php 
       $showreports = get_field('showreports'); 

$args = array(
'numberposts'  => $showreports, 
'orderby'   => 'post_date', 
'order'   => 'DESC', 
'post_type'  => 'reports', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$reptposts = get_posts($args); 

foreach($reptposts as $post) : //setup_postdata($ppost); 
setup_postdata($post); ?> 

    <div class="col-md-6 recent rpts"> 
    <h2 class="report-title" itemprop="headline"> 
    <a href="<?php echo get_permalink($post->ID); ?>"> 
    <?php echo(types_render_field("first-name", array('raw' => true))); echo(" "); 
     echo(types_render_field("last-name", array('raw' => true))); ?></a> 
      </h2> 
     <p><strong>Posted by:</strong> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" class="url fn n" rel="author" itemprop="url"> 
    <span itemprop="name"><?php the_author_meta('display_name'); ?> 
</span></a> on <?php 
$publish_date = '<time class="entry-date updated" datetime="' . get_the_time('c') . '" itemprop="datePublished">' . get_the_time(get_option('date_format')) . '</time>'; echo $publish_date; ?><br /> 
    <strong>Dates Seen:</strong> 
<?php,$dates_seen = types_render_field("dates-seen", array('raw' => true)); echo($dates_seen); ?> 
<br /> 
<strong>Affiliate:</strong> 
<?php echo(types_render_field("milb", array('raw' => true))); 
<br /> 
    <strong>MLB Team</strong> 
    <?php echo(types_render_field("mlb-club", array('raw' => true))); ?> 
    </p> 
    </div> 
<?php 
$pnum++; 
endforeach; ?> 
    </article> 

<?php endwhile; endif; ?>  

     <?php 
      // Enable/Disable comments 
      if ($ti_option['site_page_comments'] == 1) { 
       comments_template(); 
      } ?> 

      <?php 
      // Enable/Disable sidebar based on the field selection 
      if (! get_field('page_sidebar') || get_field('page_sidebar') == 'page_sidebar_on'): ?> 

</div> 
      <?php get_sidebar(); ?> 

     </div><!-- .grids --> 
     <?php endif; ?> 

    </div> 
</section><!-- #content --> 

<?php get_footer(); ?> 

回答

1

the_field(),顾名思义,回音必出的值场到页面上,不要将其存放在变量中......而是要做到这一点:

$showarticleposts = get_field('articles-posts-to-show'); 
$args = array(
    'numberposts'  => $showarticleposts, 
    'orderby'   => "post_date", 
    'order'   => 'DESC', 
    'post_type'  => 'post', 
    'cat'    => '5, -58', 
    'post_status'  => 'publish', 
    'suppress_filters' => true 
); 

要确保它得到你想要的东西,这样做:var_dump($showarticleposts);,看看2,如你要求它应该是,正在被甩在页面上。你的第三个例子“应该”起作用,但是你在每个例子中都使用了不同的“字段”名称,所以很难判断'showarticleposts'或'articles-posts-to-show'是否是实际的字段名称。

UPDATE

因为看你的代码......我注意到,您正在使用setup_postdata($post);。尽管这是一件好事,但它会改变您的全局变量$post,因此当您拨打get_field()时,它正在使用“已更改”$post变量的$post->ID,这不是您想要的。您需要当前正在查看的页面的原始$post->ID。因此,在创建每个自定义循环后,只需添加wp_reset_postdata();即可解决问题。

更新2 既然你提到它仍然没有工作,我能想到的唯一的另一件事是给一个变量在页面的极顶设置为原始页面ID ...

$current_page_id = get_the_ID(); 

然后当你调用get_field(),包括ID:

$showarticleposts = get_field('articles-posts-to-show', $current_page_id); 
+0

谢谢 - 为混乱的字段名不好意思 - - 我其实想这也是我塞弗在页面上的查询,我已经尝试了不同的排列。这也给我所有的帖子结果。这很奇怪,因为它似乎确实应该起作用 - 但事实并非如此。我想也许有一些技巧。 :) – Haikukitty

+0

实际上,当我添加var_sump代码时,我得到这个页面“bool(false)” - 我认为这可能是一个问题。但为什么会是var_dump($ showarticleposts)的结果;如果该字段回显“2”?任何想法? – Haikukitty

+0

您正在获取“bool(false)”,因为如果函数找不到您要查找的值,该函数将返回false。这表明事情失败的原因。根据文档,您可能需要将$ post-> ID传递给它。 '$ field = get_field($ field_name,$ post_id,$ format_value);'。查看你的整个代码页面来查看真正发生的事情会很有帮助。 –