[Resolved] php function / filter apply to one specific page / category

Home Forums Support php function / filter apply to one specific page / category

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #578664
    Manuel

    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

    GeneratePress 2.1.2
    GP Premium 1.6.2
    #578965
    Leo
    Staff
    Customer Support

    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 🙂

    #578975
    Manuel

    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?

    #579011
    Leo
    Staff
    Customer Support
    #579845
    Manuel

    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;
    #580160
    Leo
    Staff
    Customer Support

    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;
    }
    #580553
    Manuel

    Ah, thanks Leo, you’re the best 🙂

    #580866
    Leo
    Staff
    Customer Support

    Glad I could help 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.