0

I have a custom field with checkboxes. I created a code to show post-typs filtered by checkbox value with shortcodes e.g. [bettenaert-Querbett]. The code works but do not look nice.

if ($i = 'Querbett') {
    add_shortcode( "bettenaert-{$i}", function() use ( $i ) {
    modell_nach_bettenart_2( $i );
        } );
}
if ( $i = 'Alkovenbett' ) {
    add_shortcode( "bettenaert-{$i}", function() use ( $i ) {
    modell_nach_bettenart_2( $i );
        } );
}
if ( $i = 'Queensbett') {
    add_shortcode( "bettenaert-{$i}", function() use ( $i ) {
    modell_nach_bettenart_2( $i );
        } );
}
if ( $i = 'Hubbett') {
    add_shortcode( "bettenaert-{$i}", function() use ( $i ) {
    modell_nach_bettenart_2( $i );
        } );
}

Is there any way to make the value variable, to have only one if-function or can I do it in an other way? Has anyone an idea and can help me?

0
add_shortcode( "Querbett", "bettenaert");
add_shortcode( "Alkovenbett", "bettenaert");
add_shortcode( "Queensbett", "bettenaert");
add_shortcode( "Hubbett", "bettenaert");

function bettenaert (  $atts, $content = null, $tag ) {

   modell_nach_bettenart_2( $tag );

}

Now use shortcode [Querbett], [Alkovenbett], [Queensbett] or[Hubbett] as required.

I hope this may help.

  • Thanks for the answer. I have custom post type called 'womos'. The custom field name is 'bettenart'. And the shordcode should be variable like [Querbett], [Alkovenbett], [Queensbett] and [Hubbett] or something like this. I don't know, if I can realize this with your given code. I have no idea how. Have you any further tip for me? – kuh13 Apr 13 at 16:14
  • OK, got it. Let me update my answer shortly. – Qaisar Feroz Apr 13 at 16:32
  • Have look at updated answer. Give it a try and let me know if it works. – Qaisar Feroz Apr 13 at 16:42
  • I don't no why and how but it works great! Thanks a lot for your help! – kuh13 Apr 13 at 18:20
  • @kuh13 Check this tutorial for detail. – Qaisar Feroz Apr 13 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.