当用户没有帖子时显示“无结果”(WordPress)

时间:2018-02-23 06:46:39

标签: wordpress

我有一个自定义帖子类型,并有一个短代码使其显示,但我想如果该自定义帖子中没有帖子,那么它应该写成“没有帖子”...
这是代码。所有事情都在发挥作用,但它没有显示“没有帖子”,当它是空的时候。

    if (detailCallListTable.isEditing && indexPath == false){
        return UITableViewCellEditingStyle.none;
    }

此致

2 个答案:

答案 0 :(得分:0)

试试这个修改过的脚本,请检查shortcode参数,这里我更新了正确的方法。

    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;
}

答案 1 :(得分:0)

请尝试此代码

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;
}