Where can I find the file where the_content() gets the value from post_content in the database?
I wanted to do the concept but with another content from the database,
So far, I've added a new column to the database ex. post_content2
How can I get its value by using something like
<?php the_content2(); ?>
I tried looking at post-template.php and found the function but not how it gets the value from the database post_content.
From the comments above, and a more complete answer:
Do not modify core WP tables. Either use post_meta functions, or add a new table.
the_content() is just a convenience function that directly accesses the posts table, content column. You can do the same thing at any time by getting the value from the database, and passing it through apply_filters('the_content', $my_custom_content);. If you want to write your own the_content2() function, it could / should be done like so (using the post_meta method mentioned above):
function the_content2() {
// Global in the current post so we can get the ID
global $post;
// Load your custom content
$my_content = get_post_meta($post->ID, 'my_custom_content');
// Echo it out (like the_content()) applying 'the_content' filters
echo apply_filters('the_content', $my_content);
}
If you want to figure out how to SAVE this content, then you can read this writeup:
How to Create Custom Meta Boxes in WordPress
edit
It's worth noting that this functionality is already available in a few different plugins you can install. In my experience Custom Field Suite is the most reliable and easy to use (but that's just my opinion).
Related
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 am developing a plugin with Wordpress. But I need to save a short code like [my-shortcode] and then in my front end show the final result of the short code. Imagine that I have one form where you can upload an image, select width, height and preloaded themes, animations, etc. But I want to use short code.
How to save the compiled short code with do_shortcode() with Wordpress options?
I think, maybe I can do it with javascript, but I didn't found it on the web.
You cannot save a shortcode with do_shortcode(). do_shortcode()is used to render a shortcode.
You can use a metabox that renders an input and then you can use your JavaScript code that targets that input and stores your shortcode in its value attribute.
You can then refer to that post meta data which holds your shortcode by using the get_post_meta(); method provided by WordPress.
Here is the link to get_post_meta http://codex.wordpress.org/Function_Reference/get_post_meta
You can refer to how to add a metabox in WordPress here:
http://codex.wordpress.org/Function_Reference/add_meta_box
Your input tag should be coded like so in your callback function:
echo '<input type="hidden" id="My-Shortcode" name="My-Shortcode" ' . get_post_meta($post->ID, 'My-Shortcode', true) . '/>';
The input with the name My-Shortcode will actually hold your shortcode but as you see it requires a $post object which actually hold all the data for that post. You can then refer to your shortcode key by stating
get_post_meta($post->ID, 'My-Shortcode', true)
True simply means the value will be echoed out.
Now in order to access any data within that $post object you need to pass the $post parameter to your function. Normally WordPress gives you access to that $post object but you need to state that in your metabox callback function like so:
function My_callback_function( $post ){
// your input will be here
}
In the front end you can then simply call get_post_meta again like:
echo get_post_meta($post->ID, 'My-Shortcode', true)
Keep in mind that there are security issues when saving data to and from your WordPress database which is why you would like to use WordPress's nonce system. Here's the link. Investigate to know more: http://codex.wordpress.org/WordPress_Nonces
I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.
Here's a fun one. I'm using advanced custom fields inside of a template to pull a field called Applications with the following code.
the_field('applications');
The problem is that this won't pull the actual content without the page ID along with it, for example:
the_field('applications','42');
where the page ID is 42.
What I would like to do is use another template tag to pull the existing page ID with something like this in place of the 42, so that it will content specific to that page:
the_ID();
Which, in a perfect world would look like so:
the_field('applications','the_ID();');
This is obviously ridiculous and doesn't work, but I don't know what I need to do to get this to actually work.
Thanks!
the_ID() will automatically echo whatever is returned, whereas get_the_ID() will return that value so that it can be stored in a variable, or passed as an argument (which is the deal in this case).
<?php the_field('applications',get_the_ID()); ?>
http://codex.wordpress.org/Function_Reference/get_the_ID
I've made a template for specific pages of our website that display similar content, yet are different form the rest of the pages of the website - here's an example. I have several custom fields that I use to include all of the items you see in the boxes that float to the left and right.
My problem is, I also have pricing that's included from a custom field - and because I want it at the bottom of the page, it actually appears outside the content to work (see the bottom of my example given above). Is there a way I can call the content and tell it to display these custom fields at after the content is displayed?
Obviously the following doesn't work, but just to give you an idea:
<?php the_content(get_post_meta($post->ID, "Pricing_Mexico", true); ?>
Additionally, there are actually a lot of custom fields that need to be displayed, so if it were possible to include them in bulk (including the markup which includes divs and what-not) that would be preferred. Thanks!
You could filter the_content() so in your functions file:
function contentFilter($content){
// Output the current content
echo $content;
// Output the meta field
echo get_post_meta($post->ID, "Pricing_Mexico", true);
}
add_filter('the_content', 'contentFilter');
You could echo as many meta fields as you need inside the function.