André Teles André Teles - 1 month ago 4x
CSS Question

Different Single.php content depending on page

Backstory: I've been dealing with this website for the past 5 months. I didn't have previous experience with Wordpress, but I had some with HTML, CSS and PHP.

I have two different ways to display posts on my custom Wordpress template.

On the "Services" page, a post is loaded onto a dynamic content div by clicking on different buttons. Each button loads a different post on the same div by calling specific post ID's:

onclick="loadPage1('<?php the_permalink(361) ?>')"

<div class="text-center clearfix" id="contentBox1"></div>

On the "Search" page there is no dynamic content div. Each search result consists on an image, the title and the excerpt of the post. The posts are acessed by clicking on their respective titles after the search results come up.

Here's the problem: If I don't include the header and the footer on single.php, the post will be loaded without styles, navigation bar and footer after clicking the title of the search result. I need to load the header and the footer on single.php because of this search page, but this means there will be a second header and footer on the dynamic content div of the "Services" page, as well.

I've tried starting single.php with:

if ( is_page([page id]) ) { ?>

[single.php's content]

} else {

get_header(); ?>
[single.php's content]
<?php get_footer();

} ?>

to no success. I've also tried out !is_page, but no luck on that either.

Please don't recommend plugins. I really need to code everything by hand. The written reason for that would be longer than this post.


The final solution was to rearrange the parts of single.php so that it used include (or require) for showing the given post's content (named here: postcontent.php).

// Pseudo code

// postcontent.php is the body of the post
include_once( postcontent.php )

This way the single.php had its header and footer, but the "Services" template could include the post's content directly:

// Pseudo code

// Template: Services
// the content of the "Services" template
// postcontent.php is the body of the post referenced
include_once( postcontent.php )

This way headers and footers are there on both pages, and there are only one header and footer on each of them.