簡體   English   中英

將高級自定義字段添加到Wordpress中的相關帖子

[英]Adding Advanced Custom Fields to Related Posts in 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放在任何地方以查看結果。

foreach($ tags作為$ individual_tag){$ meta_values = get_post_meta($ post-> ID,'');

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

填寫適當的空格,這樣可以正確過濾標簽。

HTH,

= C =

使用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字段類型設置為image及其所需的變體:ID,圖像對象或圖像URL。 然后, get_field將返回圖像的附件ID,圖像對象或圖像URL。

在您的代碼示例中,假設wp_get_attachment_image_src需要附件ID,則返回ID是正確的選擇。

編輯:

我剛剛意識到,ACF圖像字段可能不會以正常的方式將圖像保存到WP附件系統中。 嘗試將圖像作為一個對象返回,並var_dump該對象的內容,以查看full尺寸圖像URL所在的位置。

我可能是錯的,並且wp_get_attachment_image_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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM