这不应该那么难!但是我在谷歌上找不到这个问题的答案。。
我怎样才能从附件模板链接到完整的帖子?
奖励问题!
如何从附件模板上附件所属的帖子中获取自定义字段数据?
我使用它来获取其他模板文件上的数据:
<?php echo get_post_meta($post->ID, \'custom-field\', true); ?>
这不应该那么难!但是我在谷歌上找不到这个问题的答案。。
我怎样才能从附件模板链接到完整的帖子?
奖励问题!
如何从附件模板上附件所属的帖子中获取自定义字段数据?
我使用它来获取其他模板文件上的数据:
<?php echo get_post_meta($post->ID, \'custom-field\', true); ?>
如果附件附加到帖子,则该帖子将被确定为“post\\u parent”。
因此,如果您在attachement模板中(attachment.php、image.php等):
while ( have_posts() ) {
the_post();
// Get parent id of current post (get_the_ID() gets id of current post,
// whicdh is the attachement, as we are in the attachment template)
$parent_id = wp_get_post_parent_id( get_the_ID() );
}
您也可以使用此方法获取父id,但我更喜欢其他方法:while ( have_posts() ) {
the_post();
global $post;
$parent_id = $post->post_parent;
}
拥有父帖子ID后,您可以获得父帖子的链接、自定义字段或任何您想要的内容: if( $parent_id ) {
// parent post has been found, get its permalink
$parent_url = get_permalink( $parent_id );
?>
<a href="<?php echo $parent_url; ?>">
<?php _e(\'Go to parent post\', \'textdomain\'); ?>
<a/>
<?php
echo get_post_meta( $parent_id, \'custom-field\', true );
}
Q: 我怎样才能从附件模板链接到完整的帖子
我想你指的是this:
<a class="back-to-article" href="<?php echo get_permalink($post->post_parent); ?>">
<?php _e(\'Get to Main Article\', \'textdomain\'); ?>
<a/>
问:如何从附件模板上附件所属的帖子中获取自定义字段数据回答,因为这是你的第一个问题。从下次开始,请回答一个问题。
您的代码是正确的。您可以在如下循环中使用代码:
echo get_post_meta(get_the_ID(), \'custom-field\', true);
也可以与global一起使用:global $post;
echo get_post_meta($post->ID, \'custom-field\', true);
请查看github repo上的完整代码以了解更多想法。情况是这样的:我有一个自动脚本,可以上传附件并将每个附件链接到特定的帖子。由于错误,脚本运行了多次,我有以下内容媒体库中针对单个文件的多个附件帖子(不同的附件帖子具有相同的文件URL)。这些附件中有一个实际上是附在帖子上的。我想做的显然是清理媒体库。我需要在不删除文件的情况下删除附件帖子,并且确保我不删除那些实际附加到他们帖子的帖子。有什么想法吗?