Viewing 10 replies - 1 through 10 (of 10 total)
  • looks like you forgot the foreach from the example on the css-tricks site. Hope this helps 🙂

    <?php
        $featured_blogs = get_post_meta( $post->ID, 'Featured-Blog', true); ?>
        <?php foreach($featured_blogs as $featured_blog) { ?>
            // do something with $featured_blog such as echo $featured_blog
            <a href=""> <!-- use PHP code in between quotes to display URL -->
            <?php the_title();
            echo $featured_blog; ?>
            </a>
    }
    Thread Starter k00kaine

    (@k00kaine)

    Hey thanks for your fast response. I did try out your code, had a little issue at first but I removed the { and }

    It does pull the first custom field url and gives a clickable link with the title of the page which is indeed what I want, but the second custom field didnt seem to show up

    The code is like this now. same as yours, but { and } removed

    <?php
        $featured_blogs = get_post_meta( $post->ID, 'Featured-Blog', false); ?>
        <?php foreach($featured_blogs as $featured_blog)  ?>
            // do something with $featured_blog such as echo $featured_blog
            <a href=""> <!-- use PHP code in between quotes to display URL -->
            <?php the_title();
            echo $featured_blog;?>
            </a>

    If removing braces worked you must already be in the ‘loop’. I didn’t post the entire code because I didn’t know the second variable 🙂

    To simplify things, you could just use the exact same code twice in a row and just change the variables:

    $featured_blogs to $name_of_second_field_array
    $featured_blog to $name_of_second_field_value

    Thread Starter k00kaine

    (@k00kaine)

    You can see here in the screenshot I have 3 different ways to try out what I’m trying to do. And you can see the results.

    The very top one, I cannot figure out how to get it to just have Title of the URL. And the bottom ones that you came up with, do have it. Except yours shows the same url and post title. The one on the very top pulls both of the custom field urls, but it doesn’t have the title of the page URL.

    Thread Starter k00kaine

    (@k00kaine)

    I looked at the image. Now I have a better idea of what you are trying to do so in the morning I will try to post a solution 🙂

    Thread Starter k00kaine

    (@k00kaine)

    thanks for your effort. Im going to continue looking on the net for some other ways to do this.

    You won’t be able to extract the title of URL because it is not in the database. You would need to have another custom field called featured_blog_post_title or something like that and assign it the title of the post.

    Thread Starter k00kaine

    (@k00kaine)

    /

    Thread Starter k00kaine

    (@k00kaine)

    Okay, I have the code pulling the custom fields URL and title of the url. Now I can’t seem to get it to show the second featured blog. Here is the working code.

    Here is a screenshot in Actions:

    <?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
    
    		$related=explode(',',$related);
    		$args = array_merge( array('post__in'  => $related, $wp_query->query ) );
    		query_posts($args);
    		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		<div id="<?php the_ID(); ?>">
    			<a href="<?php the_permalink();?>"><p class="caption"><?php the_title(); ?></p></a>
    		</div>
    
    	<?php endwhile; else: ?>
     	<p>no related</p>
    	<?php endif; wp_reset_query();?>

    This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.

    Screenshot of this one in action:

    <?php
      $custom_fields = get_post_custom($post_id); //Current post id
      $my_custom_field = $custom_fields['Featured-Blog']; //key name
      foreach ( $my_custom_field as $key => $url )
     echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom Field on Post – Pull Post Title and URL’ is closed to new replies.