0
votes

So I have a custom field displaying a link:

<li><a href="<?php echo get_post_meta($post->ID, 'link_banner', true); ?>" target="<?php echo get_post_meta($post->ID, 'target_banner', true); ?>"><?php the_post_thumbnail(); ?></a></li>

The link banner stands for:

function link_banner(){
  global $post;
  $custom = get_post_custom($post->ID);
  $link_banner = $custom["link_banner"][0];
  ?>
  <label>Link do banner:</label><br/>
  <input name="link_banner" type="text" value="<?php echo $link_banner; ?>" />

  <?php
}

What I need is:

If the client wants the page to display a link INSIDE the WordPress website, the target will be _self. If the client wants the page to display OUTSITE the wordpress, the target will be _blank.

I created another function called target_banner

function target_banner(){
  global $post;
  $custom = get_post_custom($post->ID);
  $link_banner = $custom["link_banner"][0];
  ?>
  <label>Target do banner:</label><br/>
<select>
  <option name="_blank" value="_blank">Nova Janela</option>
  <option name="_self" value="_self">Mesma Janela</option>
</select>

  <?php
}

The problem is: it doesn't works. What´s the solution? :(

The idea is:

  • 1) Inside Link -> SELF
  • 2) Outside Link -> BLANK
  • If the client clicks: Same Window (Mesma Janela), the target will be self.
  • if the client clicks: New Window (Nova Janela), target will be blank.

The client HAS to be the option to display same window or new window.

I tried, I searched all StackOverflow and Google and I didn't find.

Thanks anyway. :)


The website is this one: Ítalo Bianchi.

This big code is used at the slide.

So in the back-end I have three fields.

"Legenda" is for the title (it will not be used for now); "Link" is for the well... link; And the "target" if the link will open in a new window or in the same window.

So all internal links will be open in the same window and all external links will be open at a new window. Sorry. My original post was a little confusing.


If helps: https://gist.github.com/anonymous/5884871

1
Your <select> needs the name attribute... Debug your post via var_dump($_POST).. also, your <option>'s don't need name attributes. - Rob W
@RobW That was displayed: array(0) { } - starkbr
@starkbr: Are you trying to open all the external links in new tab? (except the links to your site's blog posts/pages/etc, of course) - Amal Murali
@AmalMurali yes. There is a custom meta box displaying a input field. IF the client posts a external link it should open a new tab. If the client posts a internal link it should open the website in the current tab. - starkbr

1 Answers

0
votes

Is not clear to me the context of the functions you wrote. Anyway I'd probably do this with your code:

function my_tsel($a, $b){
    if ($a == $b){
        echo ' selected';
    }
}
function target_banner(){
    global $post;
    $custom = get_post_custom($post->ID);
    $target_banner = $custom["target_banner"][0];
  ?>
  <label>Target do banner:</label><br/>
      <select name="taget_banner">
          <option value="_blank"<?php my_tsel($target_banner, '_blank'); ?>>Nova Janela</option>
          <option value="_self"<?php my_tsel($target_banner, '_self'); ?>>Mesma Janela</option>
      </select>

  <?php
}

Overall, probably I'd prefer to automate the check in a little function (e.g. in function.php) getting rid of target_banner()

<?php
    function my_link(){
        $lnk = get_post_meta($post->ID, 'link_banner', true);
        $target = (strstr($lnk, get_bloginfo('url'))) ? '_self' : '_blank';

        echo '<li><a href="'.$lnk.'" target="'.$target.'">';
        the_post_thumbnail();
        echo '</a></li>';
    }
?>

and in my view (let's say single.php)

<?php my_link(); ?>