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.
Related
I'm trying to output content from an Advanced Custom Fields (ACF) in my wordpress theme. At the moment though, all I'm getting is the plain text content from the ACF inside double quotation marks, not inside the div and h1 tags.
The code is copied from another theme I made where it worked, which makes me think something is interfering with it somewhere?
<?php $process_title = the_sub_field('process_title'); ?>
<?php if(!empty($process_title)) : ?>
<div class="process-title">
<h1 class="process-heading">
<?php echo $process_title; ?>
</h1>
</div>
<?php endif; ?>
Thanks to #M.Eriksson, the correct code should be:
<?php $process_title = get_sub_field('process_title'); ?>
<?php if(!empty($process_title)) : ?>
<div class="process-title">
<h1 class="process-heading">
<?php echo $process_title; ?>
</h1>
</div>
<?php endif; ?>
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 know nothing about PHP. I have the code below to show next and prev at the bottom of each page, with <-|-> as a separator but I don't want it on the home/index page.
<div align="center" >
<?php echo previous_page_not_post(); ?> <-|-> <?php echo next_page_not_post(); ?>
</div>
How can I do this?
You can add an if statement like this:
<div align="center">
<?php
if (basename($_SERVER['PHP_SELF']) != "index.php") {
echo previous_page_not_post()." <-|-> ".next_page_not_post();
}
?>
</div>
(Look at get current php page name)
I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.
I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>