On my Wordpress blog I use excerpts a lot. On my homepage I want a "read more..." on the same line as the text(without CSS), however I do not want it on my news page like that. I found this code which does exactly what I want but it applies it to all the excerpts. How do I have it just do this on the Homepage?
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'
. get_permalink( get_the_ID() )
. '">Read More...</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
This should work:
function new_excerpt_more($more) {
if(is_home())
$more = ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More...</a>';
return $more;
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
EDIT: In your theme, before the loop, you should add a variable (a flag) to see if it's the page that you want:
$my_flag = is_page( array( id, id, id, id, 'page-name', 'page-slug' );
if( have_posts() ) :
while( have_posts() ) :
// Your code
endwhile;
endif;
And in your function (new_excerpt_more), you must check it like this:
function new_excerpt_more($more) {
global $my_flag;
if(is_home() || $my_flag)
$more = ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More...</a>';
return $more;
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
I haven't tested it, but it should work. However, isn't supposed that pages display single content? It would never display only the excerpt (of course, it will depend on your theme :P)
Documentation:
is_home()
Related
I'm looking to change heading tag for products displaying on product category page (from no heading tag to H2) but only on product category page.
I found 2 solutions, which work, however they also change heading tag for products displayed on other pages, which I don't want.
1. By changing heading tag in content-product.php file
It works, however that also changes products displayed in related products on a product page.
<div class="box-text <?php echo flatsome_product_box_text_class(); ?>">
<?php
do_action( 'woocommerce_before_shop_loop_item_title' );
echo '<h2 class="title-wrapper" style="font-size: 100%">';
do_action( 'woocommerce_shop_loop_item_title' );
echo '</h2>';
2. By adding code in functions.php file for woocommerce-loop-product
It works, but it changes heading tag everywhere, even for products displayed on the home page.
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'soChangeProductsTitle', 10 );
function soChangeProductsTitle() {
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';
}
Maybe the first solution is the best and I just need then to add code in related.php file or in funtions.php file, for changes on heading tag don't apply on related products?
I never learned to code, I'm blocked here.
You can use the conditional tag is_product_category()as follow:
1. By changing heading tag in content-product.php file
<div class="box-text <?php echo flatsome_product_box_text_class(); ?>">
<?php
do_action( 'woocommerce_before_shop_loop_item_title' );
if( is_product_category() ) {
echo '<h2 class="title-wrapper" style="font-size: 100%">';
}
do_action( 'woocommerce_shop_loop_item_title' );
if( is_product_category() ) {
echo '</h2>';
}
2. Or by adding code in functions.php file arround woocommerce_shop_loop_item_title hook
add_action('woocommerce_shop_loop_item_title', 'product_heading_title_on_category_archives', 1 );
function product_heading_title_on_category_archives() {
if( is_product_category() ) {
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'change_product_heading_title', 10 );
}
}
function change_product_heading_title() {
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';
}
I use the code to display product thumbnails and for "Quick View" functionality.
Here is the code for the product thumbnail:
// Change product thumbnail markup.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' );
add_action( 'woocommerce_before_shop_loop_item_title', array( __CLASS__, 'product_thumbnail' ) );
/**
* Product thumbnail.
*/
public static function product_thumbnail() {
global $product;
switch ( konte_get_option( 'shop_product_hover' ) ) {
default:
echo '<div class="product-thumbnail">';
woocommerce_template_loop_product_thumbnail();
echo '</div>';
break;
}
}
Here is the code for the "Quick View" icon:
/**
* Quick view button.
*/
public static function quick_view_button() {
if ( ! konte_get_option( 'product_quickview' ) ) {
return;
}
printf(
'<a href="%s" class="quick_view_button quick-view-button button" data-toggle="%s" data-target="%s" data-product_id="%s" rel="nofollow">
%s
</a>',
esc_url( get_permalink() ),
'modal' == konte_get_option( 'product_quickview_style' ) ? 'modal' : 'off-canvas',
'modal' == konte_get_option( 'product_quickview_style' ) ? 'quick-view-modal' : 'quick-view-panel',
esc_attr( get_the_ID() ),
konte_svg_icon( 'icon=eye&echo=0' )
);
}
Here is the file that has this code in it - template-catalog.php
I need that when I click on the product thumbnail, a quick view window is displayed. Help combine these two codes. Thank you in advance!
In the product-catalog.php file, there are two actions where you can hook into to wrap the product thumbnail:
woocommerce_before_shop_loop_item: adds HTML code before the thumbnail
woocommerce_after_shop_loop_item: adds HTML code after the thumbnail
You should add the following code into the functions.php file:
function my_custom_link_open() {
printf(
'<a href="%s" class="quick_view_button quick-view-button button" data-toggle="%s" data-target="%s" data-product_id="%s" rel="nofollow">',
esc_url( get_permalink() ),
'modal' == konte_get_option( 'product_quickview_style' ) ? 'modal' : 'off-canvas',
'modal' == konte_get_option( 'product_quickview_style' ) ? 'quick-view-modal' : 'quick-view-panel',
esc_attr( get_the_ID() )
);
}
function my_custom_link_close(){
echo '</a>';
}
add_action ('woocommerce_before_shop_loop_item', 'my_custom_link_open');
add_action ('woocommerce_after_shop_loop_item', 'my_custom_link_close');
So this is just ham fisted, but wasn't sure if I could copy that wc-template-functions.php to my theme or not. This worked for me anyway. The one above didn't remove the thumbnail link or add the closing "a" in the right place
/includes/wc-template-functions.php and look for if ( ! function_exists( 'woocommerce_template_loop_product_link_open' ) ) {
Replace that section with
if ( ! function_exists( 'woocommerce_template_loop_product_link_open' ) ) {
/**
* Insert the opening anchor tag for products in the loop.
*/
function woocommerce_template_loop_product_link_open() {
global $product;
$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
printf(
'<a href="%s" class="quick_view_button quick-view-button" data-toggle="%s" data-target="%s" data-product_id="%s" rel="nofollow">',
esc_url( get_permalink() ),
'modal' == konte_get_option( 'product_quickview_style' ) ? 'modal' : 'off-canvas',
'modal' == konte_get_option( 'product_quickview_style' ) ? 'quick-view-modal' : 'quick-view-panel',
esc_attr( get_the_ID() )
);
}
}
add this custom css ul.products li.product .product-thumbnail .quick_view_button {width:100%!important;} div.product-summary > h2 > a{width:100%!important;}
The code below adds an image into my wordpress RSS feed. I am wanting to make it so that the image is automatically hyperlinked back to the corresponding post. The code is in my theme's functions.php
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
Can I change this so that the post_thumbnail is automatically wrapped with a link to the post?
How can I wrap the get_the_post_thumbnail( $post->ID ) part of the code with a link? Thanks
You can use the get_permalink function and pass it the post ID.
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = '' . get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}
I am wondering how to do the following:
I want to do an excerpt after 45 words, but if the text of the post is less than 45 words and images are included in the post, then the more tag should be included right after the text.
1st: I would be happy with this solution.
2nd: Great might be to have in such a case an alternative sentence, e.g. "Click to see pictures.".
Hope this makes sense to anybody reading this.
Currently I have the following:
/*-----------------------------------------------------------------------------------*/
/* Sets the post excerpt length to 15 characters.
/*-----------------------------------------------------------------------------------*/
function moka_excerpt_length( $length ) {
return 45;
}
add_filter( 'excerpt_length', 'moka_excerpt_length' );
/*-----------------------------------------------------------------------------------*/
/* Returns a "Continue Reading" link for excerpts
/*-----------------------------------------------------------------------------------*/
function moka_excerpt_more( $more ) {
return '… <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __( 'Read more', 'moka' ) . '</a>';
}
add_filter( 'excerpt_more', 'moka_excerpt_more' );
Any help is much appreciated.
Many thanks and kind regards.
I think, You can write the code yourself.
You can add filter to the_content() or an alternative way:
Refer to this: http://wordpress.org/support/topic/executing-a-function-only-if-a-post-contains-an-image, you can check if the_content() has img or no?!
And for counting words in the_content() this answer is useful:
Counting words on a html web page using php
I did some deeper research and came up with the following. I know that it won't work, but some parts work as standalone, but I am not able to bring everything together. Maybe somebody can help to point me into the right direction?
function individual_excerpt_more( $more ) {
if {
function word_count() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
return $word_count; <45 // (less than 45 words)
}
&& $content = $post->post_content;
if( has_shortcode( $content, 'gallery', 'video' ) ) {
// The content has a [gallery] & [video] short code, so this check returned true.
}
return '… <ins><a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read more</a></ins>';
}
else {
function theme_excerpt_length( $length ) {
return 45;
}
add_filter( 'excerpt_length', ’theme_excerpt_length' );
}
add_filter( 'excerpt_more', 'individual_excerpt_more' );
I want to replace [...] with read more link:
function replace_excerpt($content) {
return str_replace('[...]',
'Read More →',
$content
);
}
add_filter('the_excerpt', 'replace_excerpt');
I don't know why it's not work for me!
I call the excerpt with this:
<?php
if ( is_singular() ) {
the_content();
} else {
the_excerpt();
}
?>
What's the problem?
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">...Read More</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );