WordPress.org

Ready to get started?Download WordPress

Forums

the_permalink() is out of place in query_posts() widget (7 posts)

  1. chrisruggia
    Member
    Posted 3 years ago #

    I am making a little widget to show post in the "events" category by the "event_date" custom field, using this Date Field plugin.

    The posts are selected properly, but when I use "echo the_permalink()" in line to make my link, it jumps to the front of the line before everything else.

    This is the code from my widget function:

    echo '<ul>';
    
    	query_posts( 'category_name=events&meta_key=date_value&orderby=meta_value&meta_compare=>=&meta_value=' . time() . '&order=ASC' );
    
    	while (have_posts()) : the_post();
    		$post_id = get_the_ID();
    		$link = '<li><a href="';
    		$link .= the_permalink();
    		$link .= '">';
    		echo $link;
    		echo the_title().' - ';
    		echo date("F jS, Y", get_post_meta($post_id, 'date_value', true)).'</a></li>';
     	endwhile;
    
    		echo '</ul>';

    and it generates the following HTML:

    <ul>http://cdri.org/2010/05/native-bee-workshop/<li><a href="">Native Bee Workshop - May 31st, 2010</a></li></ul>

    I tried using the $link variable to force the_permalink() into the right place after the usual loop syntax didn't work.

    Any suggestions would be greatly appreciated!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Try using get_the_permalink() instead.

  3. chrisruggia
    Member
    Posted 3 years ago #

    Thanks!! I tried it, editing that line to:

    $link .= get_the_permalink();

    and got the following:

    Fatal error: Call to undefined function get_the_permalink() in /homepages/40/d319439206/htdocs/wordpress-site/wp-content/plugins/cdri-eventlist.php on line 29

    Any thoughts?

    If it helps, the widget is in the sidebar, which is called after the main loop runs, and before the footer.

    Thank you again for responding.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Sorry - my bad! Try get_permalink()

  5. chrisruggia
    Member
    Posted 3 years ago #

    Now, that did work!

    Is there a listing in the codex somewhere showing the breakdown of loop functions like the_permalink() and get_permalink() and when each should be used?

    THANKS!

  6. chrisruggia
    Member
    Posted 3 years ago #

    I looked a bit and found the Template Tags list, which did tell me what options are available.

    I'm still a little shaky on the difference between the_permalink() and get_permalink(), though. Could you possibly explain, or direct me to a resource that will?

    Thanks again - you've been a huge help.

  7. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    the_permalink returns & displays the permalink. get_permalink just returns the permalink for use in a function or script.

Topic Closed

This topic has been closed to new replies.

About this Topic