wordpress: Conditionally run javascript and load image for post pages - php

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.

Related

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 ?

Adding a script to two pages in my site

Im trying to get my Google Maps js loaded on two pages. One is a page template and the other is a single post type page.
template-homepage.php
single-destinations.php
I have a enqueue scripts file to load all my scripts in the header and footer. Here is the code im trying to use but its not loading the scripts at all.
if (is_singular('destinations') && is_page_template('template-homepage.php')) :
wp_enqueue_script( 'google-maps-js', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyebtdsdfgdAmyvSWk8wXmvdmtD83PR4vrCZuYs&libraries=places' );
endif;
Dan,
How can it be destinations AND template-homepage at the same time?
Try changing this:
if (is_singular('destinations') && is_page_template('template-homepage.php')) :
To:
if (is_singular('destinations') || is_page_template('template-homepage.php')) :

External Page Displaying Properties of [If (Home Page)]

I have a slider that displays only on our homepage with the following condition
<?php if( is_home() ) : ?>
It works properly with one exception, external pages located outside of the sites main folder that call the header display the slider as well.
Is there a way to designate a page as "not home/front"?
<?php is_front_page(); ?>
change is_home() to that and it should fix it right up.

wordpress custom script to handle link!

in wordpress the single page is controlled by single.php theme file. I want to put another link on the single page like "Preview". I want this link to be handled by preview.php. preview.php will be stored in theme directory! how to handle this?
usually I do these using init action hook! if the variable exists in the query string, I perform the action! but how to register a custom script to handle a custom link!
the single page link is like ?p=x and the link to preview will be ?p=x&action=preview
thanks in advance
You could do it old school style. At the very top of your single.php, put:
<?php
if( isset( $_GET['preview'] ) ) {
require( "preview.php" );
die(); # a horrible death
}
?>

How can I tell my wordpress to do something in the header if its a blog page?

In a previous post (Don't show this if the url contains the following) I asked how I would go about having my header echo a div if the user loaded a URL with /blog in the header.
What I didn't take into consideration, was that I don't want the div to display if its not just got /blog in the url, but if its any blog post, not just the index page of the blogs.
How do I run a bit of code from my header.php if the page I'm looking at is a blog post?
You would use the Wordpress functions is_single() and is_page.
if(!is_single() && !is_page() && !is_home() && !is_archive())
{
/* This will not display for any post, page, the home page, or an archive.
You can remove each is statement according to your needs */
}
For only posts, only use is_single, the same for page, home, and archive.
The full listing of is_statements can be found here. Here are some others:
is_home() : Home Page
is_front_page : Front Page
is_single() : Single Post
is_admin() : Admin Panel
is_sticky() : Sticky Post
is_page() : Page
is_category
is_tag
is_author
It's been long since I've played with wordpress but you can achieve a lot with using the conditional tags:
http://codex.wordpress.org/Conditional_Tags
Seems like is_single() might do what you want. Eg:
if (!is_single())
{
// display div
}

Categories