【问题标题】:Page navigation stopped working after updating Wordpress?更新 Wordpress 后页面导航停止工作?
【发布时间】:2018-06-17 15:40:05
【问题描述】:

我不久前使用代码向客户网站添加了页面导航:

<?php if ($paged > 1) { ?>
    <nav id="nav-posts">
        <?php
        global $wp_query;

     $big = 999999999; // need an unlikely integer

   echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
 ) );
  ?>
    </nav>

    <?php } else { ?>

这一直很好,直到我将 Wordpress 的版本更新为 4,现在每次我点击转到新页面时它都不会重定向,而是停留在同一页面上,即使 url 指向 http://jenningsrugs.co.uk/pages/?page_id=2703#038;paged=3

可以帮忙吗?

完整代码在这里:

   <?php
/*
Template Name: Runners Template
*/

get_header();
?>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="../../../images/blank_middle/blank_middle_01.jpg" width="57" height="20"></td>
    <td><img src="../../../images/blank_middle/blank_middle_02.jpg" width="886" height="20"></td>
    <td><img src="../../../images/blank_middle/blank_middle_03.jpg" width="57" height="20"></td>
  </tr>
  <tr>
    <td background="../../../images/blank_middle/blank_middle_04.jpg"><img src="../../../images/spacer.gif" width="57" height="416" /></td>
    <td valign="top" background="../../../images/blank_middle/blank_middle_05.jpg"><table width="886" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><img src="../../../images/spacer.gif" width="30" height="1" /></td>
          <td><img src="../../../images/spacer.gif" width="826" height="1" /></td>
          <td><img src="../../../images/spacer.gif" width="30" height="1" /></td>
        </tr>
        <tr>
          <td><img src="../../../images/spacer.gif" width="30" height="10" /></td>
          <td align="left">
          <div id="content" class="narrowcolumn" role="main">

        <?php if (have_posts()) : ?>




        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>

<h2 class="pagetitle"><?php echo get_the_title(); ?></h2>
<?php echo do_shortcode( '[searchandfilter id="2706"]' ); ?>
<?php echo do_shortcode( '[searchandfilter id="2706" show="results"]' ); ?>
<a class="reset" href="#" onclick="$('select').each(function() { this.selectedIndex = 0 });">Reset search</a>
    <?php // Display blog posts on any page @ http://m0n.co/l
        $temp = $wp_query; $wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('cat=24' . '&paged='.$paged);
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

        <div <?php post_class() ?>>
                <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>


                <div class="entry">
                    <?php the_content() ?>
                </div>

                <br>

        </div>

        <?php endwhile; ?>

        <?php if ($paged > 1) { ?>

        <nav id="nav-posts">
            <?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>
        </nav>

        <?php } else { ?>

        <nav id="nav-posts">
            <?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>
        </nav>

        <?php } ?>

        <?php wp_reset_postdata(); ?>
    <?php else :

        if ( is_category() ) { // If this is a category archive
            printf("<h2 class='center'>Sorry, but there aren't any posts in the %s category yet.</h2>", single_cat_title('',false));
        } else if ( is_date() ) { // If this is a date archive
            echo("<h2>Sorry, but there aren't any posts with this date.</h2>");
        } else if ( is_author() ) { // If this is a category archive
            $userdata = get_userdatabylogin(get_query_var('author_name'));
            printf("<h2 class='center'>Sorry, but there aren't any posts by %s yet.</h2>", $userdata->display_name);
        } else {
            echo("<h2 class='center'>No posts found.</h2>");
        }
        get_search_form();

    endif;
?>

    </div>
          </td>
          <td><img src="../../../images/spacer.gif" width="30" height="10" /></td>
        </tr>
    </table></td>
    <td background="../../../images/blank_middle/blank_middle_06.jpg"><img src="../../../images/spacer.gif" width="57" height="416" /></td>
  </tr>
</table>
<?php get_footer(); ?>

【问题讨论】:

标签: php wordpress pagination


【解决方案1】:

此问题的解决方案是使用替换。

$big = 999999999; // need an unlikely integer
$search_for   = array( $big, '#038;' );
$replace_with = array( '%#%', '&' );
echo paginate_links( array(
'base' => str_replace( $search_for, $replace_with, esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );

【讨论】:

    【解决方案2】:

    编辑:

    $search_for   = array( $big, '#038;', '&&' );
    $replace_with = array( '%#%', '&', '&' );
    

    【讨论】:

    • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多