【问题标题】:Show A Custom Field from Similar Category (Getting A Custom Field from Similar Posts)显示来自相似类别的自定义字段(从相似帖子中获取自定义字段)
【发布时间】:2012-12-21 16:57:09
【问题描述】:

我有一个单一的.php,我在其中显示一个产品。我有此产品的类别和子类别,我想在底部显示来自类似类别的自定义字段图像。

例如,我在 Red Bag 1 中,我想在底部放置一个 缩略图 类似页面,例如 Red Bag 2Red Bag 3 属于同一类别“Red Bag”

我实际上已经有一个代码,但我不知道如何定位由 Advance Custom Field 插件创建的自定义字段:the_field("product_image")

这是我的代码:

              <?php
            global $post;
            $cat_ID=array();
            $categories = get_the_category(); //get all categories for this post
              foreach($categories as $category) {
                array_push($cat_ID,$category->cat_ID);
              }
              $args = array(
              'orderby' => 'date',
              'order' => 'DESC',
              'post_type' => 'post',
              'numberposts' => 8,
              'post__not_in' => array($post->ID),
              'category__in' => $cat_ID
              ); // post__not_in will exclude the post we are displaying
                $cat_posts = get_posts($args);
                $out='';


                  foreach($cat_posts as $cat_post) {
                      $out .= '<li>';
                      $out .=  '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>';
                  }
                $out = '<ul class="cat_post">' . $out . '</ul>';
                echo $out;
            ?>

.wptexturize($cat_post-&gt;post_title). 假设是图像而不是标题。图片来源应取自自定义字段the_field("product_image")

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    如果您使用post_thumbnail_html,那就简单多了。相反,您使用一个不是默认为 wordpress 的函数:the_field
    我不知道哪个插件可以做到这一点。

    可能该函数 (the_field) 可以在普通的 loop 中工作。
    为此,您应该将get_posts($args) 更改为WP_Query($args) 并将foreach($cat_posts.. 替换为循环。

    我建议使用带有post_thumbnail_html的默认wordpress缩略图/特色图片

    【讨论】:

    • 这是有道理的。我将 the_field 用于高级自定义字段插件,是的,我没有看到将其用于特色图像的意义。谢谢你的建议,我会试试这个。
    • 嗨,如果我使用 post_thumbnail 代替,如何输出? .wptexturize($cat_post-&gt;post_thumb). 不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多