here's the correct code: (Thanks!) (Just had to add '. .' and the value in between when echo)
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
If you still want to read the problem, here it is below.
Problem:
Currently, I have a custom field created called "ytuser" on one of my posts. Inside that field, I have typed in a youtube username (for example: youtubeuser1). What I am trying to do now is put that value at the end of a href like this: <a href="http://www.youtube.com/$value"/> so that it actually returns the following address: http://www.youtube.com/youtubeuser1 and the text then links to that user's page.
So far I have this:
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
But that only gives me a link to "http://www.youtube.com/"
I can't add $value directly to it...
Is there a way I can include the $value as part of the href? So that it can make the text link go to http://www.youtube.com/$value ?
I know this is a noob question since it can't be that hard.
I would just do $ytuser = get_post_custom_values("ytuser"); and then call the variable $ytuser[0]; where you need it like echo "<a href='http://www.youtube.com/".$ytuser[0]."'>".$ytuser[0]."</a>";. That should work!
Also, in your code example, you want to switch the " and ' usage in your echo and make sure you echo the user after the YouTube URL and as the link itself (if that's what you want).
Related
Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`
I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta
I'm using the Types Wordpress plugin to enable custom fields. The plugin allows you to rearrange the order of the images in the admin page editor. Here's my code in my single.php to display multiple images in the custom field and have a link to itself to also use Fancybox:
<?php
$mykey_values_img = get_post_custom_values('wpcf-image');
if ($mykey_values_img != null) {
foreach ( $mykey_values_img as $key => $value ) {
?>
<img src="<?php echo $value; ?>" />
<?php
} //foreach
} //if
?>
Problem:
Right now this code works perfectly on my local copy running on MAMP. However, when I put it online hosted on iPage, the images are out of order. I don't know what's causing this discrepancy. When I use the shortcode from Types to display the images instead of the php above they are in order, but I don't have the option of using Fancybox. I was also wondering if there is a better way to display an image from Wordpress that will insert the alt tag for the image.
I just encountered this problem too, and your first answer led me to a tighter solution.
I also used types_render_field(), but if you include a 'raw' parameter, you can avoid the string manipulation.
$images_raw = types_render_field('image', array('raw'=>'true','separator'=>';'));
$images = explode(';', $images_raw);
foreach ($images as $link) {
echo '' . $link . '">"';
}
Then, if you're nasty, you can get the ID of the attachment from its SRC. Using that ID, you can get all the info you need about that attachment, like a caption, or whatnot.
I figured out a work around to get it working. Its not ideal but it works.
The Types plugin came with its own php function to display the custom field called types_render_field. It displayed my images in order. To get fancybox working I had to do sort of a hack on the string. Here's the code:
$images = ( types_render_field( "image", array( 'size' => 'full', 'alt' => get_the_title(), 'title' => get_the_title() ) ) );
$imgArray = explode(">", $images);
foreach ( $imgArray as $value ) {
$pos1 = strpos($value, 'src="', 0)+5;
$pos2 = strpos($value, '" ', $pos1);
$link = substr($value, $pos1, $pos2 - $pos1);
echo ''.$value.">";
}
For some reason, I cannot get WP_Query to return the custom field values of posts. I can get the post thumbnails using get_the_post_thumbnail($post->ID, array(50,50)), but I cannot get the custom field data using get_post_meta($post->ID, $key, true).
A stripped-down version of what I'm trying to do:
<?php
$keys = array('Show Date','Birth Year','Origin');
echo '<table>';
echo '<tr><th>Title</th>';
foreach( $keys as $key ) {
echo '<th>' . $key . '<th>';
}
echo '</tr>';
$myquery = new WP_Query( 'post_type=post' );
if( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();
$title = get_the_title();
echo '<tr><td>' . $title . '</td>';
$values = array();
foreach( $keys as $key ) {
$values[] = get_post_meta($post->ID, $key, true);
}
foreach( $values as $value ) {
echo '<td>';
echo $value;
echo '</td>';
}
echo '</tr>';
endwhile; endif;
echo '</table>';
?>
Also available here:
http://pastebin.com/at8S2THs
Even with all non-essential code removed, I cannot make this work. I know you can use parameters like meta_key and meta_value in a query to narrow down the results, but I just want to display all values for the keys I specify, if they exist, for each post.
Any help would be greatly appreciated...
** SOLUTION FOUND **
Just needed to add global $post; after the start of the loop. Thanks to #Kimikaze on the WP support forum for providing the solution!
When I can't find the data I need in wordpress, I always find it helpful to print the Global $post object to screen, so I can see if my data is making it to the page.
Global $post;
echo "<pre>";
print_r($post);
echo "</pre>";
All the helper methods or "hooks" are usually just interacting with this (or another) global object. Check the output of this on $post (and maybe your $values array) for data you're looking for and if it's there, you can just reffer to it directly, like the post title for example
$post->title
Another thought, The third parameter of get_post_meta() makes it return a single string when set to true, what do you get when it's set to false?
I'm guessing the problem has to do with the values you're using as meta keys. To retrieve the data with get_post_meta() you'll want to pass in the actual meta_key values, which are probably 'show_date', 'birth_year', and 'origin' since the meta_key value doesn't handle uppercase letters or spaces. Try changing to those values where you're setting the $keys array:
$keys = array('show_date','birth_year','origin');
If that doesn't work, it might be worth checking in the database (in the wp_postmeta table) to confirm the actual meta_key values.
I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();