dongwenhui8900
dongwenhui8900
2013-07-16 16:50
采纳率: 0%
浏览 53
已采纳

PHP高级自定义字段

This may be a basic question but i cant seem to find a correct solution.

In advanced custom fields I have set up a Field Group CD, in CD there are three fields, title, info, author and the group shows if the category = CD

Therefore when i make a new post with category CD I fill these three fields. There are 10 Posts in the CD categories.

Now the issue I am having is displaying all the posts on a page.

Here is the code I tried

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

       <?php query_posts( array(      'posts_per_page' => -1,      'cat' => '6',     'CD' => ( get_query_var('CD') ? get_query_var('CD') : 1 ), ));

            if (have_posts()) {
               while (have_posts()) {
                  the_post();
            get_post_meta();
                } // end while
                } // end if
            ?>


            <?php endwhile; endif; ?>

This returned an error Warning: Missing argument 1 for get_post_meta(), called in /Volumes/shared/Digital/_Websites/londonconchord/wp-content/themes/conchord/t-disc.php on line 25 and defined in /Volumes/shared/Digital/_Websites/londonconchord/wp-includes/post.php on line 1792

I tried a different attempt

<p><?php query_posts( 'cat=6' ); 
      the_field('title');
      the_field('info');
      the_field('author'); ?></p>

I had more luck here as I was printing some information, however only the 1st post in the category and it kept repeating, i wanted all 10 posts and no repeat.

I think im close just looking for those final pointers

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongya5893
    dongya5893 2013-07-19 12:00
    已采纳

    My solution (Finally,) Hope this can help others

     <?php
    
               $args = array('cat' => 6);
               $category_posts = new WP_Query($args);
    
               if($category_posts->have_posts()) : 
                  while($category_posts->have_posts()) : 
                     $category_posts->the_post();
            ?>
                    <div class="article">
                    <div class="articleinfo">
                    <h3><?php the_field('title'); ?></h3>
                    <?php the_field('author'); ?>
                    <?php the_field('label'); ?>
                        </div>
               <img src="<?php the_field('cd_image'); ?>" height="200" width="200" alt="cd-image" />
    
                </div>
    
            <?php
                  endwhile;
               else: 
            ?>
    
                  Oops, there are no posts.
    
            <?php
               endif;
            ?>
    

    Loops through all posts and pulls the ACF i needed

    点赞 评论
  • dongsi2317
    dongsi2317 2013-07-16 17:04

    Seems get_post_meta() has variables and your are missing it.

    Like get_post_meta($var) expected but your are just calling get_post_meta(). Hope it helps.

    点赞 评论
  • dpw30157
    dpw30157 2013-07-16 18:09

    You're not querying properly I don't think.

    You need to grab the field

    get_field('YOUR FIELD NAME') );
    

    Then loop through and grab what you need as a subfield.

    the_sub_field('YOUR SUB FIELD');
    

    Example

        <?php if(get_field('YOUR FIELD NAME')): while(has_sub_field('YOUR FIELD NAME')): ?>
            <img class="logo" src="<?php the_sub_field('logo'); ?>" />
            <h1><?php the_sub_field('title'); ?></h1> 
        <?php endwhile; endif; ?>
    

    For an example. Hope this helps... let me know.

    点赞 评论
  • douhe1864
    douhe1864 2013-07-17 10:41

    I couldn't get ether of the solutions above to work, thank you though guys for your input,

    here is my solution so far

    <h3><?php the_field('title', 475); ?></h3>
                        <?php the_field('info', 475); ?>
                        <?php the_field('author', 475); ?>
                            </div>
                   <img src="<?php the_field('cd_image', 475); ?>" height="200" width="200" alt="" />
    

    Then I just repeated this and changed the id from 475 to others, now ive pulled in all the posts however the downfall is that any new posts i have to add in this code again.

    Can i use a wp query to pull these 4 fields in a variable and then print that variable, then loop through the category until all posts are printed?

    点赞 评论

相关推荐