我需要获取与带有meta\\u键的帖子相关联的附件(每个帖子只有一个)wpcf-legislacion-gratis
(与列的值无关)通过使用get_posts()
作用我试过两种方法:
$nonActivePlan_args = array(
\'order\' => \'desc\',
\'meta_query\' => array(
array(
\'key\' => \'wpcf-legislacion-gratis\',
\'value\' => NULL,
\'compare\' => \'!=\'
)
),
\'post_status\' => \'inherit\',
\'posts_per_page\' => 1,
\'post_type\' => \'attachment\',
\'post_parent\' => get_the_ID(),
);
$nonActivePlanAttachment = get_posts( $nonActivePlan_args );
if ($nonActivePlanAttachment) {
foreach ($nonActivePlanAttachment as $attachment) { ?>
<div class="legislacion-ico">
<a class="pdf" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
<img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png">
<br>
Descargar PDF
</a>
</div>
<?php
}
}
wp_reset_postdata();
关于上述问题,我有以下疑问:
如何编写查询以查找meta\\u键等于的附件wpcf-legislacion-gratis
不管它有什么价值这种情况下的循环是正确的吗?注意,我只有一个wpcf-legislacion-gratis
每一篇文章,我是否需要对结果进行循环?不应该只有一个吗如果我将两个附件分开(正如你在文章末尾的查询中所看到的),我应该执行wp_reset_postdata()
?我也尝试过此代码:
$nonActivePlan_args = array(
\'order\' => \'desc\',
\'meta_key\' => \'wpcf-legislacion-gratis\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 1,
\'post_type\' => \'attachment\',
\'post_parent\' => get_the_ID(),
);
但这不起作用,意味着我无法从DB获得任何价值。这是我在MySQL命令行上执行的查询结果:
SELECT * FROM `wp_postmeta` where post_id=\'11839\';
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+
| meta_id | post_id | meta_key | meta_value |
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+
| 22149 | 11839 | wpcf-legislacion-gratis | http://jurisprudencia.dev/wp-content/uploads/2015/02/modelodatos.pdf |
| 22150 | 11839 | wpcf-legislacion-pagada | http://jurisprudencia.dev/wp-content/uploads/2015/02/AtencionUsuario_version1.pdf |
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+
我还检查了文档
here 和
here 但对我没有任何帮助。
如何获取附件?有什么帮助或建议吗?