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>
Related
I am trying to display certain text and apply a filter on one specific page of a wordpress site. I am using the code below, but it is not working. Specifically, it doesn't look like the if statement is properly capturing the web page and passing the resulting code through. Any tips for how to fix this?
The page in the wordpress site that I want to apply this code on is called "Predicting Renewals". It is currently listed as private, not public. I want to make that clear just in case that is part of the problem.
<?php
global $user_select_name;
global $_POST;
?>
<?php if ($_POST == 'Predicting Renewals') { ?>
<script>
$('#current-view').html('<center><h4 style="margin-bottom: 0px; margin-top: 0px;"><?php echo $user_select_name ?> Chapter</h4></center> <hr>');
</script>
<?php } ?>
You need to check is_page() using the slug or page ID.
<?php global $user_select_name; ?>
<?php if (is_page('predicting-renewals') ) { ?>
<script>
$('#current-view').html('<center><h4 style="margin-bottom: 0px; margin-top: 0px;"><?php echo $user_select_name ?> Chapter</h4></center> <hr>');
</script>
<?php } ?>
Documentation for is_page()
https://developer.wordpress.org/reference/functions/is_page/
If this is not a page, but a normal post, you can use is_single() and pass the same slug or id.
how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>
I'm using Coupon Creator and I like that it comes with the categories and shortcodes. I am using a category shortcode like this example [coupon couponid="loop" category="Brumby's Bakery" couponorderby="date" coupon_align="cctor_alignnone" name="Coupon Loop"].
I wanted to use different category shortcode for each page. Therefore, I kinda created a custom meta box for that (Wordpress Meta Boxes) and called it in the php template page with <?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>. However, the shortcode won't work in php template page unless I use <?php echo do_shortcode('[coupon...]'); ?>
In the end this code which was supposed to be like that wouldn't work because of php in php.
<?php echo do_shortcode(<?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>); ?>
How do I rework this php code? The only thing I could think of is to use something like var but I don't know how to make it work in php template page.
var $store = <?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>
<?php echo do_shortcode($store); ?>
No need to echo get_post_meta(). This should work:
<?php echo do_shortcode(get_post_meta($post->ID, 'couponshortcode', true)); ?>
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>
I'm having an issue with ajax loading wordpress content with jQuery.
It works just great, except the line returns aren't being parsed.
It's being pulled in with json, and stuck into two different divs.
jquery code:
$('.load').on('click',function(e){
e.preventDefault();
var person_clicked = $(this).attr('id');
$.post('/load.php', { id:person_clicked, callback:'person' } )
.done(function(data) {
alert(data);
$('.page_tagline').html(data.title);
$('.left_content').html(data.content);
scrollToElement($('#hero'));
});
return false;
});
load.php:
<?php
/* Send as JSON */
header("Content-Type: application/json", true);
require('../../../wp-load.php');
?>
<?php query_posts('p='.$_POST['id']);
$post = $wp_query->post;
?>
<?php if (have_posts()) : the_post(); ?>
<?php
$returned = array(
'title' => get_the_title(),
'content' => get_the_content(),
);
?>
<?php
echo json_encode($returned);
?>
<?php endif;?>
When I alert it out, the content looks as if the paragraph breaks are in there. There's a definite spacing between the paragraphs. But when it lands in the div it's just a giant run-on sentence.
Also, when I load it normally the spacing is there. I tried removing the json part of the ajax, and just loading the whole thing into a div, and the spacing remains so this is for sure a json thing (as far as I can tell). when doing this, the code outputs /r/n/r/n where the new paragraph should be, which is exactly what wordpress does.
Any ideas on how I can make json retain the paragraphs?
You don't get the automaticly conversion from rowbreaks to p-tags as the function wpautop() isn't run.
its a filter on the_content. if you want the get_the_content() to have same formating as the_content()
you need to pass it thorugth the filters:
apply_filters('the_content',get_the_content())