This is the first time I am doing something using PHP. It might be a very stupid question but I want to add a class to this line:
<?php
$image_url='http://test.haardenenkachels.com/wp-content/uploads/s2dlogo.jpg';
?>
<img src="<?php echo $image_url;?>">
How do I add a class to this line?
Thank you so much!
Regards,
Jelle
<?php
$image_url='http://test.haardenenkachels.com/wp-content/uploads/s2dlogo.jpg';
$image_class='yourclass';
?>
<img class="<?php echo $image_class;?>" src="<?php echo $image_url;?>">
would give you
<img class="yourclass" src="http://test.haardenenkachels.com/wp-content/uploads/s2dlogo.jpg">
Remember, that you must add the class to your style sheet ... and of course you can find listed in the file where the tag is present img
Related
Is it possible to add anchor links to Advanced Custom Fields?
I've created a page that shows images that when you click on the image a lightbox opens with the description. I have it all working without using Advanced Custom Fields but I'm trying to make it user friendly for the client so I added fields for them to just fill in the information and everything is already styled.
The problem is the tabs use an Anchor Link to link to the ID DIV that opens up the lightbox with the description - it works but not when I use the fields I created.
I created a url field for them to fill in the #div name for the anchor url and a field for the name of the #div for the anchor to link to. (Hope that makes sense!)
I'm also using repeater fields so they can add/delete flavors.
This is the code I am using -
<?php
// check if the repeater field has rows of data
if( have_rows('flavors') ):
// loop through the rows of data
while ( have_rows('flavors') ) : the_row();
// display a sub field value
?>
<li>
******** This is the anchor link *************
<?php
$link = get_sub_field('flavor_name');
if( $link ): ?>
<a href="<?php echo esc_url( $link ); ?>" class="wplightbox" data-width=900 data-height=800>
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
</a>
<?php endif; ?>
********* This is the div that needs to pick up the anchor name typed in as the Div ID name *****
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
<?php the_sub_field('flavor_name'); ?>
<?php endif; ?>
<div class="flavors">
<div class="flavorsleft">
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="flavorsright">
<h3><?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?></h3>
<div class="percent">
<?php if( get_sub_field('alcohol_percentage') ): ?>
<?php the_sub_field('alcohol_percentage'); ?>
<?php endif; ?>
</div>
<?php if( get_sub_field('beer_description') ): ?>
<?php the_sub_field('beer_description'); ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="clear"></div>
</li>
<?php
endwhile;
else :
// no rows found
endif;
?>
</ul>```
This is the dev site where it's at - https://headley.tfm-dev.com/beers
The page is working right now because it's currently built not using the ACF fields (I took them off for now and rebuilt it) but I would like to change it so the owner can make changes.
Thank you!
[![Screenshot of Advanced Custom Fields][1]][1]
[![Screenshot of Advanced Custom Fields Redone][2]][2]
[1]: https://i.stack.imgur.com/NeKYe.jpg
[2]: https://i.stack.imgur.com/uUN0G.png
On your dev site in the source code the anchor href is an id but you are using a page link field to match to the get_sub_field('flavor_bottom_section_name') so this would return a url so won't work.
Correct me if I'm wrong but you are using 2 repeaters, does that not reply on each of them to match up to each other? I would do this wiht one repeater field and you'd be able to avoid mistakes being made and would probably require less fields e.g you would only need one field for the anchor and one field for the div. You can have 1 repeater field but you can have 2 separate loops through the same repeater just getting what you want out of it on each loop.
ADDITIONAL
This line of code nothing is being ouput into the id=""
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
I think you need to echo the sub field like this maybe
<div id="<?php echo get_sub_field('flavor_name') ?>" style="display: none;">
Yon can see in the red boxes I've marked that the id attribute is empty on the div, even if I change it locally in the inspector it works, not sure why you got an error earlier but I also think that the value in your field might have # before it but the # is only only required in the href not the id.
Final Solution (Hopefully)
Ok I used your code and I see why you were getting the critical error this:
<a href="#<?php if( get_sub_field('beer_title') ): ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
Needs to change to this:
<a href="#<?php the_sub_field('beer_title'); ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
You have and if in there which isn't needed so removing that and echoing the sub field fixes it, we just needed to remove the endif also. I should have asked what the error was originally.
What you are saying here is
<a href="#
// IF THERE IS A SUB FIELD VALUE FOR BEER TITLE
<?php if( get_sub_field('beer_title') ): ?>"
// OUTPUT THIS
class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
get_sub_field() doesn't output anything it has to be echoed or else use the_sub_field() instead.
The problem on a Wordpress site is that even though links are set properly in the WP admin area (see attachment 1) they do not work once the site has been published. When using inspect element it kind of shows the problem (see attachment 2).
Clicking and opening the links within the admin area is working just fine. Other links on the website work correctly. What is missing here?
Code in WP template:
<?php if ($section['link_to_doc']) : ?>
<?php foreach ($section['link_to_doc'] as $link) : ?>
<div>
<a href="<?php echo $link['link_to_doc'] ?>" class="btn-txt">
<?php echo $link['text_of_link'] ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Repeater fields:
It seems the ACF link object is not returning the right output in the href because of an error in how the links where coded.
Usually you'll want something like this:
$link = get_field('link');
<a href="<?= $link['url']) ?>"<?= $link['title'] ?></a>
That's providing the ACF field is not nested, and the field name is 'link' and there's just one.
Amended code (presumably):
<?php if ($section['link_to_doc']) : ?>
<?php foreach ($section['link_to_doc'] as $link) : ?>
<div>
<a href="<?= $link['url'] ?>" class="btn-txt">
<?= $link['text_of_link'] ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Even thought the whole concept is not the best, as you wouldn't create an extra custom field to put the text of the link since you can use $link['title'] for that but yeah...
Edit 2:
According to your screenshot edit the a to be:
<a href="<?= $link['link'] ?>" class="btn-txt">
<?= $link['text_of_link'] ?>
</a>
What I'm attempting to do is to remove some <p> tags that wrap the header logo image which does not contain any text because I think they should not be there.
But this is throwing the following error when testing via the W3-validator.
Line 261, Column 216: Element p not allowed as child of element a in
this context. (Suppressing further errors from this subtree.)
What file should I look in to locate line 261?
I'm stuck. I've searched for hours and can't find the file-path to the file that contains the string I need to edit. I currently don't have access to link hints function so that out of the question.
Any suggestions would be highly appreciated.
Thanks a million
Fish
Update: Here is some code regarding the logo from the header.phtml
<div class="header-wrapper <?php echo $hdr_style?>">
<header>
<?php if ( $config['design']['below_logo'] ) : ?>
<div class="row clearfix">
<div class="grid_12">
<?php if ($this->getIsHomePage()):?>
<h1 class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><?php echo $this->getChildHtml('theme_logo', false) ?></h1>
<?php else:?>
<strong><?php echo $this->getLogoAlt() ?></strong><?php echo $this->getChildHtml('theme_logo', false) ?>
<?php endif?>
The code comes from <?php echo $this->getChildHtml('theme_logo', false) ?>.
You need to check the layout files in the theme and see which template the 'theme_logo block' is using. That's where you can find and remove the <p> tags.
I need to add a spantag to an h1 heading in wordpress. I tried writing in through the CMS but it renders the spantag as text.
The site's title is a name and I want to give different styles to the words.
Suggestions?
Following David Thomas's advice I've written this: but it appends the span last and empty.
<h1>
<a href="<?php bloginfo('url'); ?>/">
<?php
$completeName = bloginfo('name');
$split = explode(" ",$completeName);
echo $split[0]."<span>".$split[1]."</span>"
?>
</a>
</h1>
You should be using get_bloginfo as Felipe suggested, but still pass in the 'name' parameter.
I was just working on the same code for the same reason and thought I should post for future visitors:
<?php
$completeName = get_bloginfo('name');
$nameParts = explode(" ", $completeName);
echo '<span>'.$nameParts[0].'</span> '.$nameParts[1];
?>
Apply the style to the h1 instead.
By the way, you probably want to edit the theme.
What about:
<? echo get_bloginfo('name', 'raw'); ?>
Can't test it for now, but here's the documentation about it:
bloginfo()
==> it just echoes the result of get_bloginfo() and there's a second parameter to this function
==> wptexturize(), a function that WON'T be called so beware!
Nope. Ignore this. The space is put there by browser.
This is a HTML snippet from my application:
Correct answers:
0 / 6<br /><br />
You have failed to pass the final test.
<a href="/module/controller/course/id/5" class="accessible-link">
Click here
</a>
to return to the training.
As you can see, there is a single space after the </a> closing tag. Yet in the browser the space is added inside the anchor. So it looks like this:
This is the PHP code which produces the HTML:
<?php if (isset($this->correctAnswersCount) && isset($this->answersCount)): ?>
<?php echo Zend_Registry::get('translate')->_('Počet správnych odpovedí'); ?>:
<?php echo ToHtml($this->correctAnswersCount); ?> / <?php echo ToHtml($this->answersCount); ?><br /><br />
<?php endif; ?>
<?php echo Zend_Registry::get('translate')->_('Záverečný test sa vám nepodarilo úspešne absolvovať.'), "\n"; ?>
<a href="<?php echo ToHtml($this->backToCourseUri); ?>" class="accessible-link">
<?php echo Zend_Registry::get('translate')->_('Kliknite'), "\n"; ?>
</a>
<?php echo Zend_Registry::get('translate')->_('pre návrat do kurzu.'), "\n"; ?>
I am completely baffled by this and cannot figure out what's causing this even though I've been staring into the code for 30 minutes now.
This is a relevant part from the translation file:
'Kliknite' => 'Click here',
As you can see, there should be no space added by Zend_Translate.
Change this:
<a href="<?php echo ToHtml($this->backToCourseUri); ?>" class="accessible-link">
<?php echo Zend_Registry::get('translate')->_('Kliknite'), "\n"; ?>
</a>
Into this:
<a href="<?php echo ToHtml($this->backToCourseUri); ?>" class="accessible-link">
<?php echo Zend_Registry::get('translate')->_('Kliknite'), "\n"; ?></a>
The </a> should be in the same line after the <?php echo Zend_Registry::get('translate')->_('Kliknite'), "\n"; ?> aka Click Here
EDIT:
The new line and the spaces after it renders like 1 space that is still inside de <a></a> tags, that is where the blank space is coming from.
EDIT2:
For the record I also don't like the closing tag to be next to the content instead of a being in a new line but that's how it has to be done in order to work correctly.
I like good formatted code and I always look for a autoformat command in my IDE.
But at least for example in Visual Studio when you hit Ctrl + K, Ctrl + D (the Format Document shorcut) the closing tags like the </a> are not automatically moved to a new line for this exact reason: that it should not break the way it looks before the auto format.
Close the 'a' tag directly after the next, without a newline, like this:
Click here
Try it like this:
Click here
I am not sure if this will work, but it is worth trying.
Put immediately after the </a> tag.