Using Meta Box Plugin to assign URL to a variable - php

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(); ?>

Related

Using PHP variable within ACF repeater field

When using ACF (Advanced Custom Fields) repeater fields, the markup should look like this to initiate the loop.
<? if( have_rows('my-repeating-field) ): ?>
<? endif ?>
However, I would like to make this dynamic, meaning I use another PHP variable as part of the if() code, something like as follows:
<? $variable = get_field('my-variable-field'); ?>
<? if( have_rows("'" . $variable . "-repeating-field" . "'") ): ?>
<? endif ?>
However, this is not working. Is there a way I can do this with ACF/PHP? Seems pretty simple to me?
The problem is that you're concatenating the string in a way which would cause the end value being passed to have_rows to have quotes inside of it. You only need to concatenate the value and the end of the string, and that should be enough.
have_rows($variable . '-repeating-field')

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

Advanced Custom Fields in Wordpress - "if" statement troubles

I was hoping somebody may be able to help me. I'm having trouble with if statements with the Advanced Custom Fields plugin for Wordpress. I've got three options the user can choose from, all three can be chosen, but they can also choose just one if they wish.
The issue I'm having is the code I've written displays all of the HTML tags, even the empty ones. This is causing styling issues. I want to be able to just show HTML that has been populated. I've tried the solutions on the ACF forums but to no avail.
Link: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Here's the quick (newbie!) code I've got at the minute:
<?php the_sub_field('link'); ?>
<?php the_sub_field('doc'); ?>
<p><?php the_sub_field('cap'); ?></p>
I looked on the ACF forum and tried this, but it broke the theme:
<?php if(the_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(the_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(the_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
I'm looking for some help to make this work. I don't think I'm too far away from the right answer, however I'm a bit of a rookie with anything beyond standard front-end stuff, any thoughts would be very much appreciated.
Thanks!
Try to use get_sub_field();
<?php if(get_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(get_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(get_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
When looping through one of these fields, this function returns a sub field from the current row.
Like Dk-Macadamia said, try to use get_sub_field() in loops instead of the_sub_field()
the difference is get_sub_field() return the value as a string, and the_sub_field() print the data,
Also get_sub_field() only work under a repeater/ fluid field type otherwise wont work,
if its not a sub field of repeater/fluid fields just try get_field()

Wordpress: Condition Tags PHP

This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.

Using subsrt() with method the_title()

I'm trying to substring the title which it is called via the method the_title() .
Here are 2 things I did to do it but they both failed.
first try: <?php echo substr(the_title(),1,15) ?>
second try: <?php $new_title = the_title() ; echo substr($new_title, 1,15) ?>
They both didn't work. when I used the second try I still get the full title.
Note: I'm trying to implement this on a Wordpress script, also it's for my php practice.
Thanks in Advance.
The function the_title() does not return the title as string but outputs it by default.
Use the_title('','', 1) which makes it return the title or the alternative function get_the_title() instead.

Categories