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

Php 向Wordpress中的相关帖子添加高级自定义字段,php,wordpress,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

我正试图为相关帖子功能添加自定义字段值,但目前我完全无法做到这一点:

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

相关职位
这主要是因为它是在网上找到的代码,它只是在哪里放置对我似乎丢失的自定义字段的引用。我无法将变量放置在任何地方并打印出来以查看结果。

foreach($tags as$individual\u tag){ $meta_values=get_post_meta($post->ID',)

填写适当的空格,这将正确过滤您的标签

嗯,


=C=

使用ACF,您可以将帖子ID传递给
get\u字段
the\u字段
函数:

$image_id = get_field( 'image', $post -> ID );
如果使用没有设置post ID的函数,这些函数可能会返回某些其他全局初始化post的字段值

因此,您的附件图像获取程序(在相关帖子
WP\u查询
循环中)应该是这样的

$image = wp_get_attachment_image_src(get_field('image', get_the_ID()), 'full');
请确保您的ACF字段类型设置为image,以及所需的变量:ID、image object或image URL。
get\u field
随后将返回图像的附件ID、图像对象或图像URL

在您的代码示例中,如果
wp\u get\u attachment\u image\u src
需要附件ID,则返回ID是正确的选择

编辑:

我刚刚意识到,ACF图像字段可能没有以WP正常的方式将图像保存到WP附件系统。请尝试将图像作为对象返回,然后
var_dump
对象的内容,以查看
完整大小的图像URL在哪里


我可能是错的,
wp\u get\u attachment\u image\u src
可能会正常工作。

好的,任何人如果觉得这很有用,我有一个解决方案来显示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>

相关职位

只是为了确保我理解。您是否试图通过标记筛选相关帖子?@CalEvans-是的,通过标记筛选相关帖子,然后获取相关自定义字段。您是在询问高级自定义字段(插件)还是高级自定义字段(实现)?@ojrask-抱歉,是的。我想知道如何使用ACF插件。谢谢,但我正试图使用与帖子相关联的ACF图像字段,标签过滤工作正常;我在输出中得到一个超链接,但没有图像。这对我不起作用。两种方法似乎都无法访问ACF字段中的帖子图像。这有什么作用
var\u dump
输出
get\u字段('image',get\u ID())
?并且您确定存在一个名为
image
的字段,并且其中保存了一个图像或某个内容以供输出帖子?从我看到的情况来看,是的。它被称为'hero\u image',因此这应该输出一些内容:while($my\u query->have\u posts()){$my_query->the_post();$image=wp_get_attachment_image_src(get_字段('hero_image',get_ID()),'full');var_dump($image);?>是的,在这种情况下,
get_字段('hero_image',$ID)
将正确获取值。
        <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>