Wordpress PHP multiple else statements - php

I am making a wordpress theme and i want to have some of the page templates with an extended header div tag so i can add extra things in.
This is the code i have:
<?php if (!is_page_template('page-homepage.php')) { echo "</div>"; } ?>
I want it so i can have alternatives to the page-homepage.php so for example page-team.php as i want that individual page template to not close the div yet either, i tried writing this but it just does every page:
<?php if (!is_page_template('page-homepage.php' or 'page-team.php')) { echo "</div>"; } ?>
Any ideas? My php skills are not very advanced!
thanks :D

You are making a logical operation on two strings ('page-homepage.php' or 'page-team.php') which evaluates to true, so the statement becomes:
<?php if (!is_page_template(true)) { echo "</div>"; } ?>
Try this instead:
<?php if (!is_page_template('page-homepage.php') && !is_page_template('page-team.php')) { echo "</div>"; } ?>

Related

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

Wordpress Footer Text PHP

Currently in the PHP file it has:
<?php if($myfooter_text){?>
<?php echo of_get_option('footer_text'); ?>
<?php } else { ?>
something else here
<?php } ?>
What I would like is for it to insert the footer text it finds using $myfooter_text but also add a link to the end of whatever has been pre filled in the footer text. I tried to use concatenation with the following:
<?php echo of_get_option('footer_text') . 'mylink; ?>
However this still just shows the predefined footer text and not the additional content. Is there a way to do this? I'm aware it could be added in the footer area of the dashboard but this isnt what i want to do.
Try it like this:
<?php echo of_get_option('footer_text'); ?> mylink
You can concatenate them as well like this or do it your way and be sure you close all the quotes:
<?php echo of_get_option('footer_text') . 'mylink'; ?>

Hide and show divs using PHP

So I have this code that requires to be a member. Now the question is what code do I add to hide two different divs called "socialsignup" and "formwrap". Another question is can you have multiple <?php code here ?>s in different places on the same page? Much appreciated with any help.
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
You can wrap multiple time of PHP tags in a page.
That's answer for your fisrt and second question.
You can check like this
<?php if($val=='socialsignup') {?>
html for the social sign up
<?php } else if ($val=='formwrap') {?>
html for formwrap
<?php }else{?>
remaining conditions
<?php } ?>

Magento - Adding Register URL inside a href

Hey I am very new to PHP, I come from a design background, but I am now trying to figure out some of the PHP myself.
I have a problem with a if else statement where I would like users to register to view product prices (Logged in users can view prices, but other users are presented with a 'please register' blurb). But I can't figure out how to get the register URL to echo inside the a href tag.
Here is my code:
<?php if (Mage::getSingleton('customer/session')->isLoggedIn()) {
echo $this->getPriceHtml($_product, true);} else { echo 'Please register to view prices';}
?>
<?php echo Mage::helper('persistent')->getCreateAccountUrl($this->getCreateAccountUrl()) ?>
The code on the bottom is used to call the register URL. I can't figure out how to properly place the register URL code into the a href tags without it generating some sort of error. Can anyone help?
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
echo $this->getPriceHtml($_product, true);
}
else {
echo 'Please register to view prices';
}
?>
Try this code. Basically you want to join strings together. Mage::helper... returns a string. So join that result with the static string which includes your html for a link.
Bit to deep for this question but really I would not use echo to display any html. Where possible just use php to display dynamic content. e.g.
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
echo $this->getPriceHtml($_product, true);
}
else { ?>
Please register to view prices
<?php } ?>

Wordpress: Condition Tags PHP

This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.

Categories