0

I modified this code to use for a basic useful link library, I found the code at 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; ?> 

Instead of having this line:

<li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li>

to link the title to the post's permalink, I want to query the advanced custom field('url') that I added to the custom post type called 'useful-links.' I tried this:

<li><a href="<?php get_field('url'); ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>

Of course, it doesn't work, I have missed a lot of important snippets there. It links the post's permalink instead. I am kind of new to the backend development of WordPress, any helps would be appreciated. Thank you. -Boolie

2 Answers 2

0

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>
2
  • Thank you for your suggestion. But where should I add this
    – BooBoo
    Jul 29, 2014 at 21:45
  • Wherever you want to output the link.
    – Edd Aslin
    Jul 29, 2014 at 21:47
0

The fields you're trying to retrieve belong to each post within your $useful_link_post results. Without passing a post ID to get_field, it's trying to fetch that field from whatever ID exists in the global $post object, which is the ID of the page you've got this code on, or the last post in the main loop. To fetch the field from the correct post ID, you need to explicitly pass the desired ID in 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; ?>
1
  • Thank you, I used "get_field (...) but it didn't work so I tried "the_field(..) instead. It works! Thank you so much, here is the line: <li><a href="<?php $url = get_post_meta($post->ID,'_url',true); the_field('url', $useful_link_post->ID); ?>" target="_blank" title="Click here to read more"><?php echo $useful_link_post->post_title; ?></a></li>
    – BooBoo
    Jul 30, 2014 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.