Question

<?php
// Attributes
extract( shortcode_atts(
    array(
        'postid' => '',
        'ptitle' => '',
    ), $atts )
);

$postid = $atts['postid'];
$ptitle = $atts['ptitle'];


if(!empty($ptitle)){
    echo 'Whoops! Looks like went wrong here!';
}
else
{
    if($postid == false)
    {
        echo '<span class="ptitle">$ptitle</span>';
    }
    else
    {
        echo '<span class="ptitle"><a href="site.com/$postid">$ptitle</a></span>';
    }
}

I'm trying to have my code do the following

  1. Display an error message if $ptitle is empty

(This is where I need help)

2- That if another value is empty, it echo this <span class="ptitle">$ptitle</span>

3- If the other value ($postid) has something in it then it echoes <span class="ptitle"><a href="site.com/$postid">$ptitle</a></span>

However I just don't understand enough empty values and "false" to create this correctly

Was it helpful?

Solution

If you want it to display if something is "empty", you're using the wrong method with if(!empty($ptitle)){ you're telling it if it is NOT empty.

The ! is a negation character which translates as not.

You need to use if(empty)

Plus, you also need to echo while inside PHP.

Another thing, your $ptitle will not echo while inside single quotes.

Use the following:

Sidenote: You may want to change if($postid == false) to if($postid == true)

<?php
// Attributes

    extract( shortcode_atts(
        array(
            'postid' => '',
            'ptitle' => '',
        ), $atts )
    );

$postid = $atts['postid'];
$ptitle = $atts['ptitle'];


    if(empty($ptitle)){
       echo "Whoops! Looks like went wrong here!";
       }else{ 
       if($postid == false) {
            echo "<span class=\"ptitle\">$ptitle</span>";
        } else { 
            echo "<span class=\"ptitle\"><a href=\"site.com/$postid\">$ptitle</a></span>";
        }
       }

You can also try:

<?php
// Attributes
    extract( shortcode_atts(
        array(
            'postid' => '',
            'ptitle' => '',
        ), $atts )
    );

$postid = $atts['postid'];
$ptitle = $atts['ptitle'];

    if(empty($ptitle)){
       echo "Whoops! Looks like went wrong here!";
       }

       if(empty($postid)) {
            echo "<span class=\"ptitle\">$ptitle</span>";
        }

       if(!empty($postid)) { 
            echo "<span class=\"ptitle\"><a href=\"site.com/$postid\">$ptitle</a></span>";
        }

OTHER TIPS

see if this works for you

if(empty($ptitle)){
    echo 'Whoops! Looks like went wrong here!';
} else if (empty($postid)) {
    echo "<span class='ptitle'>$ptitle</span>";
} else {
    echo "<span class='ptitle'><a href='site.com/$postid'>$ptitle</a></span>";
}

I'm a noob but i think your'e missing the word echo before the strings you want to display and remove the ! sign

something like this:

  $postid = $atts['postid'];
  $ptitle = $atts['ptitle'];


if(empty($ptitle)){
   echo "Whoops! Looks like went wrong here!"
   }else{ 
   if($postid == false) {
    echo "<span class="ptitle">$ptitle</span>"
   } else { 
    echo "<span class="ptitle"><a href=site.com/".$postid.">".$ptitle".</a></span>"
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top