Tengo un tipo de publicación personalizada y tengo un código corto para que aparezca, pero quiero que si no hay ninguna publicación en esa publicación personalizada, entonces debería escribir "No hay publicación" ...
Aquí está el código. Todas las cosas están funcionando, pero no muestra "Sin publicación" cuando está vacío.

add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;

$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
    'author' => $id,
    'order' => 'DESC',
    'post_type' => 'scholarship',
    'orderby' => 'date',
    'posts_per_page' => $post_limit,
);

$query = new WP_Query($args);

if ($query->have_posts()) {



    $display .= '<div class="upme-main upme-main-">';

    // Display different views based on posts with featured images or posts as text
    if ('1' == $feature_image_status) {

        while ($query->have_posts()) : $query->the_post();

            $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
            $image_src = upme_url . 'img/default-post-thumbnail.png';
            if (is_array($image_attributes) && ('' != $image_attributes[0])) {
                $image_src = $image_attributes[0];
            }

            $display .= '<div class="upme-field upme-post-list-field">
                <div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
                <div class="upme-post-feature-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
             </div>';

        endwhile;

        wp_reset_query();
    } else {

        while ($query->have_posts()) : $query->the_post();

            $display .= '<div class="upme-field ">
                    <div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
                    <div class="upme-post-field-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
                 </div>';
        endwhile;

        wp_reset_query();
    }

    $display .= '</div>';
}

return $display;
}

Saludos

0
mukesh 23 feb. 2018 a las 09:46

2 respuestas

La mejor respuesta

Pruebe este script modificado, verifique el parámetro de código corto, aquí he actualizado el método correcto.

    add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($atts){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$display = $atts['display'];
$id = $atts['id'];
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
    'author' => $id,
    'order' => 'DESC',
    'post_type' => 'scholarship',
    'orderby' => 'date',
    'posts_per_page' => $post_limit,
);

$query = new WP_Query($args);

if ($query->have_posts()) {



    $display .= '<div class="upme-main upme-main-">';

    // Display different views based on posts with featured images or posts as text
    if ('1' == $feature_image_status) {

        while ($query->have_posts()) : $query->the_post();

            $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
            $image_src = upme_url . 'img/default-post-thumbnail.png';
            if (is_array($image_attributes) && ('' != $image_attributes[0])) {
                $image_src = $image_attributes[0];
            }

            $display .= '<div class="upme-field upme-post-list-field">
                <div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
                <div class="upme-post-feature-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
             </div>';

        endwhile;

        wp_reset_query();
    } else {

        while ($query->have_posts()) : $query->the_post();

            $display .= '<div class="upme-field ">
                    <div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
                    <div class="upme-post-field-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
                 </div>';
        endwhile;

        wp_reset_query();
    }

    $display .= '</div>';
}else{
/*need to add this*/    
 $display .= '<p>Sorry!, No Record Found</p>';
}

return $display;
}
0
Madhavi Jha 23 feb. 2018 a las 08:58

Pruebe este código

add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;

$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
    'author' => $id,
    'order' => 'DESC',
    'post_type' => 'scholarship',
    'orderby' => 'date',
    'posts_per_page' => $post_limit,
);

$query = new WP_Query($args);

if ($query->have_posts()) {



    $display .= '<div class="upme-main upme-main-">';

    // Display different views based on posts with featured images or posts as text
    if ('1' == $feature_image_status) {

        while ($query->have_posts()) : $query->the_post();

            $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
            $image_src = upme_url . 'img/default-post-thumbnail.png';
            if (is_array($image_attributes) && ('' != $image_attributes[0])) {
                $image_src = $image_attributes[0];
            }

            $display .= '<div class="upme-field upme-post-list-field">
                <div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
                <div class="upme-post-feature-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
             </div>';

        endwhile;

        wp_reset_query();
    } else {

        while ($query->have_posts()) : $query->the_post();

            $display .= '<div class="upme-field ">
                    <div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
                    <div class="upme-post-field-value"><span><a href="' . get_permalink() . '">' . get_the_title() . '</a></span></div>
                 </div>';
        endwhile;

        wp_reset_query();
    }

    $display .= '</div>';
}else{

 $display .= '<p>No Post Found</p>';
}
wp_reset_query();
return $display;
}
0
developerme 23 feb. 2018 a las 08:07