将固定链接更改为自定义字段的值

时间:2014-07-28 作者:BooBoo

我修改了这段代码以用于一个基本的有用链接库,我在https://stackoverflow.com/questions/8643508/how-to-group-articles-by-tags/8645453#8645453.

<?php
$args = array(
    \'orderby\'       => \'name\',
    \'order\'         => \'ASC\',
    \'hide_empty\'    => 1,
    \'taxonomy\'      => \'useful_link_categories\', 
);
foreach (get_categories($args) as $tax) :
    $args = array(
        \'post_type\'         => \'useful-links\',
        \'posts_per_page\'    => -1,
        \'orderby\'           => \'title\',
        \'orderby\'           => \'ASC\',
        \'tax_query\' => array(
            array(
                \'taxonomy\'  => \'useful_link_categories\', 
                \'field\'     => \'slug\',
                \'terms\'     => $tax->slug
            )
        )
    );

  if (get_posts($args)) :
?>

    <h3><?php echo $tax->name; ?></h3>
    <ul>
       <?php foreach(get_posts($args) as $useful_link_post ): ?> 
            <li><a href="<?php get_field(\'url\'); ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>
        <?php endforeach; ?>
    </ul> 
<?php 

   endif; 
endforeach; ?> 
不要使用此行:

<li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li>
要将标题链接到帖子的永久链接,我想查询我添加到自定义帖子类型“有用链接”中的高级自定义字段(“url”)我试过这个:

<li><a href="<?php get_field(\'url\'); ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>
当然,它不起作用,我错过了很多重要的片段。相反,它链接了帖子的永久链接。我是WordPress后端开发的新手,如有任何帮助,将不胜感激。谢谢你-布莉

2 个回复
SO网友:Edd Smith

Use get_post_meta()

$url = get_post_meta($post->ID,\'_url\',true)
<li><a href="<?php echo $url ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>
SO网友:Milo

您试图检索的字段属于$useful_link_post 后果无需将post ID传递给get_field, 它试图从全局$post 对象,它是您获取此代码的页面的ID,或主循环中的最后一篇文章的ID。要从正确的post ID获取字段,需要显式地将所需的ID传入get_field:

<?php foreach(get_posts($args) as $useful_link_post ): ?> 
    <li><a href="<?php get_field(\'url\', $useful_link_post->ID); ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>
<?php endforeach; ?>

结束

相关推荐