Combining php and HTML in child functions

  • jrothra

    #1526

    I’m learning how to use the hooks to add content to the theme using the child’s functions.php file. However, the content I need to add is a related posts slider. The auto-add feature of this plugin doesn’t work in the theme, but there is a manual add function: just place some code where you want the slider to appear. The code they say to use:

    Code:
    <?php if(function_exists('get_related_posts_slider')) {get_related_posts_slider();} ?>

    I stripped out the

    Code:
    <?php

    and ?>

    , inserting this into the child functions.php file:

    function graphene_custom_content_related_posts_slider(){
    if(function_exists('get_related_posts_slider')) {get_related_posts_slider();}
    }
    add_action( 'graphene_after_post_content', 'graphene_custom_content_related_posts_slider' );

    The slider was placed, but I want to change the margin around it. Since the slider is added via php, but the margin is done via html, what do I put in the file? I tried putting <div style="margin-top:20px;"> . . . </div> around the php code for the slider, but that cause the page to puke:

    Parse error: syntax error, unexpected ‘<‘

    How do I combine the two?

    jrothra

    #16145

    On a related note, I noticed that the above cited code causes the slider to appear on a single excerpted home page post, but not the others:

    Screenshot – site using Graphene theme not live yet

    I don’t want it on any excerpted content. Also, the top and second posts can be seen here (under current design format, but this way you can see the posts’ lengths:

    Top post

    Second post

    Admin

    Syahir Hakim

    #16146

    To insert HTML code in PHP codes, there are two ways. First way:

    <?php
    function foobar(){
    echo '<p>This is some HTML which will be printed out (i.e. "echoed") to the screen</p>';
    }
    ?>

    Second way:

    <?php
    function foobar(){
    // Some PHP codes
    ?>
    <p>This is some HTML which will be printed out to the screen.</p>
    <?php
    // Some more PHP codes
    }
    ?>

    To prevent the slider from being displayed in excerpts, you can use the is_singular() conditional:

    function graphene_custom_content_related_posts_slider(){
    if( function_exists('get_related_posts_slider') && is_singular() ){
    get_related_posts_slider();
    }
    }
    add_action( 'graphene_after_post_content', 'graphene_custom_content_related_posts_slider' );

    That way, the slider will only be displayed on singular pages, i.e. single post page, single page page, single attachment page.

    jrothra

    #16147

    That worked perfectly (the third listing)! I looked again a few minutes ago and realized I could edit the div containing the slider, giving it a larger top margin. I did that in the child CSS.

Viewing 4 posts - 1 through 4 (of 4 total)

  • You must be logged in to reply to this topic.