K
K
kabbyr2021-06-08 21:51:29
WordPress
kabbyr, 2021-06-08 21:51:29

Problem with single.php. What to do?

The fact is that single.php stopped working due to a problem in the header.
The header.php file has a list for the menu, which should have links to specific posts, which are displayed via get_posts().

header.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">
    <!-- <link rel="stylesheet" href='assets/css/main.css'> -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> 
    <title><? bloginfo('name'); echo " | "; bloginfo('description')?></title>
    <?
        wp_head();
    ?>
</head>
<body>
<header class="header">
        <div class="container start">
        <div class="row middle">
        <div class="col-md-1 white-text tab"><a href="sponsory" class="header_link">Sponsors</a>
            <ul class="list   none stop">

                     <? 
                     $posts = get_posts( array(
                      'numberposts' => -1,
                       'category_name'    => 'sponsors_cards',
                        'orderby'     => 'date',
                        'order'       => 'ASC', 
                        'post_type'   => 'post',
                       'suppress_filters' => true,) ); 
                     foreach( $posts as $post ){
                         setup_postdata($post); 
                         ?>
                        <li class="list-item"><a href="<? echo get_permalink()  ?>"><?the_title()?></a></li> 
                         <?
                     } 
                     wp_reset_postdata(); 
                      ?>   
            </ul>
        </div>
        <div class="col-md-1  white-text tab"><a href='uchastniki' class="header_link">Exhibitors</a>
        <ul class="list  block none stop">
                <? 
                     $posts = get_posts( array(
                      'numberposts' =>-1,
                       'category_name'    => 'exhibitors_cards',
                        'orderby'     => 'date',
                        'order'       => 'ASC', 
                        'post_type'   => 'post',
                       'suppress_filters' => true,) ); 
                     foreach( $posts as $post ){
                         setup_postdata($post); 
                         ?>
                        <li class="list-item"><a href="<? echo get_permalink()  ?>"><?the_title()?></a></li> 
                         <?
                     } 
                     wp_reset_postdata(); 
                      ?>   
            </ul>
        </div>
        <div class="col-md-1  white-text tab"><a href='diktory' class="header_link">Speakers</a>
        <ul class="list  none stop">
                 <? 
                     $posts = get_posts( array(
                      'numberposts' => -1,
                       'category_name'    => 'speakers_cards',
                        'orderby'     => 'date',
                        'order'       => 'ASC', 
                        'post_type'   => 'post',
                       'suppress_filters' => true,) ); 
                     foreach( $posts as $post ){
                         setup_postdata($post); 
                         ?>
                        <li class="list-item"><a href="<? echo get_permalink()?>"><?the_title()?></a></li> 
                         <?
                     } 
                     wp_reset_postdata(); 
                      ?> 
            </ul>
        </div>
        <div class="col-md-1  white-text tab"><a href='media' class="header_link">Media</a> 
        </div>
        <div class="col-md-6 mt-3 white-text"></div>
        <a href='<?echo get_home_url()?>' class="col-md-2 mt-2 company_name"><? bloginfo('name') ?>
        </a>
        
        </div> 
        <div class="row mt-4 mr-1">
            <div class="col-md-2">
             <a href="<? echo get_home_url() ?>"><? the_custom_logo() ?></a>
            </div>
            <div class="col-md-2  one">
                <div class="block"><img  src="<? echo bloginfo('template_url'); ?>/assets/img/UPCOMING_EVENTS.png" alt=""></div>
                <div class="two">
                <a class="header_bottom-link" href="predstoyashhie-sobytiya">UPCOMING EVENTS</a>    
               </div>
            </div>
            <div class="col-md-2 ml-1 one-without">
                <div class="block"><img  src="<? echo bloginfo('template_url'); ?>/assets/img/CONTACTS.png" alt=""></div>
                <div class="two mr-5"><a class="header_bottom-link" href="kontakty">CONTACTS</a> </div>
            </div>
            <div class="col-md-4 mr-1"></div>
            <div class="col-md-2 ml-2 mt-2 register_btn yellow white-text "><div class=""><a class="register_link" href="registracziya">REGISTER NOW</a> </div>
        </div>
        </div>
        </div>
    </header>


Before I added these link outputs single.php worked, but as soon as I added the content stopped appearing

single.php
<? get_header() ?>  
  <main id="primary" class="main"> 
    <?php
    while ( have_posts() ) :
      the_post(); 
      get_template_part( 'template-parts/content', get_post_type() ); 
    endwhile; // End of the loop.
    ?> 
  </main><!-- #main -->
  <? get_footer() ?>


content.php
<article class="page_detail-wrapper mt-5" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 
    <div class="page_detail-container">
    <?php
    the_content(
      sprintf(
        wp_kses(
          /* translators: %s: Name of current post. Only visible to screen readers */
          __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'top' ),
          array(
            'span' => array(
              'class' => array(),
            ),
          )
        ),
        wp_kses_post( get_the_title() )
      )
    );

    wp_link_pages(
      array(
        'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'top' ),
        'after'  => '</div>',
      )
    );
    ?>
  </div> 
 
</article>


I solved the problem by removing get_posts() in header.php. But the thing is, I need these outputs in header.php.
Is there a way to leave get_posts() but have content in single.php?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Tkachev, 2022-01-26
@Real_Farmer

Have you checked what get_posts() returns. Try to output var_dump(get_posts()) Most likely it will give an error somewhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question