Php Wordpress:数组中基于类别的随机短码
我试图通过建立一个附属网站来销售,在这个假期为我们的单收入家庭赚取一些额外的钱。然而,经过WordPress论坛的大量研究和少量帮助后: 我迫切需要一些PHP代码,以便根据条件类别从数组中随机选择一个do_短代码-类似于小部件逻辑插件,但用于短代码 我目前正在使用:Php Wordpress:数组中基于类别的随机短码,php,wordpress,random,conditional,shortcode,Php,Wordpress,Random,Conditional,Shortcode,我试图通过建立一个附属网站来销售,在这个假期为我们的单收入家庭赚取一些额外的钱。然而,经过WordPress论坛的大量研究和少量帮助后: 我迫切需要一些PHP代码,以便根据条件类别从数组中随机选择一个do_短代码-类似于小部件逻辑插件,但用于短代码 我目前正在使用: <?php echo do_shortcode("[asle id=15]"); ?> 在www.AvengersCollectibles.com/wp上的single.php页面中,显示我的“Azon Pro购
<?php echo do_shortcode("[asle id=15]"); ?>
在www.AvengersCollectibles.com/wp上的single.php页面中,显示我的“Azon Pro购物清单”的快捷码,该清单显示在我的单个帖子页面底部,但需要一些代码,以便根据9个类别/字符中的每一个,从数组中随机显示5个此类清单中的1个
到目前为止,建议采取如下措施:
<?php $ids = array( 43, 15, 8 ); // Default
if (in_category('Captain America')):
$ids = array( 15, 16, 17, 19, 20 );
elseif (in_category('Spiderman')):
$ids = array( 35, 36, 37, 39, 50, 5, 70 );
elseif (in_category('Green Hornet')):
$ids = array( 75, 6, 22, 49 );
endif;
$id = 0;
if ( ! $id ) {
$key = array_rand( $ids, 1 );
$id=$ids[$key];
echo do_shortcode("[asle id=$id]");
}
?>
但我一直收到以下错误消息:
方法未实现
不支持访问/wp/wp admin/theme-editor.php。
此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误
任何帮助都将不胜感激,我欢迎您的所有评论、关注或问题
PS:如果您能够成功解决此问题,我将乐意在必要时提供访问权限,并在我出售网站时提供版税。如果我将此放在我的
函数中。php
:
add_shortcode( 'asle', 'so_13771287_random_shortcode' );
function so_13771287_random_shortcode( $atts )
{
return 'Random ID = ' . $atts['id'];
}
<?php $ids = array( 43, 15, 8 ); // Default
if (in_category('Captain America')):
$ids = array( 15, 16, 17, 19, 20 );
elseif (in_category('Spiderman')):
$ids = array( 35, 36, 37, 39, 50, 5, 70 );
elseif (in_category('Green Hornet')):
$ids = array( 75, 6, 22, 49 );
endif;
$id = 0;
if ( ! $id ) {
$key = array_rand( $ids, 1 );
$id=$ids[$key];
echo do_shortcode("[asle id=$id]");
}
?>
以及您在single.php
中的确切代码:
add_shortcode( 'asle', 'so_13771287_random_shortcode' );
function so_13771287_random_shortcode( $atts )
{
return 'Random ID = ' . $atts['id'];
}
<?php $ids = array( 43, 15, 8 ); // Default
if (in_category('Captain America')):
$ids = array( 15, 16, 17, 19, 20 );
elseif (in_category('Spiderman')):
$ids = array( 35, 36, 37, 39, 50, 5, 70 );
elseif (in_category('Green Hornet')):
$ids = array( 75, 6, 22, 49 );
endif;
$id = 0;
if ( ! $id ) {
$key = array_rand( $ids, 1 );
$id=$ids[$key];
echo do_shortcode("[asle id=$id]");
}
?>
它确实对我有用。是的,我为测试创建了所有超级英雄类别
短代码从每个访问的帖子的对应类别中打印出一个随机数。您应该在“if”和“elseif”之后要执行的代码周围加上大括号。谢谢您或您的快速回复。如果我理解正确,更像这样:@ChrisHerbert-没有大括号,这与问题无关……GWMO,根据我下面的回答:这些错误不是来自您所演示的代码。除非是在短码功能中,否则这不会出现在你的问题中。亲爱的brasofilo,你绝对是这个节日的祝福。除了一个小问题外,它的功能似乎完美无缺。我注意到,当加载新页面时,我可以在新页面的顶部看到function.php代码,直到它完全加载为止。我已经在使用WP Super Cache了,还有其他建议吗?我在我的子主题中添加到functions.php文件的上述代码会在每次加载页面时出现在屏幕顶部,时间为1-2秒:屏幕截图