I'm trying to write an if statement in page.tpl.php to say:
If nodeid == 51, 52, 53 print:
<div class="band main-content">
<?php print render($page['content']); ?>
</div>
Else print:
<div class="band main-content">
<section class="layout">
<?php print render($page['content']); ?>
</section>
</div>
I haven't tested this, but it should work:
<?php
if ($node) {
// Get the current node nid
$nid = $node->nid;
}
//now compare the current node id present in $nid with some node ids you desire that you can put in $desired_node
$desired_node = array(51,52,53); //enter your desired node ids
if (in_array($nid, $desired_node)){
print render($page['content']);
}
if (!in_array($nid, $desired_node)){ ;?>
<section class="layout">
<?php print render($page['content']); ?>
</section>
<?php } ;?>
The condition you're looking for is in_array($nid, array(51, 52, 53)) (or you could use a bunch of logical ORs).
Putting it all together yields:
<?php if(in_array($nid, array(51, 52, 53))): ?>
<div class="band main-content">
<?php print render($page['content']); ?>
</div>
<?php else: ?>
<div class="band main-content">
<section class="layout">
<?php print render($page['content']); ?>
</section>
</div>
<?php endif; ?>
Also, I'm not sure what the end goal is here, but I would be wary about hard coding node IDs in a template. It's definitely not best practice.
I am not sure how do you get an array in $nid, but instead, you should call a function, written in a custom module. That function should check the current nid by taking it from the URL and return either TRUE of FALSE. In any cases, (other than known nodes), you should add logic in a function, defined in a custom module. This helps debugging & enhancements.
Related
i use the following code with custom fields:
But the problem is; when did not put any content in one of custom field it need be hide.
the class style of the other effect it, and that is something I don’t want to show.
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
I want hide one of both custom filed when on of them is empty.
How can i hide it?
<?php if (get_sub_field ('filed2') || get_sub_field('filed1'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php } ?>
You would want to wrap it in an if statement.
You can also do (if you need the conditional logic on a per-field basis):
<?php if (get_sub_field ('filed2'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<?php }; if (get_sub_field ('filed1'));{ ?>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php }; ?>
I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>
I want to print the div with the class called inner-content-div, if the variable $name is not consisted with the following strings.
Gingelly Rolls
Kithul Treacle
Coconut Vinegar
But my PHP code is not working.
Here is my code.
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
?>
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
if(!in_array($name, $exclude_list)){ ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php
}
?>
It should be -
if(!in_array($name->name, $exclude_list)){
as you are printing it -
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>
<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>
<?php if(!in_array($name->name, $exclude_list)): ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php endif; ?>
It could be because $name is an object. Also, probably better to use PHP's alternate syntax for cleaner html/templates.
The other potential issue here is that the casing of the items in the exclude list could potentially not match the value of name. You should probably normalize those to all lower case when doing the comparison.
you can try like this. First print $vid[2], using echo $vid[2];. Then put $vid[2] values into the array called $exclude_list. Then use the following code.
$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
//Your HTML code
}
I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.
I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.
Here is my code:
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1><?=$menuitem->title?></h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
Any help would be greatly appreciated. Thanks!
UPDATED CODE: (this is what works for me now)
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1>
<?php if ($menuitem->uri == $requesteduri):?>
<?=$menuitem->title;?>
<?php else: ?>
<?=$menuitem->title?>
<?php endif;?>
</h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)
<?php echo base_name($_SERVER['REQUEST_URI']); ?>
find out what your loop is outputting (Put this in the loop):
<?php echo $menuitem->uri; ?>
Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:
<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
<?=$menuitem->title?>
<?php else: ?>
<?=$menuitem->title;?>
<?php endif;?>
</h1>
Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.
I created a conditional statement for my custom theme in Concrete5. My codes goal is to toggle layout. If the current page has a child pages under it, it will display an additional sidebar (<div class="grid_3">) to list the subpages items. If there's no child page it would display a full layout (<div class="grid_13">).
Unfortunately I get a different result. there's something I probably had missed on my condition statement. Instead of just display one layout, It is rendering the two layout.
Below is what my code look like:
<? if($c->getNumChildren()) { ?>
<div class="grid_3">
<?php
$bt_sidenav = BlockType::getByHandle('autonav');
$bt_sidenav->controller->orderBy = 'display_asc';
$bt_sidenav->controller->displayPages = 'below';
$bt_sidenav->controller->displaySubPages = 'all';
$bt_sidenav->render('view');
?>
</div>
<div id="main-content-container" class="grid_10">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } else { ?>
<div id="main-content-container" class="grid_13">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } ?>
While your content generation portions of PHP use proper PHP tags (<?php … ?>), your if/else statements use short tags (<? … ?>) which are often disabled.
Use <?php instead.
try to do like this may this will solve the issue
<?php if($c->getNumChildren()!='') { ?>
...
<?php } else { ?>
...
<?php } ?>