Ask your WordPress questions! Pay money and get answers fast! (more info)

Link dependent on $my_query WordPress

  • SOLVED

I'm working with the follow code:

$html .= "<a href='" . get_permalink( $post->ID ). "'>" . get_the_time('j F',$post->ID) . "</a> | ".get_the_author_meta('first_name',$post->post_author)." | $write_comments";

At this point...

"<a href='" . get_permalink( $post->ID ). "'>"

I want to replace that with something that will <strong>1)</strong> check the "newslink" column in the database for content and <strong>2)</strong> If there is content, display that as a link and <strong>3)</strong> if there is no content, then to just display the normal permalink.

This is what I'm trying...

$html .= "$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true)";

if ($newslink_link !='') {
<a href="echo $newslink_link">the_title()</a> ;

} else {

<a href="the_permalink()">the_title()</a> ;
}


$html .= "$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true)";

if ($newslink_link !='') {
<a href="echo $newslink_link">the_title()</a> ;

} else {

<a href="the_permalink()">the_title()</a> ;
}

And then I'm thinking I'd need to replace the line with $newslink_link in the top code... but it's not working. Can someone help me sort this out?

<strong>NOTE:</strong> This is the same file I was working with at this [[LINK href="http://wpquestions.com/question/showChronoLoggedIn/id/8716"]]question[[/LINK]].

I need to know how to actually insert this into the code in the file... the syntax is what's killing me...

Answers (5)

2013-08-16

Arnav Joy answers:

try this



$newslink_link = get_post_meta($post->ID, 'newslink', true);

if( !empty( $newslink_link ) )
$link = $newslink_link;
else
$link = get_permalink( $post->ID );

$html .= "<a href='" .$link . "'>" . get_the_time('j F',$post->ID) . "</a> | ".get_the_author_meta('first_name',$post->post_author)." | $write_comments";


Jason Harris comments:

Sorry for the delay. Yes! This is it. Nailed it. Working perfectly! Thanks.


Jason Harris comments:

Thanks to all the other posters as well. I appreciate your help very much.

2013-08-16

zebra webdesigns answers:

Hello jason

Replace
if ($newslink_link !='') {

This line with
if ($newslink_link) {


zebra webdesigns comments:

Hello Jason

Forgot to ask
Why you used $my_query all of sudden. In your previous question you didnt used it.


Well just in time Arnav posted the correct solution :)

Try it sure it will work

2013-08-16

Giri answers:

$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true);
if ($newslink_link) {
$html .= '<a href="'.$newslink_link.'">'.get_the_title().'</a>' ;
} else {
$html .= '<a href="'.get_permalink().'">'.get_the_title().'</a>' ;
}


Giri comments:

Please note if that's not work. try this

$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true);
if ($newslink_link) {
echo '<a href="'.$newslink_link.'">'.get_the_title().'</a>' ;
} else {
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>' ;
}


Giri comments:


$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true);
if ($newslink_link) {
$html .= '<a href="'.$newslink_link.'">'.get_the_title($my_query->post->ID).'</a>' ;
} else {
$html .= '<a href="'.get_permalink($my_query->post->ID).'">'.get_the_title($my_query->post->ID).'</a>' ;
}


2013-08-16

Dbranes answers:

Here is one way to do it:

// get newslink meta value
$newslink_link = get_post_meta( $my_query->post->ID, 'newslink', true );

// generate link
if ( empty( $newslink_link ) ) {
$html .= sprintf( '<a href="%s">%s</a>', get_permalink( $my_query->post->ID ), get_the_title( $my_query->post->ID ) ) ;
}else{
$html .= sprintf( '<a href="%s">%s</a>', $newslink_link, get_the_title( $my_query->post->ID ) );
}

// output
echo $html;

2013-08-16

Liam Bailey answers:

Both Giri and Zebra Web Designs have given the right answer for me, but if you want to shorten it just do:

<?php
$newslink_link = get_post_meta($my_query->post->ID, 'newslink', true);
$html .= '<a href="' . ($newslink_link) ? $newslink_link : get_permalink() . '">'.get_the_title().'</a>' ;


Also, if you would rather have your posts in 2 seperate lists or have the newslink posts seperately you can do a meta_query

$meta_query = array(
array(
'key' => 'newslink',
'value => '',
'compare' => '!='
)
)


You can pass that into your args, or for simplicity you can just use 'meta_key' => 'newslink' in the top level of the args array or '&meta_key=newslink' in a query string.