WordPress.org

Ready to get started?Download WordPress

Forums

Advanced custom fields not displaying in the loop (8 posts)

  1. hazardous_waster
    Member
    Posted 1 year ago #

    Wotcha all

    Basically I'm trying to add a link to a title and it needs to link to the first post in a certain post category ... but I'm using advanded custom fields so that title is a varialbe so in the code looks like this:

    <h2><?php echo get_field('news_title'); ?></h2>

    That displays fine, but I need to add the link to is, so what I tried to do is something like this

    <?php $postslist = get_posts('category=7&numberposts=1&order=DESC&orderby=post_date');
         foreach ($postslist as $post) :
         setup_postdata($post); ?>
       <h2><a href="<?php the_permalink() ?>">Link title here</a></h2>
    <?php endforeach; ?>

    Which works fine getting the link, but if I try replacing 'Link title here' with my Advanced custom field variable in that link it doesn't pick it up, so I tried this ... which in my mind should work fine!...

    <?php $postslist = get_posts('category=7&numberposts=1&order=DESC&orderby=post_date');
         foreach ($postslist as $post) :
         setup_postdata($post); ?>
       <h2><a href="<?php the_permalink() ?>"><?php echo get_field('news_title'); ?></a></h2>
    <?php endforeach; ?>

    Should be so simple but I can't get my head around this one! Any idea why it might not be working?

    Cheers in advance peeps!

  2. elliotcondon
    Member
    Posted 1 year ago #

    your code looks fine.

    the get_field function should find the new global post object but it might be worth a try doing this:

    <h2><a>"><?php echo get_field('news_title', $post->ID); ?></a></h2>
  3. hazardous_waster
    Member
    Posted 1 year ago #

    Thanks for this Elliot, I've tried this but no joy I'm afraid, damn!

    N

  4. Harrison O.
    Member
    Posted 1 year ago #

    Change your loop to this:

    <?php
    global $post;
    $args = array( 'numberposts' => 1, 'category' => 1, 'order'=> 'DESC', 'orderby' => 'post_date' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php echo get_field('news_title'); ?></a></h2>
    <?php endforeach; ?>
  5. hazardous_waster
    Member
    Posted 1 year ago #

    Cheers for that Harrison but I'm still having no joy, I've set up a test page to get this working, it's driving me crazy! Here it is -

    http://www.uktravelboutique.com/?page_id=2358

    This is literally all that is on this page:

    <h2><?php echo get_field('news_title'); ?></h2>
    
    <?php
    global $post;
    $args = array( 'numberposts' => 1, 'category' => 1, 'order'=> 'DESC', 'orderby' => 'post_date' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php echo get_field('news_title'); ?></a></h2>
        <h2><a href="<?php the_permalink(); ?>"><?php echo get_field('news_title'); ?>test</a></h2>
    <?php endforeach; ?>

    As you can see the first title works just fine but as soon as I add the link around it the variable just doesn't want to show up! Anyone get any more ideas on this?

    N

  6. Harrison O.
    Member
    Posted 1 year ago #

    My Antivirus is blocking your site saying it contains malware in it.
    I just tested that loop and it works fine for me. If you have more than one loop in that page, then you need to use a WP_query to query the posts
    http://codex.wordpress.org/Class_Reference/WP_Query
    http://digwp.com/2011/05/loops/#wp-query

  7. anupalusoft
    Member
    Posted 1 year ago #

    am having this same problem. :(
    @hazardous_waster: Have you find any solution for this?

  8. anupalusoft
    Member
    Posted 1 year ago #

    solved cheers. totally my fault in written.

Topic Closed

This topic has been closed to new replies.

About this Topic