• Resolved sweetberry

    (@sweetberry)


    Hi , i tried writing some code i placed in my header.php into a plug in

    echo " <link rel=\"stylesheet\"  href=\"";
    echo $available_themes[$current_theme];
    echo " \" type= \"text/css \"  media=\"screen\"  >  " ;

    works fine but i want the header to check the category:

    echo " <?php if ( in_category( 'Test' ) ) { ";
    echo " <link rel=\"stylesheet\" href=\"";
    echo $available_themes[$current_theme];
    echo " \" type= \"text/css \"  media=\"screen\"  >  " ;
    echo "";
    echo " } else { ";
    echo " } ?>";

    i guess i do it wrong but that is the code i want in the header.php since it works when placed there manually, but the <?php is recognized as php code and not echoed correctly i tried placing \ but it didnt work

    thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you are trying to output the literal string “<?php” as a programming example, you can HTML encode the syntax:
    echo "<?php if ( in_category( ...

    If you are trying to trying to echo conditionally, e.g., only output your link HTML if in_category(‘Test’) returns true:

    <?php
    if ( in_category( 'Test' ) ) {
    	echo " <link rel=\"stylesheet\" href=\"";
    	echo $available_themes[$current_theme];
    	echo " \" type= \"text/css \"  media=\"screen\"  >  " ;
    }
    ?>

    For bonus points, you can get much cleaner code with:

    <?php
    if ( in_category( 'Test' ) ) {
    	echo "<link rel='stylesheet' href='{$available_themes[$current_theme]}' type= 'text/css' media='screen' />";
    }
    ?>

    The forum HTML parser took my first example and changed it. It was actually:

    echo "& lt;?php if ( in_category( ...

    Without the space after the &.

    BTW: Don’t use

    echo " <link rel=\"stylesheet\"  href=\"";
    echo $available_themes[$current_theme];
    echo " \" type= \"text/css \"  media=\"screen\"  >  " ;

    to enqueue your style. Use the function wp_enqueue_style instead.

    Thread Starter sweetberry

    (@sweetberry)

    thank you very much!
    this was exactly what i was searching for (both answers)

    thank you thank you thank you

    very polite ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to echo "’ is closed to new replies.