WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom field in hyperlink, inside recent posts (2 posts)

  1. hrcwebmaster
    Member
    Posted 1 year ago #

    I'm trying to "pull" a custom field into a hyperlink address. Problem is, I think I'm getting too deep in the coding. I pulled this code from WordPress Codex to display my recent posts by tag.

    <?php
        $key = 'member-name';
        $tag = get_post_meta($post->ID, $key, true);
        $args=array(
    	'numberposts' =>5,
    	'tag' => $tag,
    	'orderby' =>'post_date',
    	'order' => 'DESC',
        );
    $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    	      echo 'Recent News:';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php
    the_title_attribute(); ?>"><?php the_title(); ?
    ></a>
    </li>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    But now I want to add an RSS feed image next to Recent News, which isn't a problem. It's when I want to link that image to the page's custom field that gets to be a problem. I have tried a number of combinations but this seemed to make the most sense to what I'm trying to do...but it doesn't work.

    <?php
        $key = 'member-name';
        $tag = get_post_meta($post->ID, $key, true);
        $args=array(
    	'numberposts' =>5,
    	'tag' => $tag,
    	'orderby' =>'post_date',
    	'order' => 'DESC',
        );
    $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    	      echo '<a href="../tag/<?php $key= "member-name"; echo get_post_meta($post_ID,$key,true); ?>/feed">
    <img src="../wp-content/uploads/RSSfeed24transparent.png" /></a> Recent News:';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php 
    
    the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    The problem arises with this part of the code:
    <a href="../tag/<?php $key= "member-name"; echo get_post_meta($post_ID,$key,true); ?>/feed">
    I have plugged in one of the values in the place of the php script, and I got what I was looking for. So I'm just wondering if I'm getting too deep in the code and need extra () or {} or something like that to help separate it?

  2. hrcwebmaster
    Member
    Posted 1 year ago #

    I figured this out on my own - I put the RSS image before all of the PHP code and it displayed perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic