Reputation: 17
The shortcode I'm trying to create should return the 1 Hungarian word described below, this should change month to month. The code has been placed into functions.php and returns nothing when the shortcode is used in a page or post. Any help would be much appreciated. It does work when used as simple php and html by using echo honapnev($honap);
.
$honap = date("n");
function honapnev_shortcode( $ho ) {
if ($ho==1) {
return "Januári";
}
elseif ($ho==2) {
return "Februári";
}
elseif ($ho==3) {
return "Márciusi";
}
elseif ($ho==4) {
return "Áprilisi";
}
elseif ($ho==5) {
return "Májusi";
}
elseif ($ho==6) {
return "Júniusi";
}
elseif ($ho==7) {
return "Júliusi";
}
elseif ($ho==8) {
return "Augusztusi";
}
elseif ($ho==9) {
return "Szeptemberi";
}
elseif ($ho==10) {
return "Októberi";
}
elseif ($ho==11) {
return "Novemberi";
}
elseif ($ho==12) {
return "Decemberi";
}
}
add_shortcode( 'honapnev', 'honapnev_shortcode' );
When I remove the line add_shortcode( 'honapnev', 'honapnev_shortcode' );
the shortcode in brackets appears on the page...
Upvotes: 0
Views: 167
Reputation: 4228
function honapnev_shortcode($atts)
{
switch (date("n"))
{
case 1:
{
$month="Januári";
break;
}
case 2:
{
$month="Februári";
break;
}
case 3:
{
$month="Márciusi";
break;
}
case 4:
{
$month="Áprilisi";
break;
}
case 5:
{
$month="Májusi";
break;
}
case 6:
{
$month="Júniusi";
break;
}
case 7:
{
$month="Júliusi";
break;
}
case 8:
{
$month="Augusztusi";
break;
}
case 9:
{
$month="Szeptemberi";
break;
}
case 10:
{
$month="Októberi";
break;
}
case 11:
{
$month="Novemberi";
break;
}
case 12:
{
$month="Decemberi";
break;
}
default:
{
$month="Januári";
break;
}
}
return $month;
}
add_shortcode('honapnev', 'honapnev_shortcode');
or
function honapnev_shortcode($atts)
{
$months=array("Januári", "Februári", "Márciusi", "Áprilisi", "Májusi","Júniusi", "Júliusi", "Augusztusi","Szeptemberi", "Októberi", "Novemberi", "Decemberi");
return $months[date("n")-1];
}
add_shortcode('honapnev', 'honapnev_shortcode');
Upvotes: 0
Reputation: 1429
You need to parse the shortcode attributes in your function first, also, the elseif or switch statements can be replaced to get a short function.
function honapnev_shortcode($atts)
{
/**
* Get the shortcode attributes default value = 1
*/
$atts = shortcode_atts(array(
'ho' => 1,
), $atts, 'honapnev');
$ho = intval($atts['ho']);
/**
* Is better use an Array in this case
*/
$words = ['Januári', 'Februári', 'Márciusi', 'Áprilisi', 'Májusi', 'Júniusi', 'Júliusi', 'Augusztusi', 'Szeptemberi', 'Októberi', 'Novemberi', 'Decemberi'];
return $words[$ho - 1];
}
add_shortcode('honapnev', 'honapnev_shortcode');
The shortcode
[honapnev]
[honapnev ho="1"]
[honapnev ho="6"]
[honapnev ho="12"]
Upvotes: 0