在Wordpress中向相关帖子添加高级自定义字段

时间:2015-01-21 18:25:22

标签: php wordpress

我正在尝试为相关的帖子功能添加自定义字段值,但我目前完全坚持这个:

<div class="relatedposts">  
<h3>Related posts</h3>  
<?php  
$orig_post = $post;  
global $post;  
$tags = wp_get_post_tags($post->ID);  

if ($tags) {  
$tag_ids = array();  
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;  
$args=array(  
'tag__in' => $tag_ids,  
'post__not_in' => array($post->ID),  
'posts_per_page'=>4, // Number of related posts to display.  
'caller_get_posts'=>1  
);  

$my_query = new wp_query( $args );  

while( $my_query->have_posts() ) {  
$my_query->the_post();

?>  

<div class="relatedthumb">  
<a rel="external" href="<? the_permalink()?>">
<?php 
$image = wp_get_attachment_image_src(get_field('image'), 'full');
print_r( $image[0] ); ?>
<?php the_title(); ?>
<img src="<?php echo $image[0]; ?>" />
</a>  
</div>  

<? }  
}  
$post = $orig_post;  
wp_reset_query();  
?>  
</div> 

这主要是因为它是在线找到的代码,它只是将引用放在我似乎缺少的自定义字段的位置。我无法将变量和print_r放在任何地方以查看结果。

3 个答案:

答案 0 :(得分:0)

foreach($ tags as $ individual_tag){         $ meta_values = get_post_meta($ post-&gt; ID,&#39;&#39;);

    if (in_array('VALUE SEARCHING FOR',$meta_values) {
        $tag_ids[] = $individual_tag->term_id;
    } // if
} // foreach

填写适当的空白,这应该正确过滤您的标签。

HTH,

= C =

答案 1 :(得分:0)

使用ACF,您可以将帖子ID传递给get_fieldthe_field个功能:

$image_id = get_field( 'image', $post -> ID );

如果您使用没有设置帖子ID的函数,函数可能会返回其他全局初始化帖子的字段值。

因此,您的附件图片获取器(在相关帖子WP_Query循环内)将类似于

$image = wp_get_attachment_image_src(get_field('image', get_the_ID()), 'full');

确保您的ACF字段类型设置为图像及其所需的变体:ID,图像对象或图像URL。然后,get_field将返回图片的附件ID,图片对象或图片网址。

在您的代码示例中,假设wp_get_attachment_image_src需要附件ID,则返回ID将是正确的选择。

编辑:

我刚刚意识到,ACF图像字段可能无法以WP正常的方式将图像保存到WP附件系统。尝试将图片作为对象返回,并var_dump对象的内容,以查看full尺寸图片网址所在的位置。

我可能错了,wp_get_attachment_image_src可能会正常工作。

答案 2 :(得分:0)

好的,任何可能发现这个有用的人,我都有一个解决方案来显示ACF字段:

        <div class="related grid clear">
            <h2>Related posts</h2>
            <?php
            $orig_post = $post;
            global $post;
            $tags = wp_get_post_tags($post->ID);

            if ($tags) {
                $tag_ids = array();
                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                $args=array(
                    'tag__in' => $tag_ids,
                    'post__not_in' => array($post->ID),
                    'posts_per_page'=>9,
                    'caller_get_posts'=>1
                );

                $my_query = new wp_query( $args );

                if($my_query->have_posts()) {
                    while($my_query->have_posts()) {
                        $my_query->the_post();
                        $image = get_field('hero_image', get_the_ID());
                        $introduction = get_field('introduction');
                        ?>
                        <article class="grid-item" data-permalink="<? the_permalink()?>">
                            <div style="background-image: url(<?php echo $image; ?>);"></div>
                            <h3><?php the_title(); ?></h3>
                            <p><?php echo $introduction; ?></p>
                        </article>
                        <?
                    }
                }
                $post = $orig_post;
                wp_reset_postdata();
                wp_reset_query();
            }
            ?>
        </div>
相关问题