read more excerpt wordpress - wordpress

I have reviewed the other posts similar to my questions but I must be missing a something.
I have added excerpts to my page like so:
[title size="2"]LATEST ARTICLES[/title]
[recent_posts columns="4" number_posts="12" cat_id="" thumbnail="yes"  excerpt="yes" title="yes"] [/recent_posts]
I am trying to do two things
set the length of the excerpt
make it link to the article (not necessarily a read more link the dots are fine)
I have tried to add the excerpt manually to the excerpt area (screen options) for the page but that is not overwritting what Wordpress is pulling from.
in my function.php file I am using
add_filter('the_excerpt', 'do_shortcode');
which works, but when I tried to add additional code below it does not do anything. Do I need to modify anothe rphp file to get this to register?
`enter code here`function new_excerpt_more($more) {
return '' . ' read on ..' . '';
}
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_length($length) {
return 46;
}
add_filter('excerpt_length', 'new_excerpt_length');

I have achieved your desired result w/ the following code (from the codex):
function custom_excerpt_length( $length ) {
return 46;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

Related

How to filter WooCommerce page title?

I want to filter the shop page title by adding <span>-tags within the <h1>.
In the archive-product.php-file, the following code is present:
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
I don't really understand how I can use this filter. I could add <span> with a custom templatembut I want to know if this filter could be used instead. Or is it only for actually displaying the title?
When looking into the actual function woocommerce_page_title it does seem like it can be filtered:
$page_title = apply_filters( 'woocommerce_page_title', $page_title );
But when I add a filter to woocommerce_page_title, nothing happens. I have tried adding it with add_action('plugins_loaded') and add_action('init') but it doesn't work, for example:
function lcp_wc_filters() {
add_filter('woocommerce_page_title',function($page_title) {
return '<span>' . $page_title . '</span>';
});
}
add_action('plugins_loaded','lcp_wc_filters');
use this code
function filter_woocommerce_show_page_title( $array, $int ) {
return '<h1 class="woocommerce-products-header__title page-title"><span>'.implode(' ', $array).'</span></h1>';
};
add_filter( 'woocommerce_show_page_title', 'filter_woocommerce_show_page_title', 10, 2 );
The following worked with the 'init'-hook:
function filter_woocommerce_page_title( $page_title ) {
return '<span>'. $page_title . '</span>';
};
function lcp_wc_hooks() {
add_filter( 'woocommerce_page_title', 'filter_woocommerce_show_page_title', 10, 2 );
}
add_action('init','lcp_wc_hooks');

How to Add Multiple Filters in Function.php?

I'm Using Genesis Framework on my website. I wanted to add Custom CSS Classes to my website's Navigation Menu and the Primary Sidebar.
To make this happen, I used the following code:
add_filter( 'genesis_attr_nav-primary', 'themeprefix_add_css_attr' );
function themeprefix_add_css_attr( $attributes ) {
$attributes['class'] .= ' toggle';
return $attributes;
}
And a new class named "toggle" was added to Navigation Primary.
But When I added the code in Function.php for adding a new CSS class in Primary Sidebar as following, my website showed error 500.
add_filter( 'genesis_attr_sidebar-primary', 'themeprefix_add_css_attr' );
function themeprefix_add_css_attr( $attributes ) {
$attributes['class'] .= 'toggle2';
return $attributes;
}
Looks like you're defining two functions with the same name, which will cause an error. Try the following:
add_filter( 'genesis_attr_nav-primary', 'themeprefix_add_nav_css' );
function themeprefix_add_nav_css( $attributes ) {
$attributes['class'] .= ' toggle';
return $attributes;
}
add_filter( 'genesis_attr_sidebar-primary', 'themeprefix_add_sidebar_css' );
function themeprefix_add_sidebar_css( $attributes ) {
$attributes['class'] .= 'toggle2';
return $attributes;
}
Note that each filter is referencing a different function: themeprefix_add_nav_css and themeprefix_add_sidebar_css.

add Custom text field in single product page

I am working on a project in woocommerce.
Can anyone help me to add a custom text field next to "add to cart"?
I tried the following but it is not working.
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post);
echo "</div>";
return true;
}
Perhaps try using $post->ID like this:
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post->ID);
echo "</div>";
return true;
}
put your html code inside content-product.php (wp-content/themes/YOUR THEME/woocommerce/content-product.php)
content-product.php contains code of list page in wordpress. if you want to edit product page than edit content-single-product.php.
You are using Woocommerce there are two case :
You are reading post ID
You want to display a SKU
You can implement it via single function :
add_action( 'woocommerce_single_product_summary', 'display_productCode', 5 );
function display_productCode(){
global $product;
echo 'SKU: ' . $product->get_sku();
echo 'Product ID:'.$product->get_id();
}

How to make the [...] in the get_the_excerpt() as a hyperlink

I want to make the [...] displayed in posts page as hyperlink to that particular posts. I get to know that this is from get_the_excerpt().
How can this be achieved? Thanks in advance.
copy this function to your functions.php
function new_excerpt_more($more) {
global $post;
return '<span class="readmore"><a class="moretag" href="'. get_permalink($post->ID) . '"> Read more..</a></span>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Why can't I override WP's 'excerpt_more' filter via my child theme functions?

I can't seem to get my functions to work for changing the excerpt_more filter of the Twenty Eleven parent theme.
I suspect it might actually be add_action( 'after_setup_theme', 'twentyeleven_setup' ); that's the problem, but I've even tried remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' ) to get rid Twenty Eleven's function and still my functions aren't changing anything...
Can you help?
Here's the functions.php code in full:
http://pastie.org/3758708
Here's the functions I've added to /mychildtheme/functions.php
function clientname_continue_reading_link() {
return ' ' . __( 'Read more... <span class="meta-nav">→</span>', 'clientname' ) . '';
}
function clientname_auto_excerpt_more( $more ) {
return ' …' . clientname_continue_reading_link();
}
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more' );
Thanks,
Osu
Ok, so after much frustration, I found a solution to this (I thought Child Themes were meant to speed things up!?). I believe this works because 'after_theme_setup' is run once the parent theme has been set up meaning you can remove / override Twenty Eleven's functions at that point.
If I've understood correctly, according to this documentation, the Child Theme is run first, then the parent and then the 'after_theme_setup' bit of code in your Child Theme's functions.php file:
http://codex.wordpress.org/Child_Themes#Using_functions.php
and
http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme
This is what's in my Child Theme's functions.php file, hope this helps someone:
// ------------------------------------------------------------------
// // !AFTER_SETUP_THEME
// ------------------------------------------------------------------
/* Set up actions */
add_action( 'after_setup_theme', 'osu_setup' );
if ( ! function_exists( 'osu_setup' ) ):
function osu_setup() {
// OVERRIDE : SIDEBAR GENERATION FUNCTION - NO WIDGETS FOR THIS SITE
remove_action( 'widgets_init', 'twentyeleven_widgets_init' ); /* Deregister sidebar in parent */
// OVERRIDE : EXCERPT READ MORE LINK FUNCTION
function osu_readon_link() {
return '...' . __( 'Read More...', 'clientname' ) . '';
}
// Function to override
function osu_clientname_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
// $output = trim($output);
$output .= osu_readon_link();
}
return $output;
}
remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
add_filter( 'get_the_excerpt', 'osu_clientname_custom_excerpt_more' );
remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
add_filter( 'excerpt_more', 'osu_readon_link' );
// OVERRIDE : EXCERPT LENGTH FUNCTION
function osu_clientname_excerpt_length( $length ) {
return 30;
}
remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
add_filter( 'excerpt_length', 'osu_clientname_excerpt_length' );
}
endif; // osu_setup
Your own answer is complicating things and really shouldn't be necessary. I couldn't explain the why of my answers, because I found it in another answer. But in any case you can ALWAYS override functions from the parent theme in your child theme, although sometimes indeed you to use the remove_filter() beforehand, OR, like in this case, all you have to do is increase the priority of your added filter, in your case:
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more', 11 );
That should do the trick. If not, increase the number.
Thanks to this answer:

Resources