我有点新的PHP和我有一个问题,应该是小但我无法弄明白。
我有一个wordpress页面,每个帖子都有2个链接。我想只在href =""中有地址时显示链接。否则我希望他们保持隐形/消失。
<span class="play"><a rel="lightbox" href="<?php swf_file_url(); ?>">Play</a></span>
<span class="download"><a href="<?php swf_file_url(); ?>" download>Download</a></span>
任何人都知道一种简单的方法吗?
add_action("admin_init", "swf_init");
add_action('save_post', 'save_swf_link');
function swf_init(){
add_meta_box("my-swf", "swf", "swf_link", "game", "normal", "low");
}
function swf_link(){
global $post;
$custom = get_post_custom($post->ID);
$link = $custom["link"][0];
$count = 0;
echo '<div class="link_header">';
$query_swf_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'application/x-shockwave-flash',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$query_swf = new WP_Query( $query_swf_args );
$swf = array();
echo '<select name="link">';
echo '<option class="swf_select">SELECT SWF FILE</option>';
foreach ( $query_swf->posts as $file) {
if($link == $swf[]= $file->guid){
echo '<option value="'.$swf[]= $file->guid.'" selected="true">'.$swf[]= $file->guid.'</option>';
}else{
echo '<option value="'.$swf[]= $file->guid.'">'.$swf[]= $file->guid.'</option>';
}
$count++;
}
echo '</select><br /></div>';
echo '<p>Kies een swf document om te laten zien op de website.</p>';
echo '<div class="swf_count"><span>Files:</span> <b>'.$count.'</b></div>';
}
function save_swf_link(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
update_post_meta($post->ID, "link", $_POST["link"]);
}
add_action( 'admin_head', 'swf_css' );
function swf_css() {
echo '<style type="text/css">
.swf_select{
font-weight:bold;
background:#e5e5e5;
}
.swf_count{
font-size:9px;
color:#0066ff;
text-transform:uppercase;
background:#f3f3f3;
border-top:solid 1px #e5e5e5;
padding:6px 6px 6px 12px;
margin:0px -6px -8px -6px;
-moz-border-radius:0px 0px 6px 6px;
-webkit-border-radius:0px 0px 6px 6px;
border-radius:0px 0px 6px 6px;
}
.swf_count span{color:#666;}
</style>';
}
function swf_file_url(){
global $wp_query;
$custom = get_post_custom($wp_query->post->ID);
echo $custom['link'][0];
}
&#13;
答案 0 :(得分:0)
http://php.net/manual/en/control-structures.alternative-syntax.php
变化
function swf_file_url(){
global $wp_query;
$custom = get_post_custom($wp_query->post->ID);
echo $custom['link'][0];
}
到
function swf_file_url($return = false){
global $wp_query;
$custom = get_post_custom($wp_query->post->ID);
if($return) return $custom['link'][0];
echo $custom['link'][0];
}
然后使用:
<?if(swf_file_url(1)): ?>
<span class="play"><a rel="lightbox" href="<?php swf_file_url(); ?>">Play</a></span>
<span class="download"><a href="<?php swf_file_url(); ?>" download>Download</a></span>
<?php endif; ?>
答案 1 :(得分:0)
您的问题并不清楚,但要检查swf_file_url();
是否包含值,您可以使用empty(),即:
if(!empty(swf_file_url())){
$swfUrl = swf_file_url();
echo <<< EOF
<span class="play"><a rel="lightbox" href="$swfUrl">Play</a></span>
<span class="download"><a href="$swfUrl" download>Download</a></span>
EOF;
}