早些时候,我问this question 并收到一个应该可以工作的代码。不幸的是,此代码不起作用。
我正在构建一个主题选项面板,希望用户能够在我的存档和主页中的下一个/上一个链接或分页之间进行选择。
内的代码functions.php
正在控制选项面板下拉列表,允许用户选择分页或下一个/上一个链接。
This code is displaying Next/Previous links at all times, even when I choose pagination. I need the pagination option to work too!
我有什么
functions.php
array( "name" => "Paginate or next/previous links?",
"desc" => "Choose your option",
"id" => $shortname."_next_prev_or_paginate",
"type" => "select",
"options" => array("Next", "Pagination"),
"std" => "Next"),
我有什么
archives.php
<?php
function my_theme_navigation()
{
global $shortname;
if( get_option( $shortname . \'_next_prev_or_paginate\' ) == \'Next\' ) :
// the block for next-prev navigation
previous_posts_link (\'Newer\');
next_posts_link(\'Older\');
else :
// the block for pagination
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
\'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages
)
);
endif;
}
?>
所有相关代码原始分页代码(已与上一页/下一页组合)
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages
) );
?>
与分页相结合的上一页/下一页
<?php previous_posts_link (\'Newer\') ?>
<?php next_posts_link(\'Older\') ?>
主题选项的函数,在分页和下一个/上一个链接之间进行选择
array( "name" => "Paginate or next/previous links?",
"desc" => "Choose your option",
"id" => $shortname."_next_prev_or_paginate",
"type" => "select",
"options" => array("Next", "Pagination"),
"std" => "Next"),
最合适的回答,由SO网友:brasofilo 整理而成
1) 将函数移到functions.php
文件,它就属于那里。在任何结束之前把它放在末尾?>
:
function my_theme_navigation()
{
global $shortname;
if( get_option( $shortname . \'_next_prev_or_paginate\' ) == \'Next\' ) :
// the block for next-prev navigation
previous_posts_link (\'Newer\');
next_posts_link(\'Older\');
else :
// the block for pagination
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
\'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages
)
);
endif;
}
2) 看起来像
only thing 你是
missing 是
function call, 那会
print the navigation.
不太熟悉
archives.php
, 所以使用
single.php
例如:
<?php
/**
* The Template for displaying all single posts.
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<nav id="nav-single">
<?php my_theme_navigation(); /* CALLING THE FUNCTION TO ECHO THE NAVIGATION */ ?>
</nav><!-- #nav-single -->
<?php get_template_part( \'content\', \'single\' ); ?>
<?php comments_template( \'\', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
原始答案假设
If, 正如问题所说,
此代码显示下一个/上一个链接all times
这很可能意味着if/else
不工作。
期权价值Supposing$shortname
等于"shortname"
, 我在表中创建了一个选项wp_options
.
测试Putting中的此代码header.php
, 在…内<div id="main">
从2011年起:
function my_theme_navigation() {
if( \'Next\' == get_option( \'shortname_next_prev_or_paginate\' ) ) :
// the block for next-prev navigation
echo \'Previous/Next selected.\';
else :
// the block for pagination
echo \'Pagination selected.\';
endif;
}
my_theme_navigation();
结果和可能的解决方案
It does 在数据库中手动更改选项值时正确回显。
确保以下功能/命令按此顺序工作:
get_option
, 它抓住了价值吗?数据库中是否存在该值paginate_links
, 如果单独调用,打印是否正确?没有get_option
和if/else
?if/else
, 如果前一个有效gotta 正在工作