WordPress.org

Ready to get started?Download WordPress

Forums

Cann't add custom code to functions.php (4 posts)

  1. Zachis.it
    Member
    Posted 1 year ago #

    Hello,

    I am trying to add this function to make pagination in pages

    <?php
    function pagination($pages = '', $range = 4)
    {
    $showitems = ($range * 2)+1;
    global $paged;
    if(empty($paged)) $paged = 1;
    if($pages == '')
    {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }
    if(1 != $pages)
    {
    echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "« First";
    if($paged > 1 && $showitems < $pages) echo "‹ Previous";
    for ($i=1; $i <= $pages; $i++)
    {
    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
    }
    }

    if ($paged < $pages && $showitems < $pages) echo "Next ›";
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "Last »";
    echo "</div>\n";
    }
    }?>

    But I get this error when saving the file

    Warning: Cannot modify header information - headers already sent by (output started at /home/zacharyr/public_html/client/band/wp-content/themes/clean-copy-left-sidebar-1/functions.php:34) in /home/zacharyr/public_html/client/band/wp-includes/pluggable.php on line 881

  2. mokah
    Member
    Posted 1 year ago #

    Ensure the code is pasted within the confines of the original <?php></?> tags in your functions.php file. Make sure you remove the tags from the code itself as the functions.php file will already have them.

  3. Zachis.it
    Member
    Posted 1 year ago #

    <?php if ( function_exists('register_sidebar') ) register_sidebar();?>

    this is the only function in the sheet. Where should i add the code ?

  4. mokah
    Member
    Posted 1 year ago #

    Like this, I guess:

    <?php 
    
    if ( function_exists('register_sidebar') ) register_sidebar();
    
    function pagination($pages = '', $range = 4)
    {
    $showitems = ($range * 2)+1;
    global $paged;
    if(empty($paged)) $paged = 1;
    if($pages == '')
    {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }
    if(1 != $pages)
    {
    echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "« First";
    if($paged > 1 && $showitems < $pages) echo "‹ Previous";
    for ($i=1; $i <= $pages; $i++)
    {
    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
    }
    }
    
    if ($paged < $pages && $showitems < $pages) echo "Next ›";
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "Last »";
    echo "</div>\n";
    }
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic