Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using advanced custom fields and I have the following code:

<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>

Where the number of posts = 3, I was wondering if I could turn this into a custom field in the editor so that my client can easily change how many posts per page should display. Now using my 'simple this is how it should work' PHP brain, I thought I could use the following:

<?php $numposts = <?php the_field('number_of_posts', $number_of_posts); ?>; ?>

But it doesn't quite work.

Any tips? Thanks.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It might actually still work, but the syntax is currently wrong (you have a PHP opening tag inside another opening tag). And you can assign the value of the custom field to a variable using the get_field function. What if you try this?

<?php
// assuming your ACF name is 'number_of_posts'
$numposts = get_field('number_of_posts');
$args = array( 'numberposts' => $numposts );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
share|improve this answer
    
I could kiss you, this worked perfectly, thanks! –  Phill Mar 4 '12 at 3:01
1  
The "thanks" is plenty. Have fun! –  Jared Cobb Mar 4 '12 at 3:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.