【问题标题】:PHP advanced custom fieldsPHP 高级自定义字段
【发布时间】:2013-07-14 23:38:57
【问题描述】:

这可能是一个基本问题,但我似乎找不到正确的解决方案。

在高级自定义字段中我设置了一个字段组 CD,在 CD 中有三个字段,标题、信息、作者和组显示,如果类别 = CD

因此,当我使用 CD 类别发布新帖子时,我会填写这三个字段。 CD 类别中有 10 个帖子。

现在我遇到的问题是在页面上显示所有帖子。

这是我试过的代码

<?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; ?>

这返回了一个错误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

我尝试了不同的尝试

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

我在这里打印一些信息时比较幸运,但是只有该类别中的第 1 个帖子并且它一直在重复,我想要所有 10 个帖子并且没有重复。

我认为我只是在寻找那些最终的指针

谢谢

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我的解决方案(最后,)希望这可以帮助其他人

     <?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;
            ?>
    

    遍历所有帖子并提取我需要的 ACF

    【讨论】:

      【解决方案2】:

      似乎get_post_meta() 有变量,而您却缺少它。

      就像预期的 get_post_meta($var) 一样,但您只是打电话给 get_post_meta()。希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        我不认为你没有正确查询。

        你需要抢占场地

        get_field('YOUR FIELD NAME') );
        

        然后循环并获取您需要的子字段。

        the_sub_field('YOUR SUB FIELD');
        

        例子

            <?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; ?>
        

        举个例子。希望这会有所帮助......让我知道。

        【讨论】:

          【解决方案4】:

          我无法让上述解决方案发挥作用,但感谢各位的意见,

          这是我目前的解决方案

          <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="" />
          

          然后我只是重复了一遍,并将 id 从 475 更改为其他,现在我拉入了所有帖子,但缺点是我必须再次在此代码中添加任何新帖子。

          我可以使用 wp 查询将这 4 个字段提取到一个变量中,然后打印该变量,然后循环遍历类别直到打印所有帖子?

          【讨论】:

            猜你喜欢
            • 2015-01-21
            • 2013-07-28
            • 2020-05-02
            • 1970-01-01
            • 2017-12-16
            • 2012-01-17
            • 2017-08-28
            • 2018-08-06
            • 2013-11-25
            相关资源
            最近更新 更多