Adding shortcode to Wordpress Template File - wordpress

I am attempting to add the following shortcode to a template file at my site using do shortcode and I can't seem to get it working correctly for some reason. Any idea how this needs to be formatted to working correctly?
[nws_alerts zip='<? echo $decoded2['ob']['zip']; ?>' state="" display="basic"]

You can't have PHP inside the shortcode like that. It would need to be called like this:
<?php
echo do_shortcode( '[nws_alerts zip=' . $decoded2["ob"]["zip"] . ' state="" display="basic"]' );
?>

Related

How to display author name on my website post pages?

How to display author name near the date on my website homepage and single post page? . Is there is css code for that. Iam using hemingway theme. And my website is www.avjtrickz.com . please help me, Thanks for advance.
You have to use:
<?php $author = get_the_author(); ?>
or:
<?php the_author(); ?>
Here you can find more information: https://codex.wordpress.org/Function_Reference/get_the_author
And here: https://codex.wordpress.org/Function_Reference/the_author
And here: Show Author name in single.php (Wordpress)
This needs to be done using the WordPress codex for author. This reference needs to be used in the Loop. There is no CSS for this.
https://codex.wordpress.org/Function_Reference/the_author
<?php $name = get_the_author_meta( 'display_name' ); ?>
reference : https://developer.wordpress.org/reference/functions/get_the_author_meta/

Change plugin template in wordpress

I have wordpress plugin with template file. And this file has function
function parse_smart_content_func($atts, $content){
...
$html .= '<div class="smart-box-content">';
if($layout!='medium_carousel_2'){
$html .= '<div class="smart-item '.$class_cssit.'">
<div class="row">';
}
....
And I want to change this function. But if I update plugin I will lost my code.
How can I solve this problem?
Try to copy the template file from the plugin in your theme. The version in our theme should overwrite the one in the plugin.
To know how to name it, use this diagram of wordpress theme:
http://codex.wordpress.org/User:Fpp

Wordpress Post Excerpt Class

I'm customizing a Wordpress Theme and i have stucked in add a custom class to the "the_excerpt()".
I have searched on the web for this function, but nothing is specific to add a custom class.
Hm, i've tried the "get_the_excerpt", but this one doesn't work (returns nothing).
Anyone knows how to do this?
<?php echo get_the_excerpt(); ?>
Shows the excerpt
<p class="your-class"><?php echo get_the_excerpt(); ?></p>
Shows the excerpt in a div with a class
As far as I'm concern wordpress doesnt provide a solution for this however you can try printing the excerpt using this solutions:
Solution 1: printing the class in the page.
<?php echo '<p class="yourclass">' . get_the_excerpt() . '</p>' ?>;
Link of where i found answer 1
Solution 2: override excert function:
You can override the excerpt function in wordpress functions.php using the following code and pasting in the same file:
function my_custom_excerpt( $excerpt ) {
$post_excerpt = '<p class="yourclass">' . $post_excerpt . '</p>';
return $post_excerpt;
}
add_filter( 'the_excerpt', 'my_custom_excerpt', 10, 1 );
Link where i found answer 2
Conclusion: I prefer using the solution #1 because is cleaner and very understable for someone which will maintain the site.
You can add this code to your functions.php to add the class myclass to the excerpt container:
add_action('the_excerpt','add_class_to_excerpt');
function add_class_to_excerpt( $excerpt ){
return '<div class="myclass">'.$excerpt.'</div>';
}
you don't have to put the_excerpt() inside a p tag, this is going to generate a new p tag contains the excerpt , all you have to do is to put the_excerpt() inside a div tag with your class name.
<div class="post-sumary">
<?php the_excerpt() ?>
</div>

Display WP Gallery by default

I'm creating a custom post type for creating gallery posts. One of the things I took out was the 'editor' section since I have my own uploader. Since the HTML editor is gone (can't use shortcodes now), is there a wp function which is the equivalent of the [gallery] shortcode?
As per the Codex recommendation for outputting the [gallery] shortcode content directly in a template file, I would use do_shortcode():
<?php do_shortcode( '[gallery]' ); ?>
You can even pass accepted parameters:
<?php do_shortcode( '[gallery columns="4" size="medium"]' ); ?>
Got it. Here's the code for anyone else who wants to do the same thing. Throw this baby in your theme template file and try it out.
$images = get_children(array('post_parent'=>$post->ID, 'post_type'=>'attachment', 'post_mime_type' => 'image'));
foreach($images as $image){
echo wp_get_attachment_link($image->ID);
}
You can use the wordpress function make for this purpose
gallery_shortcode(array('orderby'=>ID));
by sure to call this after you setup the post

Displaying code snippets from WordPress custom fields

I'm trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo '<div class="post-item-divider">Post Divider</div>';
}
?>
If I try to display this code wrapped in <pre></pre> tags in my page template like
<?php if ( get_post_meta($post->ID, 'snippet-1', true) ) : ?>
<pre><?php echo get_post_meta($post->ID, 'snippet-1', true) ?></pre>
<?php endif; ?>
but it returns nothing to the template. I understand WordPress is filtering the snippet out as it sees as PHP code to execute. Is there a way just to print this out on the page as a code snippet?
Many thanks in advance
rob
Use htmlspecialchars() to escape your code.
Update
echo htmlspecialchars(get_post_meta($post->ID, 'snippet-1', true));

Resources