I'm writing a sidebar for my site and I'm trying to check if:
The page is a post, via: is_post()
The author can edit the post via: current_user_can('edit_post')
I'm trying to combine both of these conditions, but I'm horrid at PHP and I just can't figure out the syntax to do so. I figured it'd be something like below. Could you please let me know what I'm doing wrong? I'm assuming it's something simple, but my inexperience is causing problems and I can't find the right example/documentation to help me out.
<?php if is_single and if (current_user_can('edit_post')) { ?>
<li>Edit post</li>
<?php ;} ?>
It should be:
<?php if (is_single() && current_user_can('edit_post')) { ?>
<li>Edit post</li>
<?php } ?>
The syntax and idea is:
if (true && true) {
// things will happen ...
}
For your functions:
if (is_single() && current_user_can('edit_post')) { ...
Related
I am very new to PHP and I am trying to get a piece of PHP code to run inside of my HTML file.
I have a drop down menu. If users select one item, it should display additional fields. So, I want them to only display if they select that item from the drop down menu. I am trying to select it based on the value for that drop down item. I have not declared any PHP values in a PHP script. This is all in HTML.
I know that with jquery you have to pull in the jquery library before running the script. Do I need to do this with PHP also?
Here is the code that I am trying to run:
Thank you in advance!
<?php
if ($dropmenuValue == "specificParameter") {
?>
<div>
-conditional content-
</div>
<?php
}
?>
<?php
if ($value ) {
?>
<div>
-conditional content-
</div>
<?php
}
?>
If you are setting the $values variable like this
$value = true
Then you can do
<? if($value){ ?>
<div>Content Here</div>
<? } ?>
Is the page a .php page?
A multi conditional statement is done in a different manner. The manner being:
<?php if($val): ?>
<div>
-conditional content-
</div>
<?php endif; ?>
the problem is that your comparing a boolean to a String.That's why your code won't work I would suggest something like If($value){ or if($value==true){.
you want to do if($value == true). The quotes you have around true does not make it a boolean variable.
Everything else in your code looks find.
tldr: correct: if($value == true) ----------
incorrect: if($value == "true")
<?php if ($value ):?>
<div>
-conditional content-
</div>
<?php endif;?>
Here are a few pointers.
$value is a boolean value. It is best to evaluate using if($value)
The PHP if syntax used above is a much cleaner way.
You will be able to see where your if statement begins and ends.
Enjoy!
another problem. I'm making a CMS and I want the login link to disapear when I want to. So I can configure it in my adminCP. When I "flip the switch" my configfunctions.php changes $login to true.
To use it I'm doing:
<?php if($loginenabled = true) { '<li><span class="mmLogin">Login</span></li>' }; ?>
but my site literally prints ALL PHP, also when I do other PHP. So my site looks like:
Home, About us, <?php if($loginenabled = true) { 'login' }; ?>
Does anyone know how to solve it?
Wesley
You can't output HTML code by simply creating a string. To write output, you can use the echo statement:
<?php if ($loginEnabled == true) {
echo '<li><span class="mmLogin">Login</span></li>';
}; ?>
Note the semicolon at the end of the single quotes ('). You can also use PHP's alternate syntax for control structures for cleaner code:
<?php if ($loginEnabled == true): ?>
<li><a href="sample-login.html" class="login">
<span class="mmLogin">Login</span>
</a></li>
<?php endif; ?>
The above will accomplish the same thing. Also note that ($loginEnabled = true) is always true, since = is an assignment operator. To check if $loginEnabled is true, use:
if ($loginEnabled == true)
You can also shorten it to simply:
if ($loginEnabled)
Read more on assignment operators, comparison operators, and the if statement.
I have looked at the other posts on this topic and can't find what I'm looking for.
I want to call a different shortcode depending on the page I am on.
The is_page function doesn't work in the loop so Im not sure how to accomplish this. Here is what I have so far. It makes logical sense to me but won't work for some reason.
if ( is_page(545) ) { <?php echo do_shortcode('[scrapeazon asin="Product1"]') ?>}
elseif ( is_page(525) ) { <?php echo do_shortcode('[scrapeazon asin="Product2"]') ?>}
else ...
Any help is much appreciated!
Thanks,
Mike
You're using the function correctly according to Wordpress Docs but your PHP is off. You have <?php open tag when php is already open, matching closing tags that aren't required either, and you're missing semicolons.
<?php
if ( is_page(545) ) { echo do_shortcode('[scrapeazon asin="Product1"]'); }
elseif ( is_page(525) ) { echo do_shortcode('[scrapeazon asin="Product2"]'); }
else ...
?>
You don't need the curly braces in there for single line executions but I left them for the sake of highlighting the error.
There is a cleaner way to do it though
<?php
$pages=array(
545=>'Product1',
525=>'Product2',
567=>'Product3',
568=>'Product4',
// etc
// etc
);
foreach ($pages as $page_id=>$asin){
if (is_page($page_id)) {
echo do_shortcode('[scrapeazon asin="'.$asin.'"]');
break; // because you used elseif you must want to stop checking ids after finding one is a page
}
}
?>
Though it's probably faster, and less work for your db, to select all the pages at once and loop through that array.
I'm trying to create a PHP 'if' statement that tells an image to display on certain pages in Wordpress. What I'm trying to do is shown below, but I think the syntax is incorrect.
I'm trying to say "If the page displayed equals 'about' or 'gallery' and that page is not 'services', go ahead and display the image.
Here's the section of code that you should be able to analyze and see where I went wrong:
<?php if(is_page('about' || 'gallery') && (is_page != 'services')) { ?>
<li><img id="profile_pic" alt="DermaKare (Official Fan Page)" src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/50556_123510051042639_3343345_n.jpg" class="photo img" /></li>
<?php } ?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Banner") ) : ?>
<?php /*?><p><a href='<?php bloginfo('siteurl')?>/wp-admin/widgets.php'>Widgetize this sidebar</a></p><?php */?>
<?php endif; ?>
Thanks in advance for any help on this.....
If a page is about or gallery then it never is services. So you can spare that not part.
Next to that you need to call the function twice:
if (is_page('about') || is_page('gallery'))
{
# then
}
This should do it. Wrapped into your code this should look like:
<?php if (is_page('about') || is_page('gallery')) { ?>
if ( ( is_page('about') || is_page('gallery') ) && !is_page('services') )
watch the brackets.
edit: and what #hakre said, if it's about or gallery, it never is services
<?php if( (is_page('about') || is_page('gallery') ) && (!is_page('services') ) {
echo "<li><img id=\"profile_pic\" alt=\"DermaKare (Official Fan Page)\" src=\"http://profile.ak.fbcdn.net/hprofile-ak-snc4/50556_123510051042639_3343345_n.jpg\" class=\"photo img\" /></li>";
} ?>
this should work
I don't understand PHP very well... not good start I know.
I'm using http://detectmobilebrowsers.mobi/ for mobile/desktop website, and I'm using wordpress as my CMS.
I have set up my function like so...
require_once('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,true,true,false,false,false);
But now in my theme, I want a conditional statement similar to this...
<?php if (is_home()) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Though I want it to return the values from my function. See below my rubbish attempt below...
<?php if (mobile_device_detect==true) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Obviously this is not going to work lol, but can you see what I'm trying to achieve? and help would be most awesome thanks!
Cheers
Josh
Nearly there.
require_once('php/mobile_device_detect.php');
$mobile = mobile_device_detect(true,true,true,true,true,true,false,false,false);
Then check for
if ($mobile==true) // then do the rest of your stuff