<?php if( $fields )
{
foreach( $fields as $field)
{
$value = get_field( $field['name'] );
if ($value) {
echo '<dl>';
echo '<dt>' . $field['label'] . '</dt>';
echo '<dd>' . $field['value'] . '</dd>';
echo '</dl>';
}
}
}
?>
This is what I have. If I do a var_dump on acf_get_fields, it apparently sets the value to NULL. I could have known, as it's written here:
https://www.advancedcustomfields.com/resources/get_field_object/
The problem: I have to first get all fields in a specific field_group, hence I am using acf_get_fields.
I thought with using $field['value'] I could get it to work, but apparently this does not work.
Can someone help me to retrieve the values in the foreach per field? Surely there must be a way?
PS:
<?php
$fields = get_fields();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This gives me ALL fields. I just need a specific list of fields from a specific field group. That is the problem. Anybody knows how I can solve it?
<?php
$fields = get_field('fieldname');
foreach( $fields as $field)
{
echo '<dl>';
echo '<dd>' . $field . '</dd>';
echo '</dl>';
}
?>
Think the value is always NULL because you're getting it only the group fields, not the fields from a post. Have made a few examples maybe this will help you out.
$post_id = 123;
$group_id = 1234;
// Get post fields
$post_fields = get_fields($post_id);
foreach ($post_fields as $key => $value) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get group fields
$group_fields = acf_get_fields($group_id);
foreach ($group_fields as $key => $group_field) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get field objects
$field_objects = get_field_objects($post_id);
foreach ($field_objects as $key => $field_object) {
if ($key == 'my_custom_field') {
echo $field_object['value'];
}
if ($field_object['name'] == 'my_custom_field') {
echo $field_object['value'];
}
// only show fields from specific group
if ($field_object['parent'] == $group_id) {
echo $field_object['value'];
}
}
Related
I have two databases tables called article_category and article_case. They are supposed to be combined in a webpage as below:
Article category[title]
Article category[description]
Article case[title]
Article case[contents]
Article case[title]
Article case[contents]
This should repeat itself using foreach until all article categories are populated on the page.
Each article_case will also use foreach, but it has to respect its 'category_id' value which matches the 'id' in article_category .
I'm having trouble filtering the article_case table so that it only picks up rows from its parent 'category_id'.
This is my current php code (which only makes an array of each table):
$this->db->select( "id,title,description" );
$this->db->from( 'article_category' );
$query_article_category = $this->db->get_compiled_select();
$query = $this->db->query( $query_sustainability_category . 'ORDER BY sort ASC ' );
foreach ($query->result_array() as $val) {
$data['article_category_posts'][$val['id']] = $val;}
$this->db->select( "id,category_id,title,contents" );
$this->db->from( 'article_case' );
$query_article_case = $this->db->get_compiled_select();
$query = $this->db->query( $query_article_case);
$data[ 'article_cases' ] = $query->result_array();
And here is my html:
<?php foreach ($article_category_posts as $key => $val) { ?>
<section>
<h2><?php echo $val['title']; ?></h2>
<p><?php echo $val['description']; ?></p>
</section>
<?php foreach ($article_cases as $key => $val) { ?>
<section>
<h3><?php echo $val['title']; ?></h3>
<p><?php echo $val['contents']; ?></p>
</section>
<?php } ?>
<?php } ?>
The current setup will input all article_cases disregarding the category_id.
How can I use foreach while assigning each article_case to their respective article_cateogry?
This would generate unnecessary amount of queries to the DB server.
The rly basic approach would be:
select all categories (as you have) SELECT * FORM a_category
for each category select cases SELECT * FROM a_case WHERE category_id = {$category['id']}
The second one is to combine the categories and cases right in the query with join and adjust the resulting data in php:
SELECT cat.title, cat.description, case.id AS case_id, case.category_id, case.title, case.content
FROM a_case case
INNER JOIN a_category cat ON cat.id = case.category_id
You can use a query to get all the records of article_case with related article_category.
group the query result by category_id.
$this->db->select("cat.title,cat.description,case.category_id,
case.title AS case_title, case.contents" );
$this->db->from( 'article_category cat');
$this->db->join('article_case case','case.category_id=cat.id');
$result=$this->db->get()->result_array();
$result_group = array_group_by($result, 'category_id');
array group by function
function array_group_by(array $array, $key)
{
if (!is_string($key) && !is_int($key) && !is_float($key) && !is_callable($key)) {
trigger_error('array_group_by(): The key should be a string, an integer, or a callback', E_USER_ERROR);
return NULL;
}
$func = (is_callable($key) ? $key : NULL);
$_key = $key;
// Load the new array, splitting by the target key
$grouped = [];
foreach ($array as $value) {
if (is_callable($func)) {
$key = call_user_func($func, $value);
} elseif (is_object($value) && isset($value->{$_key})) {
$key = $value->{$_key};
} elseif (isset($value[$_key])) {
$key = $value[$_key];
} else {
continue;
}
$grouped[$key][] = $value;
}
// Recursively build a nested grouping if more parameters are supplied
// Each grouped array value is grouped according to the next sequential key
if (func_num_args() > 2) {
$args = func_get_args();
foreach ($grouped as $key => $value) {
$params = array_merge([$value], array_slice($args, 2, func_num_args()));
$grouped[$key] = call_user_func_array('array_group_by', $params);
}
}
return $grouped;
}
in view you can show as below
<?php foreach ($result_group as $key => $cat_result) { ?>
<section>
<h2><?php echo $cat_result[0]['title']; ?></h2>
<p><?php echo $cat_result[0]['description']; ?></p>
<div class="box-bd-doted">
</section>
<?php foreach ($cat_result as $key => $val) { ?>
<section>
<h3 style="color:red"><?php echo $val['case_title']; ?></h3>
<p><?php echo $val['contents']; ?></p>
</section>
<?php }
}
?>
Is this the right way to get ID dynamic within $_POST?
if( isset( $_POST['episode_title '. $episode_ID .''] ) ){
$episode_title . $episode_ID = $_POST['episode_title '. $episode_ID .''];
}
using '. $episode_ID .' to get id dynamic.
If not, then does someone know how? thanks.
$posts = $_POST;
foreach( $posts as $key => $value)
if(preg_match('/episode_title (\d+)/', $post))
{
// rest...
}
}
I am new to php and i have a small question.
This is my array
$cars=array( "1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars']=$cars;
the elements 1234, 2468 and 4587 are basically registration numbers of the cars and my task is to insert these registration number in a table.
if( isset($_SESSION['cars']))
{
foreach($_SESSION['cars'] as $key)
{?>
<tr><td><?php echo $key?></td></tr>
this is wht i did but it gives me an error saying Notice: Array to string conversion.
can anyone tell me how to do this ? I'll be grateful
This should work for you:
if( isset($_SESSION['cars'])) {
foreach($_SESSION['cars'] as $key => $v)
echo "<tr><td>" . $key . "</td></tr><br />";
}
EDIT:
It should work see this example:
<?php
session_start();
$cars = array(
"1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars'] = $cars;
if( isset($_SESSION['cars'])) {
foreach($_SESSION['cars'] as $key => $v)
echo "<tr><td>" . $key . "</td></tr><br />";
}
?>
Output:
1234
2468
4587
<?php
$cars=array( "1234"=> array("Toyota","100","2","white"),
"2468"=> array("Mazda","1000","0","red"),
"4587"=> array("Mercedes","200","0","green")
);
$_SESSION['cars']=$cars;
//In separated file
if(!isset($_SESSION)) session_start();
if( isset($_SESSION['cars']))
{
foreach($_SESSION['cars'] as $key => $value)
{
echo "<tr><td> $key </td></tr>";
}
}
?>
Controller:
$categorys = array(
'1234' => array('Car Audio','Car Subwoofers'),
'12' => array('Car Stereos')
)
$category_id = $this->input->get('category_id');
$product_id = $this->input->get('modelNumber');
if (array_key_exists($category_id,$categorys))
{
foreach($categorys[$category_id] as $key => $value)
{
echo $value;
}
}
How can I echo the $value outputted from the foreach statement in my view file?
You can pass the whole array to the view and run the foreach directly in the view, e.g.:
$data['array'] = array(/* etc. */);
$this->load->view('view', $data);
And in the view:
<?php foreach($array as $key => $value): ?>
<p>The key is <?php echo $key; ?><br>
The value is <?php echo $value; ?></p>
<?php endforeach; ?>
Controller
if (array_key_exists($category_id,$categorys))
{
$query['cats'] = $categorys[$category_id];
}
View
foreach($cats as $key => $value)
{
echo $value;
}
This worked perfectly in a previous version of wordpress, different site tho. I have a custom field called flash on several wp pages but it keeps defaulting to "printed if results are empty". Any ideas on why this wouldn't work?
<?php
if ( function_exists('get_post_custom_values') )
{
$mykey_values = get_post_custom_values('flash');
if(count($mykey_values) > 0)
{
foreach ( $mykey_values as $key => $value )
{
echo "$value";
}
}
else
{
//printed if results are empty
echo("mainPage.swf");
}
}
else
{
//printed if function doesn't exist
echo("mainPage.swf");
}
?>
Hi not really sure why that is not working (as long as you are sure you do have custom fields with 'flash' values in the post). Anyhow, try the following, I am sure the following will work.
<?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['flash'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>