Custom Field’s
1) Name: Featured-Blog and Value: 704 (704 is the postID)
2) Name: Featured-Blog and Value: 699 (699 is the postID)
Code being used to display a link to each of the posts. (can only get one of the custom fields to display)
Screenshot of output
Screenshot
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<a href="<?php the_permalink();?>"><p class="caption"><?php the_title(); ?></p></a>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
Now below is an older code that I was originally trying to use, but didn’t end up using. This one actually does pull both of my “Custom-Fields”. You can see it’s obviously coded different because you can see it says “Title” instead of the posts title. But I am just using this code as an example to show you that more than one “Custom Field” can be displayed, unless there is an easy fix for the code below?. Maybe some of the code form that can be incorporated into my working script above. Both the above code, and this bottom one are very close to what I’m trying to do. It seems like one, has something the other one needs.
Screenshot of output
Screenshot
<div id="related-posts">
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
]]>Mainsite -> query post in subsite -> display in main site
I was thinking about writing a custom query for the specific table wp_2_posts but can not find a good example. Not even sure if this is possible.
Thanks
]]>I’m playing with the ‘add_meta_boxes’ to understand how it works and how different it is from custom field
when I look at the database, it seems both are the same value
it is only a difference visual aspect on the post page
I have seen there is also different function, but to me they return the same
let say I have add an extra box named ‘price’
what is the difference between
$custom = get_post_custom($post->ID);
$price = $custom["price"][0];
and
$price2 = get_post_custom_values('price',$post->ID,);
and
$price3 = get_post_meta($post->ID, 'price', true);
to me they pull out the same date, is there performance difference? is one better than the other to use? if I have several ‘custom field’ to retrieve, is that best performance white to use get_post_custom? is that generating only one query while get_post_meta make a query per request?
thank you in advace
Steffy
]]>Warning: Invalid argument supplied for foreach() in /home/.sites/4/site38/web/wp-content/themes/NATOW_theme/single.php on line 54
Here is the questionable code that was working up until I upgraded:
<?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['didYouKnow'];
foreach ( $my_custom_field as $key => $value ) // this is 'line 54' that breaks the script
echo '<li>' . $value . '</li>';
?>
I’ve tried a few different things and nothing worked.
]]>On some homepage items of my theme, the publisher will be able to change the background colour of the div to make the info stand out.
To achieve this, I created a custom field for the posts named “change_colour”.
In the loop, I test the custom field, which is either empty or contains the word “black”:
<?php [bla bla] the_post();
$MAMA = get_post_custom();
$DADA = $MAMA['change_colour'];
if ($DADA='black'):
[ CSS CHANGE ] ; [endif etc]
Hmm. My PHP works but results are not as expected. Everything turns black. Doesn’t work.
I php-[echo]ed the variable to check: it just says “Array”.
Damn. What am I missing ?
]]>I’m experiencing some very weird array indexing problems for get_post_meta and get_post_custom.
In short, I want to have a post where I can enter multiple custom fields of the same type(name)and then extract the value of each and display them on a page in the order they were entered (in my case, the homepage).
I should also mention I am doing this outside of the Loop.
Instead of receiving an array that contains the custom field values in the same order I entered them in the admin area, the entries are mixed.
I tried this:
<?php
$custom_fields = get_post_custom(244);
$my_custom_field = $custom_fields['field_title_here'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>
and this
<?php
$my_custom_field2 = get_post_meta(244,'field_title_here',false);
foreach ( $my_custom_field2 as $key => $value )
echo $key . " => " . $value . "<br />";
?>
Example output:
0 => Title 3
1 => Title 4
2 => Title 1
3 => Title 2
The order I entered them in the admin area is ascending:
Title 1
Title 2
Title 3
Title 4
Does anyone know what’s happening?
]]>function F_custom_fields() {
if (have_posts()) : while (have_posts()) : the_post();
$custom_fields = get_post_custom();
$page_icon = $custom_fields['eva_page_icon'][0];
$div_id = $custom_fields['eva_div_id'][0];
endwhile; endif;
}
Since this is not working, I figure there is a variable/array/other that I need to set with global $var as the first thing in the function. I just can’t figure out what it is.
]]>Using this code to fetch a custom meta value only when one is present almost works for me:
if (get_post_custom()) {
$quote = get_post_meta($post->ID, 'quote', true);
echo "<div class=\"quote\">$quote</div>";
}
The almost is this:
If a post has a tag, but not a custom meta value (the $quote) it shows the div container. If a post doesn’t have tags and no meta value no div shows up (as I want it).
As long as the $quote is defined it doesn’t seem to matter whether or not the post is tagged…
What is going on?? What does the post tags have to do with custom meta values?!? *scratches head, utterly confused*
I’d surely like som input on this one, it’s making my brain itch.
]]>The problem is that the information collected in the metabox isn’t showing up in the post, and doesn’t seem to be appearing in the get_post_custom() array.
I’ve tested and the plugin is reading the metabox form correctly (ie getting the right info from the form) and something is being put into the meta, but it looks like consecutive numbers.
Here’s part of the function I’m using to save the meta info (validation removed for brevity):
function save_sermon_servant_info() {
global $post, $sermon_servant_info;
foreach ($sermon_servant_info AS $info) {
if (){} //verification code goes here
else { //verified
//get data
$field_data_name = $info['name'].'_value';
$sermon_servant_field_data = $_POST[$field_data_name];
if (! add_post_meta($post->ID, $field_data_name, $sermon_servant_field_data, true)) {
update_post_meta($post->ID, $field_data_name['name'], $sermon_servant_field_data);
}
}
}
And here is the results of putting print_r(get_post_custom_keys($post_id));
into the coed for writing the post:
Array ( [0] => _edit_last [1] => _edit_lock [2] => _encloseme )
And here is the results of this code in the post:
$sermon_info = get_post_custom($post_id);
print_r($sermon_info);
Array ( [_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1232729875 ) [_encloseme] => Array ( [0] => 1 [1] => 1 ) )
And after the second time I try to reenter info into the meta fields this is how the array grows:
Array ( [_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1232730051 ) [_encloseme] => Array ( [0] => 1 [1] => 1 [2] => 1 ) )
I’m having trouble interpreting this array, and there is precious little information in the codex about these keys. You can download the entire file: amosglenn.com/files/sermonservant.php
Where’s my data going?
Thanks for your help.
]]>