I've searched and searched, but basically what I'm trying to do is this:
I have a custom post type where there is a custom field called "loop". What I would like to do is when this field is filled out, I want to use it's content as a template part. So, for instance:
Custom Field Loop: customloop
get_template_part( 'customloop')
So, when I run my query for this post, I basically need whatever's filled out in that custom field to then be thrown into the get_template_part() so that it pulls that particular loop.
Any thoughts on how to do this?
Thanks in advance.
Yes, we can do this with the get_post_meta() function, just make sure to use the correct key name.
$loop = get_post_meta( get_the_ID(), 'customloop', true );
if($loop):
get_template_part('customloop');
else:
get_template_part('loop');
endif;
Related
I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode
I need to get the value of a custom field (_subtitle) from the post meta and have it saved to the beginning of the post text (post_content).
How can I achieve this? (I tried the update() function but lost the post content before)
You need to use get_post_meta() it might look something like that if you want to place it within a template.
<?php
$subs = get_post_meta(get_the_ID(),'_subtitle',true);
$post_content = get_the_content();
echo $subs.$post_content;
?>
This would be the gist of it, you could also filter the_content(); and place the code within the functions.php
I'm trying to link posts from WordPress to an associated post on Tumblr. I have the Tumblr id set as the value for a custom field in each WordPress post.
Here's what makes sense to me, but the link isn't capturing or outputting the value:
<h3><?php the_title(); ?></h3>
Is there a problem with the above code that anyone can see? Any help is, as always, greatly appreciated, thanks!
Also, I'm using the Ajax Load More plugin to load the posts to the page, not sure if that's why...
The meta key should be a string. You're not using $key so I'd remove that to avoid confusion.
Change this:
<?php $key="mykey"; echo get_post_meta($post->ID, tumblr_id, true); ?>
To:
<?php echo get_post_meta( $post->ID, 'tumblr_id', true ); ?>
Okay, so I realize what the problem is, because I'm using a function to load the posts to the page, global $post; needs to be declared within the loop.
Also, as #Nathan Dawson pointed out the meta key needed to declared as a string.
Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks
I am currently posting from the front end of wordpress using code I found here
http://voodoopress.com/review-of-posting-from-front-end-form/
however I am using a few plugins that have thier fields in meta boxes on the backend that I can't seem to pass the data to from the front end form.
One of the plugins is the wordpress facebook plugin http://wordpress.org/extend/plugins/facebook/
The field id like to have access to is the suggest-friends ID as it has ajax facebook auto complete for current friends as you type.
Is this a possibility on the front end or am I attempting the impossible?
Thanks
If I understand your question right, this is the function you need:
http://codex.wordpress.org/Function_Reference/get_post_meta
The usage example they give in the documentation is this:
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
For the first value, $post_id, you can follow this instruction:
$post_id is the ID of the post you want the meta values for. Use
$post->ID to get a post's ID within the $post variable scope. Use
get_the_ID() to retrieve the ID of the current item in the WordPress
Loop
So if you are on your template page $post->ID would be fine.
Next, you need $key to be "suggest-friends"
Finally, $single set as true returns a string
So you can use this as your final result:
<?php
$suggestFriends = get_post_meta($post->ID, 'suggest-friends', true);
echo 'The value of suggest-friends for this post is: '.$suggestFriends;
?>
Hope that helps!