Support » Themes and Templates » Simple php question

  • Resolved peterspliid

    (@peterspliid)


    Hi, i’m not very experienced with php yet, and i’m trying to merge two php codes into each other, but I’m running out of luck.

    I have the following code:

    <?php if ( $format == 'gallery' ) echo '
    <div id=spotlightg>
    Contributer Spotlight
    </div>
    <div id=gcontent>
    Info
    </div>
    <br /><br /><br />'; else echo ''; ?>

    And where is says info (between two divs), I want to add this code

    <header class="entry-header">
    		<?php do_action( 'gpp_before_title' ); ?>
    		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'gpp' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    		<?php do_action( 'gpp_after_title' ); ?>
    	</header>

    This code contains html and php, so it can’t all be in an echo, due to the php part. I just need this to be between those two divs. How do i manage to do this?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Andrew Nevins

    (@anevins)

    Is this what you mean?

    <?php if ( $format == 'gallery' ) echo '
    <div id=spotlightg>
    Contributer Spotlight
    </div>
    <header class="entry-header">
    		<?php do_action( 'gpp_before_title' ); ?>
    		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'gpp' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    		<?php do_action( 'gpp_after_title' ); ?>
    </header>
    <div id=gcontent>
    Info
    </div>
    <br /><br /><br />'; else echo ''; ?>

    I know how to copy and paste, but the formatting is wrong. Firstly, you can’t have <?php ?> tags within another tag, secondly, it’s in an echo tag.

    Michael

    (@alchymyth)

    Forum Moderator

    one possibility, getting rid of the echo all together:

    <?php if ( $format == 'gallery' ) : ?>
    <div id=spotlightg>
    Contributer Spotlight
    </div>
    <div id=gcontent>
    <header class="entry-header">
    		<?php do_action( 'gpp_before_title' ); ?>
    		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'gpp' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    		<?php do_action( 'gpp_after_title' ); ?>
    	</header></div>
    <br /><br /><br />
    <?php endif; ?>

    (no empty ‘else’ part needed)

    esmi

    (@esmi)

    Forum Moderator

    <?php if ( $format == 'gallery' ) :?>
    <div id=spotlightg>
    Contributer Spotlight
    </div>
    <div id="gcontent">
    <header class="entry-header">
    <?php do_action( 'gpp_before_title' ); ?>
    <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'gpp' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php do_action( 'gpp_after_title' ); ?>
    </header>
    </div>
    <br /><br /><br />';
    else:
    echo '';
    endif;?>

    That’s exactly what I was looking for. Thanks a lot!

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    alchymyth and esmi beat me to it. Lol. Either way works.

    Michael

    (@alchymyth)

    Forum Moderator

    you can of course keep as much echo as you like:

    <?php if ( $format == 'gallery' ) { echo '
    <div id=spotlightg>
    Contributer Spotlight
    </div>
    <div id=gcontent>
    <header class="entry-header">';
    do_action( 'gpp_before_title' );
    echo '<h1 class="entry-title"><a href="'; the_permalink(); echo '" title="'; printf( esc_attr__( 'Permalink to %s', 'gpp' ), the_title_attribute( 'echo=0' ) ); echo '" rel="bookmark">'; the_title(); echo '</a></h1>';
    do_action( 'gpp_after_title' );
    echo '</header>
    </div>
    <br /><br /><br />';
    } else { echo ''; }
    ?>

    and there are possibly hundreds more of different ways to do it.

    I know how to copy and paste, but the formatting is wrong. Firstly, you can’t have <?php ?> tags within another tag, secondly, it’s in an echo tag.

    should you not be able to do it yourself with that knowledge?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Simple php question’ is closed to new replies.