skip to Main Content

I am trying to run a function which is using a featured image as my header image on my webpage I want to include the options to also allow my end user to select between using the featured image or to select a slider instead if they wish to use that page depending here is the code I have for the featured image development.

add_action('neve_before_primary', 'getPageFeaturedImage', 5);
function getPageFeaturedImage() {
//  These two variables will only be used if set
    $pageTitle = get_field('page-title');
    $pageSecondTitle = get_field('page_second_title');

    if (has_post_thumbnail($post -> ID) ) {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail');
        

            <div class="featured-image-container">
                <img src="<?php echo $image[0]; ?>" class="featured-image">
            </div>

    </div>

This code when run is making the feadured image the main image on the page with a title container on top of it with some css which doesnt matter for this question below you will find the code I have for the slider.

function smartsliderheader() {
echo '<div class="smart-slider-header">';
$slider = get_field("smart_slider_header"); 
echo do_shortcode($slider); 
}

This on its own does what I need it to do the featured image code works and so does the slider but getting them both to run toghether and have only one of them run if the other has no options used is where I could used some help.

Any help with this will be much apprechiated. I look forward to your questions if I have missed something out.

2

Answers


  1. Chosen as BEST ANSWER

    I was actually able to solve this by myself by doing the following code we are using 2 variables one is for a featured image and the other is go a slider please look below for the code and an explanation.

    if(!$checkFeaturedImage and !$slider) { 
    return null ; 
    } 
    

    This is checking to see if either of these variable's have a value assigned to them e.g. if a featured image is selected a value will be assigned to it and vice versa for a slider what this is doing if neither of these have a value it will stop the rest of the function running and save render time on the site.

    if ($slider) {
    echo '<div class="header-featured-image">' ; 
    echo do_shortcode($slider); 
    echo '</div>' ;
    
    }
    

    What this is doing is it is making a call to an advanced custom field set up to allow a slider to be selected and if a value is returned that value is then being directly echoed out onto the page.

    else {
            if (has_post_thumbnail($post -> ID) ) {
                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail'); }
            ?>
                <div class="header-featured-image"> 
                    <div class="featured-image-container">
                        <img src="<?php echo $image[0]; ?>" class="featured-image">
                    </div>
                </div>
            }
    <?php
        }
    

    What this is doing is this is displaying our featured image onto our page if a value is returned by our page editor.

    The way this code has been set up is if a featured image and a slider have both been selected it will default back to a slider as that is the first call and due to the null statement at the beginning if nothing is selected nothing will run saving some rendering time.


  2. I would simply do this using an if statement checking if the $pageTitle and the $pageSecondTitle are empty

    if(empty($pageTitle) && empty($pageSecondTitle)) {
        // do stuff for when settings aren't filled in
    }
    else {
        // do stuff for when settings are filled in.
    }
    

    I don’t know if this answers your question. Let me know if this goes into the direction you want to go so I can build on it further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search