i got the following php. and what i want is this loop to exclude specific post ids
The following field will generate the id i wish to exclude
<?php the_field('excludeid'); ?>
(this field basicly checks if a checkbox is marked on the page, and if it is, it will output the page id)
So if it checks page id 701, checkbox is marked, it will output 701 (and otherwise it outputs nothing)
so in the loop i want to check if the page it loops for, matches with that output, if it does, it needs to skip it
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
Do something, except when page id is equal to <?php the_field('excludeid'); ?>
<?php endwhile; ?>
Edit, this is what i have now:
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php if( $post->ID == 686) continue; ?>
<?php endwhile; ?>
That works, but when i change the 686 to "the_field('excludeid')" (which outputs 686) it doesnt work.
I am a total newb in php, so manybe i am doing something dumb here XD
while cycle is a type of conditional, but in loop, you can set any type of logic inside its arguments. In this case you iterate about $parent->have_post()
while($parent->has_post() && (!in_array($parent->the_post()->id, the_field('excludeid')) {
// your logic
}
I don't know how is your business code, but is that, you must know that while is that a conditional, but it loop
Related
I'm creating a WordPress website using the Advanced Custom Fields plugin. I have a set of fields for tennis score results. In the template, I'm showing these fields like this:
<?php if(get_field('my_field')) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The problem is that some scores are zero, so they're not showing up. I understand that this is because 0 basically equals null, so the statement is false.
One solution I found and tried was this:
<?php if(get_field('my_field') !== false) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
However, this means that empty fields now show up too, which is not desirable since there are a lot of fields that are intended to be hidden if empty.
So, my question is, is there a way to phrase an if statement that allows for zeros, while still returning false if the field is empty? Please note that some scores aren't purely numeric, with values like '6(1)'.
In order to check for empty strings you have to explicitly check them in your if condition.
<?php if(get_field('my_field') !== '') : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The reason is 0, null, empty string, empty array all evaluate to (but are not exactly) false, in case of a boolean check.
I'm trying to use get_field in a loop to retrieve some custom field values but
when using get_field('container', post_id) the value is always empty.
I tried to even use it inside the block file and the same happens.
$container = get_field('container'); // this works
$test_container = get_field('container', 144); //this returns empty, post id === 144
Did you try the_field() function.
In ACF the_field() function is used to display the value of a specific field. it is same as get_field().
https://www.advancedcustomfields.com/resources/the_field/
<?php if( get_field('container') ): ?>
<p><?php the_field('container', 144); ?> </p>
<?php endif; ?>
The code you using should work but if it's not working for any reason. You can also use
get_post_meta(); //to retrieve the value.
I'm modifying some code but I'm not an expert on php level and need some help :)
<div class="emd-main emd-main-<?php echo $emd_state; ?>">
<?php if ( $userpro->memberlist_in_search_mode($args) ) { ?>
<?php $arr = $userpro_emd->users( $args );
if (isset($arr['users']) && !empty($arr['users']) ) {
?>
<?php if (isset($arr['paginate']) && $args['emd_paginate'] && $args['emd_paginate_top'] == 1) { ?>
<div class="userpro-paginate top"><?php echo $arr['paginate']; ?></div>
<?php } ?>
<div class="emd-list" data-layoutmode="<?php echo $args['emd_layout']; ?>">
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
<?php $tk_image_1 = get_field('foto_1', 'user_'. $user_id); ?>
<?php if (!empty($tk_image_1)) { ?>
<div class="emd-user">
I have the following issue; I have a page that shows a grid with members. The number of members per page is 20 (that is set within the plugin settings). The foreach is starting with this:
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
After that I check if the value $tk_image_1 is not empty, if not empty then go on. So far so good.
The only thing now is; when I have 20 members on a page and for 8 of them the $tk_image_1 is empty then it shows 12 members on that page... I think it's something in the array that counts them before checking the $tk_image_1 value.
What is need is to show 20 members per page and only the one if $tk_image_1 is not empty.
Can someone help me with this?
Many thanks!
Regards,
Robert
The source of your problem is that you're working with a set of results that contains users you don't want to show. To properly correct this, you need to alter the query that's taking place in this code fragment: $userpro_emd->users( $args )
That ->users() method may accept additional $args that would allow you to say, "only users who have foto_1". That way you're not trying to perform magic in the loop, and then be left with the problem of not having enough others to make up for those who were missing foto_1.
Have been reading through multiple similar questions and went over my syntax many times, but I can't figure out why my PHP code is executing both conditions.
I'm trying to replace the url of an element with the string from a custom field if the field is not empty. If the field is empty, I want to output the permalink normally. What happens is that the code concatenates both the string from the custom field and the permalink when the field is not empty. If i remove the string in the field, it works fine.
<div class="profile-content">
<a href="
<?php
if ( the_field('direct_resource_link') != '') {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
Thanks!
Dan.
EDIT after comment from original poster
My initial assessment (left below for reference) was correct. You are using function that will print/echo content instead of returning it. Your if will always evaluate to false, because you are calling function that returns nothing; and PHP thinks that nothing and empty string are the same thing.
You didn't see that when field was empty, because the_field() for empty field printed empty string (or nothing at all), i.e. it didn't modify value printed by the_permalink() in any way/
According to ACF documentation, the_field() is accompanied by get_field() which returns value instead of printing it.
Your code should look like that:
<div class="profile-content">
<a href="
<?php
if ( get_field('direct_resource_link') ) {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
My initial post
What happens is that you run function the_field('direct_resource_link') and compare it's return value to ''; if that value is empty, you run the_permalink($id);.
It's hard to tell what the_field() is and what it is supposed to do, but I guess that it prints value instead of returning it. So if field is empty, it prints nothing, resulting in pure run of the_permalink(). If field is not empty, it prints it content and returns nothing. Since nothing is equal to empty string, PHP proceeds with else branch and invokes the_permalink() that prints additional info.
Solution: modify the_field() to return value instead of printing it, or make additional function that will query for value and return it, and use that function in if statement.
Miroslaw Zalewski already answered your question here, so this is simply to show you the kind of code needed to fix your issue:
function get_the_field($field) {
ob_start();
the_field($field);
return ob_get_clean();
}
This code will start an output buffer (which will capture all echo'd data), run the_field and return (and delete) the output buffer (the echo'd data from the_field). This means you can simply do the following:
...
<?php
$theField = get_the_field('direct_resource_link');
if ( $theField != '') {
echo $theField;
} else {
the_permalink($id);
}
?>
...
This can all be simplified. the_field() echoes a meta value. You don't want that...Instead, you want to return the value to check it, before conditionally echoing it. You can do this using get_field().
In the simplest form, your final code would look like the following:
<a href="<?php get_field('direct_resource_link') ? the_field('direct_resource_link') : the_permalink($id); ?>">
<h5><?php the_title(); ?></h5>
</a>
I have a site based on wordpress. I need to allow people to create posts from frontend so I have made a multi-part form which works pretty well. There are three parts of the form and each part of the form is validated before moving to the next part. Data is passed to another page through hidden inputs.
My form template looks somewhat like this ( complete code is pretty massive and irrelevant here, so just showing just the relevant parts ) which I hope is enough to give an idea how the form works.
MULTI-PART FORM WP-TEMPLATE SAMPLE CODE :
<?php
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL ) { ?>
<?php include_once('multiparts/form-files/first_part.php'); ?>
<?php } else if ( $page == 1 ) { ?>
<?php include_once('multiparts/validation/validate_first_part.php');
if (isset($_POST['submit-1']) && (!empty($error))) { ?>
<div class="error-head">Cannot continue registration. Error/s highlighted below.</div><br/>
<?php echo $error . '</br>'; ?>
<?php } else {
include_once('multiparts/form-files/second_part.php');
}
?>
<?php
} else if ( $page == 2 ) { ?>
//SO ON AND SO FORTH
<?php
}
?>
Recently, I have a added several checkbox fields in the form with an intention to display values of selected checkboxes, in the created posts. So here is a relevant html form code that I am currently using.
<fieldset class="work-areas">
<label for="areas" class="label">INTERESTED IN :</label></br>
<div class="work-class">
<input type="checkbox" name="workareas[]" value="administration"/>administration</br>
<input type="checkbox" name="workareas[]" value="technical"/>technical</br>
<input type="checkbox" name="workareas[]" value="creative"/>creative</br>
<input type="checkbox" name="workareas[]" value="fieldwork"/>fieldwork</br>
<input type="checkbox" name="workareas[]" value="marketing"/>marketing</br>
</div>
</fieldset>
I insert the values of these checkboxes into the post just like any other custom fields using this code: add_post_meta($pid, 'areas', $workareas, true );. In the processing part it is assigned a meta_key areas. I display it in the single.php with the code below :
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>
ISSUE: All this works well when the above given html form code for checkboxes is in the last/third part of the form. But it does work when the same checkbox fields is in the second part of the form. I guess it simply does not pass the array values of the selected checkboxes to the third part. Print_r shows an empty array and obviously does not display anything in the single.php too. Although I understand that the trouble is just the array of selected checkbox values, NOT being carried to the third part properly, I need help as I am noob to all this.
So the bottomline question is how do I save the array of the selected
checkboxes' values in the second part and carry it to the third part
and finally assign it to a variable which will hold the array values.
That which can be displayed in post using the code above.
THINGS TRIED : I have looked into this thread here and I am confused where I will insert my checkbox fields and not even sure it applies to my situation. I have been able to pass other text input values from one part to another part of the from using something like this :
<input type="hidden" name="eligible" value="<?php echo $eligible;?>" />
So, I tried using name="workareas[]" but did not work. I am doing print_r() for everything I am trying and till now have only been getting empty array. I am still going through tons of other threads looking for possible hints. In the meanwhile if you can help, that will be great. Thanks in advance.
UPDATE : Solved, please check the answer.
Not a WP user but your checkboxes are named "workareas" but you seem to refer to them as "areas" everywhere else.
Thanks for the suggestions in the comments but I have found a graceful and robust solution in my opinion, that is using sessions. I followed the basic idea from here which actually deals with a multipart form with sessions. Now I have the following code in my third/last part of the form, at the very beginning of the document. At this time please remember that the html checkbox fields as illustrated above in the question are in the second part.
<?php
session_start();
$_SESSION['workareas'] = $_POST['workareas'];
$result=$_POST['workareas'];
?>
The code is that is repeated during the validation of the third part is the same but this way the variable $result is still holding the values of the array of the selected checkboxes from the second part of the form.
session_start();
$result=$_SESSION['workareas'];
You can do a print_r($result) at this point and check. Furthermore, if you want to insert these array values to post in order for them to show up in the post just assign meta_key eg. areas to $result and use the code below to add it as a custom field :
add_post_meta($pid, 'areas', $result, true);
In the single.php you can use the code below to pull values from the array and show. Do not avoid if(is_array) statement else wordpress might throw an error warning: invalid arguments supplied foreach(). Goodluck.
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>