Archived topic
php function / filter apply to one specific page / category
7 replies · Started by Manuel on May 18, 2018
Hi all,
I've found this code
if ( ! function_exists( 'generate_custom_blog_excerpt_more' ) ) :
/**
* Prints the read more HTML
*/
add_filter( 'excerpt_more', 'generate_custom_blog_excerpt_more', 100 );
add_filter( 'the_content_more_link', 'generate_custom_blog_excerpt_more', 100 );
function generate_custom_blog_excerpt_more( $more ) {
$generate_settings = wp_parse_args(
get_option( 'generate_blog_settings', array() ),
generate_blog_get_defaults()
);
return '<div class="read-more-button"><a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . $generate_settings['read_more'] . '</a></div>';
}
endif;
Now I want to use it on a few specific sites / categories. Any chance to achieve this with the ID or the SLUG?
I'm using "Code Snippets" to add php.
Thanks in advance
Hi there,
Not quite sure if I understand, are you trying to set custom read more text?
If so this should help:
https://docs.generatepress.com/article/option_generate_blog_settings/#options-%E2%80%98read_more%E2%80%99
You can see an example on how to use this filter here:
https://docs.generatepress.com/article/option_generate_blog_settings/#examples
and conditional tags are listed here:
https://codex.wordpress.org/Conditional_Tags
Let me know if this helps :)
Hi Leo,
the code works very well and does everything I need. But I want the code to apply just to a specific site (f.e. contact) and not sitewide.
In css I could do it with f.e. .page-id-67 {any code in here} but how can I achieve it in PHP?
You got my point?
Does this help? https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
Thanks Leo.
But how can I wrap the code from above in it?
I've tried this but it doesn't work:
is_category( '9' )
if ( ! function_exists( 'generate_custom_blog_excerpt_more' ) ) :
/**
* Prints the read more HTML
*/
add_filter( 'excerpt_more', 'generate_custom_blog_excerpt_more', 100 );
add_filter( 'the_content_more_link', 'generate_custom_blog_excerpt_more', 100 );
function generate_custom_blog_excerpt_more( $more ) {
$generate_settings = wp_parse_args(
get_option( 'generate_blog_settings', array() ),
generate_blog_get_defaults()
);
return '<div class="read-more-button"><a href="'. get_permalink( get_the_ID() ) . '">' . $generate_settings['read_more'] . '</a></div>';
}
endif;
For custom read more page in a specific category?
Then try the option plus filter I suggested above:
add_filter( 'option_generate_blog_settings', 'lh_custom_read_more_text' );
function lh_custom_read_more_text( $options ) {
if ( is_category( '9' ) ) {
$options['read_more'] = 'My custom read more label'
}
return $options;
}
Ah, thanks Leo, you're the best :-)
Glad I could help :)