Php 如何根据引用的当前帖子来提取帖子对象?
我为产品视频设置了一个名为“tv”的自定义发布类型,并在其中添加了一个自定义字段,该字段将产品称为发布对象 我现在想对其进行反向工程,这样,如果在该字段中引用了产品,就会在产品页面上显示一个指向正确视频的链接。这就是我到目前为止所做的:Php 如何根据引用的当前帖子来提取帖子对象?,php,wordpress,foreach,advanced-custom-fields,custom-post-type,Php,Wordpress,Foreach,Advanced Custom Fields,Custom Post Type,我为产品视频设置了一个名为“tv”的自定义发布类型,并在其中添加了一个自定义字段,该字段将产品称为发布对象 我现在想对其进行反向工程,这样,如果在该字段中引用了产品,就会在产品页面上显示一个指向正确视频的链接。这就是我到目前为止所做的: <?php $thisinstrument = get_post($post->ID,$output = OBJECT,$filter = 'raw'); $posts = get_posts(array( 'nu
<?php
$thisinstrument = get_post($post->ID,$output = OBJECT,$filter = 'raw');
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'tv',
'meta_key' => 'instruments',
'meta_value' => $thisinstrument
));
if( $posts ): ?>
<!-- TV -->
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
-
我想我说得对。首先,我将$thisinstrument设置为get_post($post='null',$output=OBJECT,$filter='raw');那只是打印了所有的帖子
我哪里出错了?我假设“'meta_key'=>'instruments'”应该包含当前帖子的ID,因此当您查询$thisinstrument时,应该使用$post->ID。
我相信下面的代码应该可以帮助您完成这项工作
<?php
// $thisinstrument =.... No need for this line if the 'instruments' field is the ID of the current post, we can just insert that inside the get_posts() below.
GLOBAL $post;
$posts = get_posts(array(
'numberposts' => 1,
'post_type' => 'tv',
'meta_query' => array(
array(
'key' => 'instruments',
'value' => $post->ID,
)
)
));
if( $posts ): ?>
<!-- TV -->
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink($post->ID); ?>"><?php the_title($post->ID); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
-
我相信这应该可以做到,试试看,让我知道我可能看错了你。我假设“'meta_key'=>'instruments'”应该包含当前帖子的ID,所以当你查询$thisinstrument时,你应该使用$post->ID。
我相信下面的代码应该可以帮助您完成这项工作
<?php
// $thisinstrument =.... No need for this line if the 'instruments' field is the ID of the current post, we can just insert that inside the get_posts() below.
GLOBAL $post;
$posts = get_posts(array(
'numberposts' => 1,
'post_type' => 'tv',
'meta_query' => array(
array(
'key' => 'instruments',
'value' => $post->ID,
)
)
));
if( $posts ): ?>
<!-- TV -->
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink($post->ID); ?>"><?php the_title($post->ID); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
-
我相信这应该可以做到,试试看,让我知道我可能看错了你。好的,我只是测试了一下,运气不好(抱歉耽搁了)-有什么想法吗?meta key是自定义字段的ID,需要与循环中的“当前”post ID匹配,这是正确的。#Coopero我已经更新了代码,请重试。谢谢-但运气不好,我不得不删除
数组(
因为它导致写了两次错误,现在它会打印所有电视类型的帖子。当我将帖子数量更改为1时,它只打印最近的一篇。谢谢你的帮助-你觉得怎么样?我再次更新了代码。除非你有问题,否则代码应该会得到你想要的。请问你是否可以通过我的电子邮件与我联系?是a b讨论这个问题很肤浅,这里有一些灰色区域。请检查我的个人资料。好的,我只是测试了它,但没有运气(很抱歉延迟)-有什么想法吗?你是对的,meta key是需要匹配循环中“当前”帖子ID的自定义字段ID。#Coopero我已经更新了代码,请再试一次。谢谢-但运气不好,我不得不删除数组(
因为它导致写了两次错误,现在它会打印所有电视类型的帖子。当我将帖子数量更改为1时,它只打印最近的一篇。谢谢你的帮助-你觉得怎么样?我再次更新了代码。除非你有问题,否则代码应该会得到你想要的。请问你是否可以通过我的电子邮件与我联系?是a b讨论这个问题很肤浅,这里有一些灰色地带。请查看我的个人资料。