I'm trying to implement the following guide to my custom wordpress theme http://tomsbigbox.com/wordpress-load-more-posts-on-page-scroll/
Unfortunately I have problems to include metabox and term values into the PHP variable.
How can I add/echo a variable inside of another variable? I hope I'm clear enough.
<?php
// .. wordpress function ... (have_posts()) : while (have_posts()) : the_post();
// Metabox
$email = get_post_meta( $post->ID, '_rsd_email', true );
// Taxonomy Term
$city = get_rsdt_terms('city');
$item = '<li>
<a id="itemn-'. get_the_ID() .'" href="'. get_permalink($post->ID) .'" title="'. get_the_title() .'">
<span>
<div class="role sixcol">
<h3>'. get_the_title() .'</h3>
<h4>'. $email .'</h4>
<span></span>
</div>
<div class="location threecol"><span>'. $city .'</span></div>
</span>
</a>
</li>';
// wordpress .....
?>
The simplest answer to this is to add some debugging statements to your script to output the contents of the variables. The variables are fully accessible within the scope of the script unless they are encapsulated within a function or class.
The most commonly used statement is to pass the variable into the print_r() function. So you would do something like "print_r($city);" to get the contents of that.
I, personally, like to use a slightly more extensive version of that command that ensures it outputs the results in a cleaner, more easily read fashion. Here is what I normally write.
echo "\n<br>\n<pre>" . print_r($var, true) . "</pre>\n<br>\n";
The tags ensure that the browser renders the spaces and line breaks from the output. The "\n" and that you see makes sure that it does both browser rendered line breaks before and after and source output line breaks.
You can do this with pretty much any variable type. The only times I have run into an issue with the output of a variable is if the variable is actually (or contains) a resource. An example of that is when outputting SimpleXML variables.
Related
I am using the repeater field of Wordpress' advanced custom fields to make the following content dynamic on my site.
Image of how the content looks while marked up statically
This is how I have marked up the content using plain html.
<div class="container">
<h2>A few general rules of thumb</h2>
<p><span>1.</span>Hallways and transitional spaces are best painted...</p>
<p><span>2.</span>It is best to keep expensive furnishings...</p>
<p><span>3.</span>If you want furnishings and accessories...</p>
<p><span>4.</span>Neutral furnishings and accessories?</p>
</div>
The paragraphs have been styled by setting their display to flex so that the span (number) is on the left and the paragraph is on the right.
This is how my code now looks with PHP.
<div class="container">
<h2><?php echo $container3title['title'];?></h2>
<?php
// Check rows existexists.
if(have_rows('rules')):
// Loop through rows.
while(have_rows('rules')) : the_row();
// Load sub field value.
$ruleNumber = get_sub_field('ruleNumber');
$ruleDetails = get_sub_field('ruleDetails');
// Do something...
echo '<span>' . $ruleNumber . '</span>';
echo '<p>' . $ruleDetails . '</p>';
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
?>
</div>
The problem is that I do not know how to echo the php so that the paragraphs aren't displayed as two block elements that are stacked on top of one another. I want it to be marked up the same way as I have marked it up using plain html so that my CSS will style it accordingly.
I tried the following but it didn't work.
echo '<p>' '<span>' . $ruleNumber . '</span>'; . $ruleDetails . '</p>';
Can someone please tell me how to put this together correctly? Thank you
Just store the span in a variable and concatenate it in:
$span = '<span>' . esc_html($ruleNumber) . '</span>';
echo '<p>' . $span . esc_html($ruleDetails) . '</p>';
I threw some escapes in there, too. It is generally recommended to always escape user-generated input when outputting, just in case. And even though you might “know” that the number is always numeric, it doesn’t really hurt to escape it anyway, and it makes it easier to scan for unescaped values.
I've come across a situation where there's a gnarly mix of HTML and PHP (well at least it seems that way to me because I'm not an expert in PHP). Currently, there's a hard-coded URL that I'd like to generalize using a PHP function. However, this is where I'm running into issues as this mix is getting rather complex.
After spending over 2 hours on this, I think I'm at a point where looking through topics on this doesn't seem to discuss this particular use case, and lots of trial-and-error isn't yielding the desired results.
Inside my template, I have the following code for my sidebar:
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<?php
// First attempt HTML in PHP
$home_url = get_home_url();
echo '<div class="field-title"><a class="field-value rating" target="_blank" href=' . $home_url . '/rating-criteria/' . '>Rating:</a> <span class="rating">' . the_field('rating') . '</span></div>'; ?>
// Second attempt PHP in HTML
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php the_field('rating'); ?></span></div>
// The hard-coded URL that I'm trying to generalize
<div class="field-title"><a class="field-value difficulty" target="_blank" href="https://s1.temporary-access.com/~allacros/sandbox2/difficulty-criteria/">Difficulty:</a> <span class="difficulty"><?php the_field('difficulty'); ?></span></div>
...
The results of this code can be seen in the sidebar in:
https://s1.temporary-access.com/~allacros/sandbox2/california-switzer-falls.html
In that sidebar (beneath "About Switzer Falls" below the Hero Image), you can see 2 Ratings.
The first one has the correct link, but the formatting is off as the "2" is not where it's supposed to be and it's unformatted.
The second one has the correct formatting, but it has the incorrect link.
Anyone know what I'm doing wrong?
Thanks
For the first line, looks like there is "echo" statement in your function the_field("rating"), which cause the rating "2" output first before your "echo" execute.
For the second line, there is error at "$php", which should be "?php".
Change your 2nd statement to the following:
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php echo the_field('rating'); ?></span></div>
In addition to what others have mentioned about the $ where a ? should be, you could maybe even simplify things for yourself by doing something like this for the link. All you've got to do is echo the $home_url variable you're creating at the start.
link
It's quite simple, really. There's a number of things you are doing on one single line that you don't actually need to do...
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<div class="field-title">
<a class="field-value rating" target="_blank" href="<?php echo home_url(); ?>/rating-criteria/">Rating:</a>
<span class="rating"><?php the_field('rating'); ?></span>
</div>
Now, things to note... I split the HTML over multiple lines instead of trying to keep it on just one. I have also removed the bulk of it from PHP processing altogether, as the static HTML doesn't need to go through PHP. This cleans it up immensely.
Lastly, if you look back at your code, you were echoing out the return value of the_field. This is where your random '2' is coming from, most likely. the_field should be outputting the value itself, so you should not concatenate it's return value on then output it.
I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>
This code must be used to output my images in a wordpress layout:
<?php
$temp_content = explode(" ",substr(strip_tags(get_the_content()),0,175));
$temp_content[(count($temp_content)-1)] = '';
$new_content = implode(" ",$temp_content);
?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('home-post',
array('alt' => 'post image',
'class' => '', 'title' =>
'<div class="home_post_content">
<h4>
<a href="' . get_permalink() . '">
' . get_the_title() . ''. $the_date .'
</a>
</h4>'));
I want to put the_field('event_date') in just after get_the_title. However whenever I do, it merely outputs the date either above or below the code, with absolutely no formatting around it.
You've got a host of issues going on here depending on your css. Remember headings are block level elements (they take up a whole line) and anchors can be too. I would map out the html I'm trying to create and determine what needs to be inline and sort out the parent/child relationships. I'm not sure this is an issue with Wordpress output.
Post the html that is being outputted and the relevant css.
You should return the value:
get_the_field( 'event_date' )
Trying to concat these PHP expressions into LI's but it's getting rendered as the php first and then the LI's...
function getVolArchives() {
query_posts('category_name=volunteerspotlights&showposts=5');
while (have_posts()) : the_post();
echo '<li>' . the_title() . '</li><li>' . the_date('F') . '</li>';
endwhile;
}
Obviously, it doesn't work like I thought it should...can I get some help?
What happens is that the TITLE and DATE show up and THEN two blank LI's... but the answer was already given to me below. I was using the wrong Wordpress functions.
You need to use get_the_title() and get_the_date() instead. In Wordpress, the get_*() versions of functions RETURN their data. Otherwise WP defaults to outputting the data instead.