Search Results for 'is_paged()'

Home Forums Search Search Results for 'is_paged()'

Viewing 15 results - 1 through 15 (of 50 total)
  • Author
    Search Results
  • #1618664
    David

    OK so here is a simple Element Header:

    <h1>{{post_title}}</h1>
    <p>{{custom_field.description}}</p>
    <p>
    Subscribe Now
    </p>

    I can change this to

    <h1>{{post_title}}</h1>
    <p>[show_excerpt]</p>
    <p>
    Subscribe Now
    </p>

    and write a shortcode that returns the excerpt on the first page and otherwise return empty

    Something like this:

    add_shortcode( ‘show_excerpt’, function() {
    ob_start();
    if( is_archive() && !is_paged() ) {
    echo get_the_excerpt();
    }
    return ob_get_clean();
    } );

    But this shortcode needs work!

    #1618663
    David
    Staff
    Customer Support

    Unless there is a fundamental SEO issue i don’t see what the problem is here, as we’re only dealing with text that only requires a simple database query.

    But there are alternatives, one of which is to create a shortcode to output the description and only output it on non paged archives eg.

    add_shortcode( 'term_description', 'tu_term_description' );
    function tu_term_description() {
      if (!is_paged()) {
        ob_start();
        echo term_description( get_queried_object()->term_id, 'category' );
        return ob_get_clean();
      }
    } 

    Which will provide you with this shortcode [term_description]

    #1589750
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, you still need both functions:

    function wpsites_query( $query ) {
        if ( $query->is_paged() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', 8 );
        }
    }
    add_action( 'pre_get_posts', 'wpsites_query' );
    
    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 8;
    
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    This is something I wish WordPress had built-in support for, it’s certainly far too complex to achieve.

    #1583386
    Tom
    Lead Developer
    Lead Developer

    Hmm, I found this: https://wordpress.stackexchange.com/questions/324836/using-the-posts-pagination-with-offset-adds-extra-empty-page

    So instead of the second function you’re using (there are two separate functions in the thread we linked to), try this one:

    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 3;
    
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    Just change $first_page_ppp to the correct number of posts on the first page.

    jaiji

    OK, I must be getting something wrong with the filter, or it doesn’t do what I need. Here’s how I set it up, theoretically targeting all archives but the 2 listed, and replicating the other customizer settings for the blog:

    add_filter( 'option_generate_blog_settings', 'jc_custom_archive_page_settings' );
    function jc_custom_archive_page_settings( $options ) {
    	if ( is_paged() && ! is_category( array( 'white-papers', 'ebooks' ) ) ) {
    	$options['read_more_button'] = true;
    	$options['date'] = false;
    	$options['categories'] = true;
    	$options['tags'] = false;
    	$options['comments'] = false;
            $options['infinite_scroll'] = false;
    	$options['infinite_scroll_button'] = false;
    	$options['masonry_load_more'] = 'More search results';
    	$options['post_image'] = true;
    	$options['post_image_position'] = 'post-image-above-header';
    	$options['post_image_alignment'] = 'post-image-aligned-center';
    	$options['column_layout'] = true;
    	$options['featured_column'] = true;
    	$options['masonry'] = false;
        }
      
        return $options;
    }

    This doesn’t add the featured-column class to the top post on paged pages, which is all I’d like to do. Does this filter option only add that class to the most recent post in the entire archive i.e. the first post on the first page? If so it’s redundant here as that’s happening already. I need it to target whichever post is most recent on a given paged page, if that’s even possible? If not I’ll stick to CSS for this.

    I successfully got the 2 archives into a single column layout, thank you, they just need some CSS now, which I’m happy to sort out.

    #1575502
    Ingrid

    Hi Everyone

    I have been trying to fix an issue for days but didn’t manage to figure this out.

    I am using the 2 columns layout + one sticky post on the top. If I had 8 posts showing in each page it would leave one post alone in the last column of the homepage so I set the post per page in the settings as 7 and I am using a code to leave 8 posts in all other pages that doesn’t have a sticky post, so the pages with a sticky post are like this and the other pages are like this

    This is the code I am using:

    function wpsites_query( $query ) {
    if ( $query->is_paged() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', 8 );
        }
    }
    add_action( 'pre_get_posts', 'wpsites_query' );
    

    It works fine and solved the issue with the amount of posts to show but the new problem I got is that the pagination does not count the number of pages correctly, for example in the homepage it shows < 1-2..29> and in the other pages it shows <1-2..26>, the page with the sticky post is showing more pages than the site actually has, therefore I get 404 not found error when clicking on the last page.

    The quick fix I found for now is to hide the last pagination page number by CSS but that is not ideal

    Thanks in advance!

    #1538468
    Leo
    Staff
    Customer Support

    In that case you would need this PHP snippet:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id && is_paged() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Replace 123 with the actual element ID.

    We will be adding this in the display rules in the next version of GP Premium 🙂

    #1517258

    In reply to: Duplicate H1 Tages

    David
    Staff
    Customer Support

    Try this instead:

    <?php 
    if ( is_paged() ) {
        // If paginated page output title with current page
        $current_page = max(1, get_query_var('paged'));
        echo '<p class=”main-title” itemprop=”headline”>Dark Stories True Crime Blog - Page ' . $current_page .  '</p>';
      
    } else {
        echo '<h1 class=”main-title” itemprop=”headline”>Dark Stories True Crime Blog</h1>';
    }
    ?>
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this, instead:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
      if ( 123 === $element_id && is_paged() ) {
         $display = false;
      }
    
      return $display;
    }, 10, 2 );
    #1506209
    Brian

    To Remove Archive Description Paginated

    add_filter( ‘generate_header_element_display’, function( $display ) {
    if ( is_paged() ) {
    $display = false;
    }

    return $display;
    } );

    add_action( ‘wp’, ‘tu_remove_paged_category_description’ );
    function tu_remove_paged_category_description() {
    $page = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    if ( 1 !== $page ) {
    remove_action( ‘generate_archive_title’, ‘generate_archive_title’ );
    add_action( ‘generate_archive_title’, ‘tu_custom_paged_archive_title’ );
    }
    }

    function tu_custom_paged_archive_title() {
    ?>
    <header class=”page-header”>
    <h1 class=”page-title”>
    <?php the_archive_title(); ?>
    </h1>
    </header>
    <?php
    }

    Alessandro

    Thanks David, but the modify don’t show.
    I use a child theme, but strangely, the css and the function.php changes are not read either.
    Currently in the child there is this content:

    
    <?php
    /**
     * GeneratePress child theme functions and definitions.
     *
     * Add your custom PHP in this file.
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
     */
    function generatepress_child_enqueue_scripts() {
    	if ( is_rtl() ) {
    		wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
    
    add_filter( 'generate_google_font_display', function() {
        return 'swap';
    } );
    
    function no_self_ping( &$links ) {
        $home = get_option( 'home' );
        foreach ( $links as $l => $link )
            if ( 0 === strpos( $link, $home ) )
                unset($links[$l]);
    }
     
    add_action( 'pre_ping', 'no_self_ping' );
    
    add_filter( 'generate_block_element_display', function( $display ) {
      if ( 434 === $element_id && is_paged() ) {
         $display = false;
      }
    
      return $display;
    } );
    
    David
    Staff
    Customer Support

    Hi there,

    you can add this PHP Snippet to exclude a specific Block Element when visiting a paged instance:

    add_filter( 'generate_block_element_display', function( $display ) {
      if ( 123 === $element_id && is_paged() ) {
         $display = false;
      }
    
      return $display;
    } );

    The 123 has to be replaced with the Block Elements ID – you can get this by editing the Block element and checking the URL in the browser.

    Leo
    Staff
    Customer Support

    It should work just with a different filter:

    add_filter( 'generate_block_element_display', function( $display ) {
        if ( is_paged() ) {
           $display = false;
        }
    
        return $display;
    } );

    Let me know 🙂

    David
    Staff
    Customer Support

    Hi there,

    use this for your template condition:

    if ( is_front_page() && !is_paged() )

    Jiri

    Your PHP snippet took me to an interesting coding “trip”. 🙂 I have already resolved the first page before I wrote here. But the snippet, after some changes, is also usable to add new CSS classes into paged parts of infinite scroll. After some hard investigation I get to this version of PHP and jQuery codes.

    I changed the PHP snippet to use current page number to generate new classes for infinite scroll pages

    
    add_filter( 'post_class', function( $classes ) {
        
    	if ( is_paged() ) {
    	    $pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$classes[] = 'page-'.$pagenum.'-post';
    	}
    	
    	return $classes;
    	
    });
    

    jQuery script adding the tracking code I changed to use the new classes with current page number (and added also new functionality – add target _blank to the links)

    
    <script type="text/javascript">
    jQuery( document ).ready( function($) {
        var $container = $( '#main article' ).first().parent();
    
        $container.on( 'append.infiniteScroll', function( event, response, path, items ) {
            var $infScroll = $container.data('infiniteScroll');  //current page number
    	var pagenum = $infScroll.pageIndex;
    	var aclass1 = '.page-' + pagenum + '-post .post-image a'; //create css classes based on current page number
    	var aclass2 = '.page-' + pagenum + '-post h2.entry-title a';
      
    		//select all classes and set target _blank
            	document.querySelectorAll(aclass1 + "," + aclass2) 
            	.forEach(function(elem) {
            	elem.setAttribute('target', '_blank');
            	});
               
    			//select all classes and add tracking code prefix to href 			
    			$(aclass1 + "," + aclass2).one('click',function(){  
               		url=$(this).attr('href');
               		$(this).attr('href','/tracking/track.php?u=' + escape(url));
               		});
        } );
    } );
    </script>
    

    Thank you very much for the PHP snippet. It helped me a lot.
    Now it seems to be working fine. But I appreciate your possible suggestions to the new code.

    Have a nice rest of the weekend

Viewing 15 results - 1 through 15 (of 50 total)