Wordpress Post Excerpt Class - wordpress

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>

Related

Wordpress custom button link output after the_content

I'm trying to insert a button to show at the end of each post on Wordpress in which the link it goes to is defined by a setup using the custom fields plugin. When creating each post, I am able to select the link I wish to display.
Here is the code I have which I know is wrong but I was hoping someone could help here.
function wpb_after_post_content($content){
if (is_single()) {
$content .= 'Contact Franchise →';
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
I assume $franchise_profile_url is a variable and you should concatenate it in the string like this
$content .= 'Contact Franchise →';
function afterContent($content) {
if(!is_feed() && !is_home()) {
$content.= "<div class='footNote'>";
$content.= "<h4>Click the below button if you like it:</h4>";
$content.= "<p><a href='#'>LIKE</a></p>";
$content.= "</div>";
}
return $content;
}
add_filter ('the_content', 'afterContent');
Use the above function. It will help you to achieve what you need.
Thanks for the help here, however, that code simply links back to the post itself and isn't pulling in the URL as set on the post using custom fields. This is the code I had set up before which was working on a default post setup but now I wish to use an alternative method in the functions.php file
<?php if( get_field('franchise_profile_url') ): ?>
Contact Franchise →
<?php endif; ?>

wordpress add field to post_class

I need to add a custom field to my article post but not sure how to add an additional class to it.
Currently the classes get pulled through like this <?php post_class($classes); ?>.
However I need to add a custom field to this as well. To demonstrate ive added a class= but this doesnt work as class= is being added twice.
<?php post_class($classes); ?> class="<?php the_field( "size" ); ?>
So i need post_class and the_field to work together.
Thanks for your answers but I've found a simple way to do this
<?php post_class(get_field('field_name')); ?>
You can do this with two different way,
First:- Add following code in theme's functions.php file:
This will add your class where post_class is called.
function category_id_class($classes)
{
global $post;
if($post->post_type == 'post')
{
$classes[] = get_field( "size" );;
}
return $classes;
}
add_filter('post_class', 'category_id_class');
Second:- Add this following code directly into your page:-
$post_classes = get_post_class();
$post_classes= implode(' ', $post_classes);
echo 'class="'.$post_classes. the_field( "size" )'"';
Hope this will help you little bit.
So why you could not do like this:
<?php post_class(the_field( "size" )); ?>
Because it is working like this:
<?php post_class('my_custom_class'); ?>

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Display all comments for a WordPress blog on a single page?

What I need - The code to perform the following:
I am trying to setup a WordPress template that will display all the comments that have been posted to my blog. How do I pull all comments and have all the same formatting that is applied to comments under a single post? Such as the formatting that occurs when comments are displayed using the comments.php template.
Note I want to pull all the comments from my blog to a single page. I still want the comment pagination but instead of having 20 comments under post #1, 20 under post #2, etc. I want to have all 40 show up at one time on one page.
You want to use the get_comments() function.
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
See also the apply_filters() function to apply comment output filters to specific fields.
<?php echo apply_filters('comment_text', $comment->comment_content); ?>
EDIT:
For pagination, you can use the offset and number parameters of the get_comments() arguments:
<?php
$args = array(
'number'=>20,
'offset'=>0,
'status'=>'approve',
);
foreach (get_comments($args) as $comment) {
// ...
}
?>

Obtaining wp_list_pages in a Wordpress widget

I am creating my own little widget sitemap to put in my footer. Here's my code:
function widget($args, $instance)
{
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if ( $title ) { echo $before_title . $title . $after_title; }
// WIDGET CODE GOES HERE
// ?> THIS IS A TEST! <?php
?><ul><?php
wp_list_pages('title_li=<h2>MAP OF THE SITE</h2>&sort_column=menu_order&depth=0&include=57,55,59,61,63,65,192');
?></ul><?php
echo $after_widget;
}
but looks like wp_list_pages doesn't return anything. Not even the title. Indeed if I uncomment that "this is a test" then it's shown.
The strange thing is the same wp_list_page is implemented also in header.php to obtain menus.
First rule when creating Wordpress custom functions is to check if function with the same name already exists.
<?php if (!function_exists("function_name")) {...} ?>
Second rule is to name your function with really unique name, in order to avoid conflicts. In your case this function already exists, that's why you cannot extract the data as you expect...
One more thing, I assume you will put this whole function code in your theme's functions.php file.

Resources