WordPress.org

Ready to get started?Download WordPress

Forums

Custom Fields That Add Text After Post Url (3 posts)

  1. ianbee
    Member
    Posted 1 year ago #

    I would like to have custom fields in my posts and the value that I enter in for that post gets displayed at the end of the post url when you click on it. I would like to add ?fromwhere=news" at the end of some posts or ?fromwhere=videos", ?fromwhere=phots etc. I can sucesfully add in ?fromwhere=news manually, but I would like to do it with custom fields so it would be much easier for me. Here is the code I have so far:

    $returnlink = ($this->params['lightbox'])? ('"'.$cg_url.'/includes/CatGridPost.php?ID='.$single->ID.'" class="cgpost"') : (''.get_permalink($single->ID)).'';
    		$news = get_post_meta($post->ID, 'newsMP', true);
    		$cgfontsize=$this->cg_get_font_size();
    		$excerpt = apply_filters('get_the_excerpt', $single->post_excerpt);
    		$post = get_post($single->ID); // unless $single is already a complete object
    		$cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div><div class="cgtitle cgnojs '.$this->params['showtitle'].'"><p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;"><a href="'.$returnlink.''.$news.'">'.$title.'</a></p><div id="lol">'.$excerpt.' </div></div>';
    		return $cgtitle;
    	}

    So this grabs the title, excerpt and post id and displays it back to wordpress. As you can see I tried using get_post_meta to get the custom field in wordpress and then I put '.$news.' right and the end of the return link. What I get is, nothing. Absolutely nothing. :( I'm typing in text in the custom field titled "newsMP" but it doesn't display annythhhiiiiiing. I'm going to add more coding to this like if statments that say if customfield is this, then do this. :)

    If anyone could help me out it would make me really really happy lol.

  2. ianbee
    Member
    Posted 1 year ago #

    Update: I tried changing the code so I add a key to display the custom field text after the url.. here is what I did:

    $news = get_post_meta($post->ID, $key, TRUE);
     $key = 'testme';
    		$cgfontsize=$this->cg_get_font_size();
    		$excerpt = apply_filters('get_the_excerpt', $single->post_excerpt);
    		$post = get_post($single->ID); // unless $single is already a complete object
    		$cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div>
    <div class="cgtitle cgnojs '.$this->params['showtitle'].'">
    <p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;">
    <a href="'.$returnlink.''.$news.'">'.$title.'</a></p>
    <div id="lol">'.$excerpt.' </div></div>';

    So yeah, I added a $key which would be the name of my custom field. I entered the custom field on wordpress with the text field filled in as "testtest" and still, it displayed nothing at the end of the url. :(
    If anyone could help me out I'd appreciate it so much! I honestly would.

  3. wpismypuppet
    Member
    Posted 1 year ago #

    It's hard to tell exactly what that code is doing, but it seems to me that this is pulled from a function and not inside any loop?

    You have this line of code:

    $post = get_post($single->ID); // unless $single is already a complete object

    Which looks like it's getting a post object. Maybe you need to place your:

    $news = get_post_meta($post->ID, 'newsMP', true);

    Line AFTER that get_post() command? Not only that, but you could simplify your custom field by just typing in "news" or "videos" and change this line:

    $cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div><div class="cgtitle cgnojs '.$this->params['showtitle'].'"><p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;"><a href="'.$returnlink.''.$news.'">'.$title.'</a></p><div id="lol">'.$excerpt.' </div></div>';

    To something more like this:

    $cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div><div class="cgtitle cgnojs '.$this->params['showtitle'].'"><p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;"><a href="'.$returnlink.'?fromwhere='.$news.'">'.$title.'</a></p><div id="lol">'.$excerpt.' </div></div>';

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.