skip to Main Content

I’m working on a shortcode to display a custom post type, which mostly works. I don’t control the feed this data is coming from, hence the need to split up the title based on delimiters. The problem I’m experiencing is that the variables first, second, third, and fourth can be null, and I don’t know how to account for this in this context.

function display_custom_post_type() {
    // ... irrelevant code ...
    $title = (get_the_title ());
    $str = preg_split('(||[|]|=)', $title,-1, PREG_SPLIT_NO_EMPTY);
    print_r($title);
    $first = $str[0];
    $second = $str[1];
    $third = $str[2];
    $fourth = $str[3];
            
    $string .= '<h3 class="test-parl-title"><div>' . $first . '</div></h3>';
            
    $string .= '<h5>' . $second . ' ' . $third . ' ' . $fourth . '</h5>';
    // ... irrelevant code ...
    return $string;
}

5

Answers


  1. you can check if value that come from html is empty string and make for loop to transfer empty string to null

    foreach($_POST as $key => $value){
      if($value === ""){
          $_POST[$key] = null;
    }
    }
    

    you can you also isset($var); also to check if variable have null value or undefine variable.

    Login or Signup to reply.
  2. UPDATE:

    In a comment you said that $str[0] is always populated and the others vars can be null:

    $string .= '<h3 class="test-parl-title"><div>' . $str[0] . '</div></h3>';
    
    unset($str[0]);
    
    if (!empty($str) {
         $string .= '<h5>' . implode(' ', $str) . '</h5>';
    }
    

    I used $str[0] in h3. I deleted it and imploded whatever remained if it wasn’t empty. This is shorter than using $first, $second, $third, $fourth, etc.

    Login or Signup to reply.
  3. Try some syntactic sugar

    If you are running php 7.0.x or higher you can use the null coalescing operator. It’s syntatic sugar but it’s very easy to read:

    $title  = get_the_title();
    $str    = preg_split( '(||[|]|=)', $title, -1, PREG_SPLIT_NO_EMPTY );
    $first  = $str[0] ?? '';
    $second = $str[1] ?? '';
    $third  = $str[2] ?? '';
    $fourth = $str[3] ?? '';
    

    The variables are getting assigned the $str[0] if it exists and is not null, otherwise they are assigned the second argument (here, an empty string, but you can use what you want).

    Here’s your whole function rewritten in a slightly easier to read format (untested):

    function display_custom_post_type() {
        $args = array(
            'post_type'      => 'parl',
            'post_status'    => 'publish',
            'pagination'     => true,
            'posts_per_page' => '10',
            'orderby'        => 'date',
            'paged'          => $paged,
        );
    
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) :
            while ( $query->have_posts() ) :
                $query->the_post();
                ?>
                <div class="test-parl-article">
                    <img class="test-parl-icon" src="/wp-content/uploads/thumbnail.jpg" alt="Photo of Joe Blogs">
                    <div class="test-parl-article-meta">
                        <?php
                        $str = preg_split( '(||[|]|=)', get_the_title(), -1, PREG_SPLIT_NO_EMPTY );
                        $first  = $str[0] ?? '';
                        $second = $str[1] ?? '';
                        $third  = $str[2] ?? '';
                        $fourth = $str[3] ?? '';
                        ?>
                        <h3 class="test-parl-title"><div><?php echo esc_html( $first ); ?></div></h3>
                        <h5><?php echo esc_html( "${second} ${third} ${fourth}" ); ?></h5>
                    </div>
                    <div class="test-parl-date"><?php the_date(); ?></div>
                    <div class="test-parl-link"><a href="<?php the_permalink(); ?>" target="_blank" rel="noopener">View in context</a></div>
                </div>
                <article class="test-article-body"><?php the_content(); ?></article>
            <?php endwhile; ?>
            </div>
        <?php endif; ?>
        <div class="pagination">
            <div class="previous-page"><?php previous_posts_link( 'Newer Posts' ); ?></div>
            <div class="next-page"><?php next_posts_link( 'Older Posts', $query->max_num_pages ); ?></div>
        </div>
        <?php
    }
    
    Login or Signup to reply.
  4. You can try use empty function:

    if( $query->have_posts() ){
            while( $query->have_posts() ){
                $query->the_post();
                $string .= '<div class="test-parl-article">';
                $string .= '<img class="test-parl-icon" src="/wp-content/uploads/thumbnail.jpg" alt="Photo of Joe Blogs">';
                $string .= '<div class="test-parl-article-meta">';
    
                $title = (get_the_title());
                $str = preg_split('(||[|]|=)', $title,-1, PREG_SPLIT_NO_EMPTY);
    
                $string .= '<h3 class="test-parl-title"><div>' . empty($str[0]) ? '': $str[0] . '</div></h3>';
    
                $string .= '<h5>' . empty($str[1]) ? '' : $str[1] . ' ' . empty($str[2]) ? '' : $str[2] . ' ' . empty($str[3]) ? '' : $str[3] . '</h5>';
    
    
    
                $string .= '</div>';
                $string .= '<div class="test-parl-date">' . get_the_date() . '</div>';
                $string .= '<div class="test-parl-link"><a href="' . get_permalink() . 'target="_blank" rel="noopener">View in context</a></div>';
                $string .= '</div>';
                $string .= '<article class="test-article-body">' . get_the_content() . '</article>';
            }
            $string .= '</div>';
        }
    

    More detail is here. Hope help you.

    Login or Signup to reply.
  5. Why not easily with standard PHP functions?

    $elements = preg_split('(||[|]|=)', $title,-1, PREG_SPLIT_NO_EMPTY);
    $string .= '<h3 class="test-parl-title"><div>' . array_shift($elements) . '</div></h3>';
    $string .= '<h5>' . implode(' ', array_filter($elements)) . '</h5>';
    // EDIT: or to avoid empty h5 tags
    $string .= empty($elements) ? '' : '<h5>' . implode(' ', array_filter($elements)) . '</h5>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search