Wordpress PHP within PHP - php

I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.

Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/

You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

ACF page while loop breaks footer while loop

So basically i have a main page and a footer page. Both are seperate .php files.
Im using ACF for this site.
Following the documentation, ive created a while loop for my 'flexible content' in the main page and it works, displaying all the data that gets looped and hooked from the CMS input fields.
My Problem is in the footer, i have a while loop that displays links, but it wont display unless i remove the while loop from the main page, then the links display in the footer.
I honnestly dot get why this happens ive tested allot and get my head wrapped around this, please help.
Main page code:
<?php
// check if the flexible content field has rows of data
if( have_rows('flexible_content_field_name') ):
// loop through the rows of data
while ( have_rows('flexible_content_field_name') ) : the_row();
// check current row layout
if( get_row_layout() == 'gallery' ):
// check if the nested repeater field has rows of data
if( have_rows('images') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
echo '<li><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></li>';
endwhile;
echo '</ul>';
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
<?php get_footer(); ?>
Footer Code:
<div class="links">
<?php
if( have_rows('footer_page_links', 'option') ):
var_dump("test");
while( have_rows('footer_page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php wp_footer(); ?>
I would just like to add , not even the vardump() displays in the footer if main page while loop is implemented so it never gets inside the footer loop. The footer uses ACF option page - > LINK
Also all other option fields in footer displays, if its not within the while loop. I have removed the main page while loop then the footer while loop works, and this only happens with flexible content, my other pages with loops, that does non consists of flexible content works perfectly.
So This issue is resolved on my side, after contacting the ACF guys, they made a duplicate of what i did and could not recreate my issue.
Which got me thinking since i had the latest Wordpress (Version 4.9.7) the only difference is the hosting.
What i used on localhost was XAMP Version 3.2.2, which i did not think was the problem, but it was, so upgraded to a live server and everything works as expected, so for future ref should you run into these simple unexplained code errors, check the hosting, or upgrade.

PHP - Do this, and if the next part meets this criteria also do this

I'm quite new to PHP, but have no idea how to word this question.
I want to return a coloured circle if the colour has been selected elsewhere.
So if silver has been ticked, show a silver circle.
If silver and gold have been ticked, show both.
There must be an easier way to code the below, instead of having to create lots of seperate if statements and call a variable each time?
elseif's wouldn't work here from my understanding.
Forgive me for my naivety!
The code I've written below:
<?php
// for iphone
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('silver', $colours_available) ): ?>
<div class="colour-circle-title"><p>Silver</p></div>
<?php endif; ?>
<?php
// vars
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('gold', $colours_available) ): ?>
<div class="colour-circle-title"><p>Gold</p></div>
<?php endif; ?>
Thanks so much for any help!
As mentioned in comments, you can iterate through your $colours_available array, and then use ucwords() to camel-case your p-tag's text:
<?php $colours_available = get_field('iphone_colours_available'); ?>
<?php foreach($colours_available as $color) :; ?>
<div class="colour-circle-title"><p><?php echo ucwords($color); ?></p></div>
<?php endforeach; ?>
Note: If you don't need to use the $colours_availble array elsewhere, you could eliminate that line and replace it wtih get_field('iphone_colours_available') in the loop. Also, this solution does not address sorting, which your previous solution does. For that, I would recommend looking at PHP's sorting functions, specifically uasort, uksort, and usort

Woocommerce / Sensei overwrite a variable or function in child theme

I'm working on a website that is using Woocommerce (and its plugin Sensei, which supports courses, lessons, quizzes, etc.). I'm running into trouble changing a small piece of info.
Within the Sensei plugin there if a file (woothemes-sensei > includes > class-sensei-course.php). In this file, there is one line of code that I want to change without editing the core file. I understand that I probably need to do this in my child theme's functions.php files, but I'm not quite sure what to do. I need to change the $course_page_url to a custom url in the following code:
<div id="active-courses">
<?php if ( '' != $active_html ) {
echo $active_html;
} else { ?>
<div class="sensei-message info">
<?php echo $no_active_message; ?>
<a href="<?php echo $course_page_url; ?>">
<?php _e( 'Start a Course!', 'woothemes-sensei' ); ?>
</a>
</div>
<?php } // End If Statement ?>
</div>
Can anyone tell me the best way to go about doing that? I don't know how to hook into a filter to change that url.
And the entire class-sensei-course.php file can be found here. The code that I included above begins on line 1596.

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>

Categories