【问题标题】:wordpress custom theme with custom post - hide the button if no post带有自定义帖子的wordpress自定义主题 - 如果没有帖子,则隐藏按钮
【发布时间】:2022-01-14 22:04:51
【问题描述】:

我尝试了来自互联网的不同代码,但没有什么能帮助我解决我的问题。我有一个带有自定义帖子的 Wordpress 自定义主题。在我的首页/主页上有一个新闻容器,其中发布了前 3 条当前新闻,从这个容器有一个 [查看更多] 按钮链接到新闻页面。如果没有发布新闻,我会尝试隐藏 [查看更多] 按钮,并且会回显一条文字,说“目前没有帖子”。如果有新的新闻发布,[查看更多]按钮将出现。这个函数会通过php解决吗?我仍然不擅长 PHP,有人可以帮我做这件事吗?这是我当前的自定义帖子:

 <?php
   $args = array(
   'post_type' => 'post',
   'posts_per_page' => 3,
   'category_name' => 'news',
   'orderby' => 'post_date',
   'order' => 'DESC',
   'post_status' => 'publish',
   );
   $posts = get_posts( $args );
   foreach ( $posts as $post ):
   setup_postdata( $post );
 ?>  

 <div class="newbox">
     <div class="newimg">
     <?php if (has_post_thumbnail()) : ?>
     <?php the_post_thumbnail('small-img'); ?>
     <?php else : ?>
                                            
     <img src="<?php echo get_template_directory_uri(); ?>/img/image/default-img.png">
    <?php endif; ?>
    </div>
     <h3 class="title1"><?php the_title(); ?></h3>
     <p class="cont"> <?php the_excerpt(); ?></p>
     <p class="date"><?php the_time('Y.m.d'); ?></p>

     <a href="<?php the_permalink(); ?>">
       <h5 class="btnmr">Read</h5>
     </a>
 </div>

<?php endforeach; wp_reset_postdata(); ?>  

<!-- VIEW MORE -->
 <div class="col-lg-12">
     <a href="<?php echo esc_url( home_url( '/post/newspage' ) ); ?>">
         <div class="btn-default1">
            <p>VIEW MORE</p>
         </div> 
      </a>
 </div>

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    您可以使用WP_Query 类,您可以使用found_posts 获取帖子总数。试试下面的代码。

    <?php
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 3,
        'category_name'  => 'news',
        'orderby'        => 'post_date',
        'order'          => 'DESC',
        'post_status'    => 'publish',
    );
    
    $posts     = get_posts( $args );
    $the_query = new WP_Query( $args );
    $totalpost = $the_query->found_posts; 
    
    foreach ( $posts as $post ): setup_postdata( $post ); ?>  
    
        <div class="newbox">
            <div class="newimg">
                <?php if (has_post_thumbnail()) : ?>
                    <?php the_post_thumbnail('small-img'); ?>
                <?php else : ?>                                    
                    <img src="<?php echo get_template_directory_uri(); ?>/img/image/default-img.png">
                <?php endif; ?>
            </div>
            <h3 class="title1"><?php the_title(); ?></h3>
            <p class="cont"> <?php the_excerpt(); ?></p>
            <p class="date"><?php the_time('Y.m.d'); ?></p>
            <a href="<?php the_permalink(); ?>">
                <h5 class="btnmr">Read</h5>
            </a>
        </div>
    
    <?php endforeach; wp_reset_postdata(); ?>  
    
    <!-- VIEW MORE -->
    <?php if( $totalpost > $args['posts_per_page'] ){ ?>
        <div class="col-lg-12">
            <a href="<?php echo esc_url( home_url( '/post/newspage' ) ); ?>">
                <div class="btn-default1">
                    <p>VIEW MORE</p>
                </div> 
            </a>
        </div>
    <?php }else{
        echo "No post this time";
    } ?>
    

    【讨论】:

    • 嗨@Bhautik 我在上面尝试了你的代码,但结果是,即使有帖子也没有显示查看更多按钮。如果我删除了帖子,则没有文字回显“这次没有帖子”
    • 即使有帖子也会显示回显文本,即使有帖子也会隐藏查看更多按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2016-11-29
    • 2013-08-09
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多