how to show a element only in post page wordpress - php

There is a div element in header.php which I want to show only in post page.
The div should not be visible on home page or 404 page or category or pages or tag page.
It should be only visible post page.
The method I am thinking is using css. The div element will have a class. Let the class be hide-container this class is set to display:none;. . the trick is show another div container in post page.
Like this below code which shows the class in singles.
<div class="hide-container <?php if(is_singular() ) { ?>container<?php } ?>">
The above code will have worked, but it shows the div element on all pages except home page. I only want the div to be displayed in post page.
thanks in advance.

use the is_home() conditional tag
<div class="social-tools <?php if(is_home() ) { ?>container<?php } ?>">
even though its written like that - is_home() shows the posts page
check out this question

Related

How to remove Wordpress taxonomy description from paged pages

I'm attempting to remove or prevent the from being added to is_paged() taxonomy pages (tag and category pages).
I'm not sure if it is possible to remove, however is it possible to set a condition that this class is only added is the page is NOT is_paged(). This will prevent the class="clr tax-desc" from being displayed on page 2, page 3, page 4, etc of my category and tag pages.
I only want the class="clr tax-desc" to be present on the main page (or page 1) of the relevant category and tag pages.
I need to create something along the lines of:
if ( is_archive() && !is_paged()) {
Remove <div class="clr tax-desc"></div>
}
Any suggestions on how to go about this?
If not mistaken, does the description under the category name have the class entry-meta?
If you give this class in your css the property
display:none;
then it will be hidden from your pages.
So add this line into your css and it should be gone.
.entry-meta { display:none; }

add class to element based on custom page (Wordpress)

Goal:
I need the element on the menu/nav links to be highlighted based on the current active page. Example - viewing "About" page will highlight the "About" item on the menu.
What I've tried:
highlight active element based on active page made the most sense but:
It's not adding the class based on page being viewed. I added the variable on the .php template file and the Wordpress page created from dashboard itself but both don't work.
Any suggestions on how to accomplish this?
Wordpress Dashboard Page: About
custom template file: About.php
Menu items:
<div>
<a class="(add class here based on page url)">About</a>
<a class="(add class here based on page url)">Blog</a>
<a class="(add class here based on page url)">Products</a>
<a class="(add class here based on page url)">Contact</a>
<div>
You can try with below:
<?php
global $post;
// add 'post_name' to the $post_class
$post_class = $post->post_name;
?>
<div>
<a class="<?php echo $post_class; ?>_page">item</a>
<a>item</a>
<a>item</a>
<a>item</a>
<div>
On the $post_class you will get the current page name so the class will get based on page name.
Thanks!

Hide wp_head () only in the homepage

I've this code in the header Homepage that find automatically the title page for homepage and the posts how title tag. But I've my personal title tag only in the home page. The source code show 2 title tag in the home.
How can I hide this code in the home?
<? php wp_head(); ? >
i think, you can use the "is_home()" and "is_frontpage()" function, depending on how your frontpage is setted up in theme (static page, blog page...), one of them or using logical and can be necessary.
with this you can do like:
<?php
if( !is_home() && !is_frontpage() ) {
wp_head();
} else {
// custom head code
}
?>
an other way is using a separate template for home/frontpage (using a frontpage.php/home.php), where you put in your code.
here the difficult for me was to learn how wordpress is using more specific templates and templateparts (like using get_header('home') to use header-home.php instead of header) and with which weight-order they are used.
https://developer.wordpress.org/reference/functions/is_home/
https://codex.wordpress.org/Creating_a_Static_Front_Page
is_front_page() & is_home() can't help you ?

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:
<a
href="#contentBox1"
class="dem_buttons"
onclick="loadPage1('<?php the_permalink(361) ?>')"
target="#contentBox1">
</a>
<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:
<?php
if ( is_page([page id]) ) { ?>
[single.php's content]
<?php
} 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
get_header()
// postcontent.php is the body of the post
include_once( postcontent.php )
get_footer()
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
get_header()
// the content of the "Services" template
services_content()
// postcontent.php is the body of the post referenced
include_once( postcontent.php )
get_footer()
This way headers and footers are there on both pages, and there are only one header and footer on each of them.
I'm not 100% sure on the Wordpress side of all this, but could you perhaps wrap the second/first header in a div with a specific id, then give that id a style of display none? Like below:
HTML
<header class='header'> ..... </header>
<div id='hidden-header'>
<header class='header'> ..... </header>
</div>
CSS
#hidden-header {
display: none;
}
Let me know if that helps or not and maybe I can see if I can find something else.

wordpress: Conditionally run javascript and load image for post pages

I have a javascript curl effect in place which loads an image and javascript file for the curl effect. But I want to appear in the post pages only.
So I want to do:
if this is a postpage{
load image;
load javascript;}
What is the way to do this in wordpress with php?
If you want to add it only to posts pages and homepages and your plug ins are troubling you..you might want to try
<?php if (is_single()|| is_home() || is_page()){ ?>
load image;
load js;
<?php } ?>
if you want it for only posts and pages, try
<?php if (is_single()|| is_page()){ ?>
load image;
load js;
<?php } ?>
not entierly sure what your after,
but you say you have the code ont he index.php page,
again un-sure has to, how you have this setup,
but if its only to be shown on a post or page or a postpage? (single post),
would be better to move your code into the header.php file? or have it only in the single.php file which means its only loaded when viewing a single post,
in turn it wont be shown on any other pages, except the single (full post) page..
then using the likes
if( is_single() ){
//then show - run your code?
}
?
Marty
Edit:
for both posts & pages then use
if( is_single() || is_page() ){
//then show - run your code?
}
this will only show your code when its a single post OR within a page.

Categories