Changing link text php from wordpress shortcode - php

I am working with a client's wordpress affiliate site.
We are using two plugins in conjunction with each other.
ACF (Advanced Custom Fields)
EasyAzon (This inserts an affiliate link via shortcode into your post)
I have set up custom fields for this shortcode, and coded it into my them like this.
<div class="top-link"> <?php the_field('link-button'); ?></div>
This outputs:
"<div class="top-link">
Buy Now
</div>"
And now I want to use the same function "<?php the_field('link-button'); ?>" at the bottom of my page.
However I would like it to display text from yet another ACF field "<?php the_field('bottom-link'); ?>" within the anchor tag.
But if there is no text in that field, I want it to use the text "Click to buy on Amazon"
So here is what I have:
<div class="top-link"><?php the_field('link-button'); ?></div>
<!-- The post content -->
<div class="bottom-link"> <?php the_field('link-button'); ?></div>
<?php
$link = get_field('bottom-link');
if (get_field('bottom-link') != '') {
$link = get_field('bottom-link');
}
else {
$link = "Click to Buy Amazon";
}
?>
<script>
var link = '<?php echo $link; ?>';
jQuery( document ).ready(function() {
jQuery('.bottom-link a').text(link);
});
</script>
I'm pretty new to both languages, and maybe complicating things a lot. Any thoughts would help!
Here is the dev site live if you're interested in looking at that.
http://tttrend.com/wtbag/gifts-for-men/general/test-post/

Here is what you should do
<?php
$link = get_field('bottom-link');
if (!isset($link) || empty($link) ) {
$link = "Click to Buy Amazon";
}
?>
Also if you are using the same field over and over, try getting the results in a variable and then using the variable instead. Using get_field, sends a request to the database and using it over and over again might slow your application down.

Related

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

Turning a <div> content into a $variable in php

I have this website were you can order products.
The title of the products you can order are in HTML:
<div class="whatever"> Title </div>
I want to retrieve this "title" and set my php variable $product to the value "Title".
I have search a lot on the internet but somehow I am not able to find my answer.
How can I do it?
You can use \DOMDocument->loadHTML();
Ex:
<?php
$doc = new \DomDocument();
$doc->loadHTML('<div class="whatever"> Title </div>');
View examples here:
http://docs.php.net/manual/en/domdocument.loadhtml.php
This is assuming that your source is available to php. It would probably be more pragmatic to extract the value with javascript in the client and send it with the page request. If your app is well structured, the logic that renders the title into the page in the first place is probably where you should be looking to retrieve the information rather than trying to parse the html separately.
If you mean that you would like to do this from the client side, you should be using AJAX to achieve this. However, I think you mean that you want to put the HTML in a variable. That is very simple:
$variable = "<div class=\"whatever\"> Title </div>";
And to output the HTML:
echo $variable;
You can also add multiple elements to a single variable by concatting.
$variable = "";
$variable .= "<div class=\"whatever\"> Title </div>";
$variable .= "<div class=\"whatever\"> Another Title </div>";
echo $variable;
If you mean that you want to echo a variable within a dv, that works exactly the same way:
<div class="title"><?php echo $product; ?></div>
Or better looking:
<div class="title"><?= $product; ?></div>

link to the other website in "the_title()" of the WordPress page

So i have the single-portfolio.php which present one of my project.
This function makes proper title to my projects every time I choose one.
<h1><?php the_title(); ?></h1>
Now whatever project i choose it always transport me to the Angela...
<h1><?php the_title(); ?></h1>
What i want to do is to have proper link to the proper project in the title.
I figured sth like this but it does not work.
<?php
$f = "http://facebook.com/";
$t = "http://twitter.com/";
$l = "http://linkedin.com/";
if (the_title()=='Facebook') {
Echo "<a href=$f> Facebook</a>";
} elseif (the_title()=='Twitter') {
Echo "<a href=$t> Twitter</a>";
} else {
Echo "<a href=$l> Linkedin</a>";
}
?>
What i get on the page is 3 times written Facebook if its Facebook page or 3 times Twitter e.g:
FacebookFacebookFacebook(the only last one "Facebook" is a link)
The problem is that the_title() echos the title, rather than returning it (as documented in the Codex). That means that, for each of your if conditions, the title gets echoed out.
Try using get_the_title() instead - this returns the post title rather than echoing it.
the_title() is a function that returns the title of the current page/post in wordpress. For the FB/Twitter/LinkedIn portion of your code you should just include "Facebook" instead of using the_title()
Use a custom field in your post to store the link in the post. Call the field "url" for example:
now you can read the field in your template and use it:
<?php $url = get_post_meta( get_the_ID(), "url", true ); ?>
<h1><?php the_title(); ?></h1>

Wordpress, Jquery, Custom Fields, PHP Loops

I am working on a new blog site, and I am far from seasoned when it comes to PHP.
http://www.theredo.ca/
What I am trying to do is the following:
Each post has a custom field, "timer".
Inside the custom field, there is a piece of javascript:
<script type="text/javascript">
$j(document).ready(function(){
//init plugin
$j('#event-1').fancyCountdown({year:2011, month:9, day:31, hour:0, minute:0, second:0, timezone:0, dayDigitsAmount: 3, digits:{days:true,hours:true,minutes:true,seconds:true}});
});
</script>
I need to loop through all my posts and place this javascript in the footer - each post will have a slightly unique javascript (ID and other variables).
Currently I am using this piece of code, which isn't a loop and is only placing the last custom field into the footer.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'timer', true);
wp_reset_query();
?>
I am already using a loop earlier to pull all the posts and display the titles and a couple other custom fields above, which was causing a conflict when I originally was placing the Javascript inline with the HTML...
Long story short - help?
try this, not sure this is what you want.
<?php
query_posts();//get all posts
while ( have_posts() ) : the_post();
echo get_post_meta($post->ID,'timer', true);
endwhile;
wp_reset_query();
?>
I can't imagine why you would need JS which is inside a doc.ready call to be in the footer (it's going to wait for the DOM to be ready anyway), but you can use the wordpress add_action hook to get your content down there.
Looking at the docs, my guess would be something like this:
<?php add_action('wp_footer', function() { echo get_post_meta($postid, 'timer', true);}) ?>
You would do this inside of "the loop" so that it is run once for each post, and all the JS piles up in the footer.
each post will have a code snippet:
$j('#event-1').fancyCountdown({year:2011, month:9, day:31, hour:0, minute:0, second:0, timezone:0, dayDigitsAmount: 3, digits:{days:true,hours:true,minutes:true,seconds:true}});
while in your wordpress loop concat these values:
$countdown_script .= get_post_meta($post, 'timer', true);
then after the loop echo the script:
<script type="text/javascript">
$j(document).ready(function(){
<?php echo $countdown_script; ?>
});
</script>

Go Link thru iFrame

I have problem and I tried click link then it doesn't work to open link using target: name of iFrame. i dont want use href because im going make show/hide div.
Javascript:
<script type="text/javascript">
<!--//
function godirect(url, targetname)
{
document.getElementById(targetname).src = url;
//frame[targetname].location.href = url;
}
//-->
</script>
in HTML and PHP:
$a=0;
echo 'Click Me!';
echo '<iframe class="iframe_url" id="iframe_url'.$a.'"></iframe>';
How about
<script type="text/javascript">
function godirect(url, targetname) {
window.frames[targetname].location = url;
//OR
//window.open(url,targetname);
return false;
}
</script>
<?PHP
$a=0;
?>
Click Me!
<iframe class="iframe_url" name="iframe_url<? echo $a; ?>" id="iframe_url<? echo $a; ?>"></iframe>
You have to quote strings in JavaScript. You are trying to get the id of the element by passing in a variable which you haven't defined.
You are also using the same quote characters to delimit your HTML attribute value as you are using to delimit your JS strings.
To use the approach you are using, while making the minimum number of fixes to make it work:
echo 'Click Me!';
Using JS for this is a very silly idea in the first place though, and your implementation fails to have any kind of fallback for when JS is not available (which is odd, since you are taking steps to stop browsers which don't recognise the script element from rendering the JS as content text).
You can do this with plain HTML:
<a href="http://www.google.com"
target="iframe_url<?php echo htmlspecialchars($a); ?>">
Click Me!
</a>
i dont want use href because im going make show/hide div.
You can do that as well as having a normal, functioning link. Build on things that work.
Try this:
echo 'Click Me!';
echo '<iframe class=\"iframe_url\" id=\"iframe_url'.$a.'\"></iframe>';

Categories