• Hello,
    how do I correctly write a condition for the header, shown only if it’s on a blog page (must not show up on other content pages or start page)

    What I already have, but did not work:

    <?php 
    if(is_single()){
    <meta property="og:title" content="<?php the_title_attribute(); ?>" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php echo the_permalink(); ?>" />
    <meta property="og:image" content="<?php echo $img_src; ?>" />
    }?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You have nested <?php ?> blocks, that is illegal syntax. To cause HTML output without echo or print statements, you must close out the PHP block, then resume it again.

    <?php 
    if(is_single()){ ?>
    <meta property="og:title" content="<?php the_title_attribute(); ?>" />

    Alternately, use echo statements to cause output and never close out and resume PHP.

    <?php 
    if(is_single()){ 
    echo '<meta property="og:title" content="' . the_title_attribute(['echo'=>false,]) . '" />';

    Note how the_title_attribute() is supplied with an argument to return values instead of echoing. This amounts to the same thing:

    <?php 
    if(is_single()){ 
    echo '<meta property="og:title" content="';
    the_title_attribute();
    echo '" />';

    PHP will expand variables within double quoted strings, so this works too:

    <?php 
    if(is_single()){ 
    $attr = the_title_attribute(['echo'=>false,]);
    echo "<meta property='og:title' content='$attr' />";

    If you don’t like single quotes in HTML, replace every occurrence of ' with \"

    in other alternative. you just close your php code first. like @bcworkz first answer. in all you need. just replace your code like this

    <?php  if(is_single()){ ?>
    <meta property="og:title" content="<?php the_title_attribute(); ?>" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php echo the_permalink(); ?>" />
    <meta property="og:image" content="<?php echo $img_src; ?>" />
    <?php };?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Printing og meta only if posts page?’ is closed to new replies.