I am trying to create a "add to string if condition" is met function, here's my actual code:
if ( ! function_exists( '_s_posted_on' ) ) :
function _s_posted_on( $params = array() ) {
$output = 's'; /** Holds the string of the code output by the arguments. */
if ( in_array('time', $params) ) : /** Begin main loop */
/**
Show the time of the post
*/
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', '_s' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$output .= '<span class="posted-on">' . $posted_on . '</span>';
elseif ( in_array('author', $params) ) :
/**
Show the post's author
*/
$author = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', '_s' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
$output .= '<span class="author"> ' . $author . '</span>';
elseif ( in_array('category', $params) ) :
/**
Show the post's category.
*/
$categories = (array) wp_get_post_terms( get_the_ID(), 'category' );
$category_string = sprintf (
esc_html_x( 'in %s', 'post_category', '_s' ),
'<a class="category">' . $categories[0] -> name . '</a>'
);
$output .= '<span class="category">' . $category_string . '</span>';
else :
$output = 'No info about the post!';
endif; /** End main loop */
echo '<div class="post-meta-info"' . $output . '</div>';
}
endif;
Calling _s_posted_on('time', 'category'); only outputs the code from time, yet it should output the code from timeand category since both conditions are met.
The pseudo-code looks like this:
if (string_exists_in_params($string, $params)):
add_to_my_string --> $string2;
elseif (string_exists_in_params($string1, $params)):
add_to_string --> $string1;
elseif (string_exists_in_params($string2, $params)):
add_to_string --> $string2;
The problem is that the loop breaks every-time it hits a condition, so say I'd call my function _s_posted_on with $string1, $string2, my output should be the results of $string1 and $string2 but unfortunately, it stops at $string1 and doesn't go further to check for $string2.
In short, I'm trying to add to a string, based on some conditions, where 2 conditions can be present, but the loop stops at the first condition met and breaks.
Aucun commentaire:
Enregistrer un commentaire