I noticed that when I call the the_post() or the_content() function from my wordpress template, it automatically parses the database data to replace new lines with <br/>, wraps the text with <p> tags etc...There's probably some kind of "format" function within the_post() or the_content().
I wrote a query to directly get posts from the wp_posts. I then print it out like
<?php
$results = $wp->get_results($sql)
foreach($results as $row) echo $row->post_content; ?>
Clearly, this data is not parsed by wordpress' "format" function. What is the proper way to output this content such that it undergoes the same "formatting" functions as the_post() or the_content()?
#dpelletier is right, and you can simply apply that function to the $row->post_content string.
If you want WordPress to do everything it would normally do on the content, including wpautop and parsing shortcode, use;
$content = apply_filters('the_content', $row->post_content);
There is a function called wpautop().
Only adds <p> and <br/>, so I'm not sure if there is another formatting function that would do anything else.
Related
I am using a simple shortcode to add line space or separator. I added this shortcode in my post meta field where we can add custom description but this shortcode is not extract on frontend
This is the text in my editor of that post meta
Have you been looking for a better, easier way to enjoy your favorite [linespace gap="30"] Introducing the beautifully simple, versatile and discreet don’t let its size fool you.
where [linespace gap="30"] is that shortcode
This is the output on frontend
Have you been looking for a better, easier way to enjoy your favorite [linespace gap="30"] Introducing the beautifully simple, versatile and discreet don’t let its size fool you.
this is my code for the shortcode function
function linespace_gap_shortcode($atts,$content = null){
$arg_s = shortcode_atts(
array(
'gap'=>'30',
), $atts, 'linespace' );
//getting the values from shortcode
$gap = $arg_s['gap'];
ob_start();
?>
<div class="clearfix separator-<?php echo $gap; ?>"></div>
<?php
return ob_get_clean();
}
add_shortcode('linespace','linespace_gap_shortcode');
In my mind you need to add a do_shortcode() inside your template around your meta value.
Example :
$my_meta = do_shortcode(get_post_meta( 0, 'meta_key', true ));
NB : think about esc_html() inside your shortcode function or force $gap to be an integer for better security .
I am trying to use the content to signify which template_part code to load on my page. For example, if the content == "volume" I want to load get_template_part('lib/volume');
<?php
$content = get_the_content();
$content = strip_tags($content);
$content = html_entity_decode($content);
$content = filter_var($content, FILTER_SANITIZE_STRING);
get_template_part('lib/'.$content);
?>
This is the code in my template, however it doesn't load the desired template part.
You can strip all html tags with using wp_strip_all_tags() to get only the content. strip_tags() won't help you get rid of comments and wordpress gives us the great function to also delete the script and style.
https://developer.wordpress.org/reference/functions/wp_strip_all_tags/
So you can use one line of code to get a clean content:
$content = wp_strip_all_tags( get_the_content() );
Maybe this is the reason. Have you tried so display the value of $content to make sure you have the string you want to have? If it outputs the string the right way, there might be a problem with the get_template_part() function.
https://developer.wordpress.org/reference/functions/get_template_part/
You can try putting the string together, before using in the function:
$content = wp_strip_all_tags( get_the_content() );
$content = "lib/".$content;
get_template_part( $content );
If this is not working, check the name of the folder lib (has to be subfolder in the root of your theme folder) and the name of your php files. It could also be possible that there is something wrong with the template part file and that is the reason it is not loaded. Next check the page template inside of which you are trying to get the template part.
If nothing helps you out, it could be an alternative using include() instead of get_template_part().
So I'm trying to reference custom field values in a plugin I'm building. All I need to do at this stage is grab the values and store them in variables. This is my code to get the custom field value of pageName:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$pageName = get_post_meta($postid, 'pageName', true);
wp_reset_query()
?>
So when I try to echo that out, I get nothing. I notice that my plugin runs before the head or anything else, so it's the first code in the source. My hunch is that this is due to timing and the value just isn't there yet. Is there a way to make my plugin, or this chunk of code, wait until the custom field values are there before trying to grab them?
I'm trying to avoid doing anything in the theme files so this can be a stand alone plugin that I can share.
yes, you can get the value of any post meta of the custom post type.
Just make sure that you are receiving the correct post_id in the $postid variable.
If you get the correct id of the post type you can get any meta field
Example:
global $post;
if ($post->ID) {
$media_id_meta = get_post_meta($post->ID, 'media_id', true);
}
Found the solution! I wrapped the whole thing in a function to put it in the footer, which made sure that everything it needed was there.
//----This function is wrapped around the code for my plugin
function dataLayerInject() {
*ALL MY CODE*
}
//----This drops my code into the footer
add_action('wp_footer', 'dataLayerInject');
In the theme, I use get_the_excerpt in index.php, where I use the loop to display the titles, excerpts, ... of several post and it works as expected. The excerpt/teaser is manually marked with the "read more" tag.
However, I also would like to use get_the_excerpt in single.php but instead of receiving only the text up to the "read more" tag, I get the first 55 words.
The documentation says:
The <!--more--> quicktag will not operate and is ignored in Templates where just one post is displayed, such as single.php.
Ok, so what can I do if I want the excerpt to contain the text only up to the quicktag in single.php?
I believe you can accomplish what you want with the built-in functionality of WordPress, utilizing the $more variable.
In order to show the content before the more quicktag, you should be able to do this on your single.php:
// Declare global $more (before the loop).
global $more;
while( have_posts() ) {
// Set (inside the loop) to display content above the more tag.
$more = 0;
// call the_post() to prepare post variables / functions
the_post();
// output your markup, post title, etc...
// then this the_content() should render only the content before the more quicktag
the_content( 'More ...' );
}
Should be pretty straight forward but placing my filter in the themes function file is not having any affect on the template:
add_filter('term_description', 'filter_ptags_on_images');
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
and my markup:
<?php echo term_description(); ?>
EDIT: I tried testing the filter by returning dummy content from the function and nothing changes, so the filter isn't running on the content for some reason
It turns out I was trying to target the wrong template and the actual function was an Advanced Custom Field wysiwg custom content tag:
<?php the_field('fieldname'); ?>
It took me a little while to figure out how to filter this but with a little googling I found out you can use this to target an ACF wysiwyg field from your functions file:
add_filter('acf_the_content', 'your_function');