为什么我不能将自定义字段变量传递给Wordpress数组中的'numberposts'变量?

时间:2015-12-08 20:15:59

标签: php wordpress advanced-custom-fields

我在这里疯了 - 我无法弄清楚为什么这不起作用!

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

我能做到这一点没问题:

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

将“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'); ?>

每个请求 - 这是代码的完整页面 - 我试图尽可能地清理它 - 你会毫无疑问地注意到报告部分的奇怪编码,我是从使用Types创建的人那里得到的自定义字段和自定义帖子类型。但是我正在使用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 个答案:

答案 0 :(得分: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推送到页面上。你的第三个例子&#34;应该&#34;工作,但你正在使用一个不同的&#34;字段&#34;在每个示例中都有名称,因此很难判断是否有“showarticleposts&#39;或者&#39;文章 - 帖子显示&#39;是实际的字段名称。

<强>更新

从查看代码开始......我注意到您使用的是setup_postdata( $post );。虽然这是一件很棒的事情,但它会更改您的全局$post变量,因此当您致电get_field()时,它正在使用&#34;更改&$post->ID #34; $post变量,这不是你想要的。您想要当前正在查看的页面的原始$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 );