WordPress function for detecting what page has loaded? - php

is there a wordpress function that I can use to detect pages? example of what I want to do is below.
<?php if( is_FUNCTION_page('Contact Us') ) : ?>
...display this <div> / xhtml
<?php else: ?>
something else <div>
<?php endif;?>

Check is_page() function:
is_page();
// When any single Page is being displayed.
is_page(42);
// When Page 42 (ID) is being displayed.
is_page('Contact');
// When the Page with a post_title of "Contact" is being displayed.
is_page('about-me');
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page(array(42,'about-me','Contact'));
// Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "Contact". Note: the array ability was added at Version 2.5.

Yeah. Use Wordpress is_page function to do that.
Example:
<?php if( is_page('Contact Us') ) : ?>
<div> Your If content goes here </div>
<?php else: ?>
<div> something else </div>
<?php endif;?>
NOTE:
Cannot Be Used Inside The Loop
Due to certain global variables being overwritten during The Loop is_page() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.

Related

How to insert page/post specific script in header and footer in WordPress?

How to insert page/post specific script in header and footer in WordPress?
I need to insert the script on a specific page or post. For Example, I will insert different codes in different pages/posts. The codes will not appear in all the pages/posts.
You can use is_page() function:
is_page( $page = '' );
$page
(int|string|array) (Optional) Page ID, title, slug, or array of such.
For example if your page id is 1954
if( is_page( $page = 1954 ) ){
your specific code for this page here..
}
You can Do it via Slug of post or page or id of post/page using...
1. is_single() for post
2. is_page() for page
you can add or functions also if you want to add that code in multiple pages but specific pages only.
here is code...
if (is_page('home') || is_page('contact') || is_page('45')
Example 1....
<?php if (is_page('home')): ?>
<!--home page add custom JS-->
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/Scripts/customJS.js"></script>
<?php endif; ?>
Example 2...
<?php if (is_page(346) ):?>
<!-- Google Analytics Content Experiment code here -->
...
<!-- End of Google Analytics Content Experiment code here -->
<?php endif; ?>

advanced custom fields true/false only works in front-page.?? not in innerpages why?

I have an ACF true or false based if the condition to show a section based on the mentioned code. it works correctly on the front page/homepage of WordPress. but it doesn't work in the inner pages, not getting the values of the field and I use this code in footer templates.
its a WordPress site with the latest version. and the code is in the footer template
<?php
$show_company_info = get_field( "show_company_info", get_the_ID() );
if($show_company_info ):
?>
<section class="company">
<div class="container">
<?php if ( is_active_sidebar( 'company-info-widget' ) ) : ?>
<?php dynamic_sidebar( 'company-info-widget' ); ?>
<?php endif; ?>
</div>
</section>
<?php endif; ?>
if we checked the ACF true/fields condition in any page it will show the section otherwise not. but it actually works in homepage only
Assuming that you have created a field in a Home Page.
So, to retrieve the home page field you need to do like this :
$frontpage_id = get_option( 'page_on_front' );
$show_company_info = get_field( "show_company_info", $frontpage_id );
if ( $show_company_info ) {
//Your code
}

is_page() is not working

Ok, so I've been trying to figure this out for a while. I'm not exactly a PHP developer but can usually figure things out be looking at examples and Wordpress docs. However I'm struggling.
I'm trying to add a "simple" bit of code to a Wordpress template file, but it won't work. Basically I want to show content on certain pages and different content on other page. This is what I have so far:
<?php if(is_page('123')): ?>
This text
<?php endif; ?>
<?php if(!is_page('123')): ?>
That text
<?php endif; ?>
Any help would be so greatly appreciated.
Are you using this inside the WP Loop? If yes, note this:
Due to certain global variables being overwritten during The Loop,
is_page() will not work. In order to call it after The Loop, you must
call wp_reset_query() first.
(from: https://developer.wordpress.org/reference/functions/is_page/)
Also, you shouldn't put the page ID in quotes - it's an integer.
You should provide more context. What does the provided code echo? Does it always say "That text" although you think that it should say "This text"?
Perhaps too obvious but is '123' the name of the post?
If you want to check for the Post with the ID you should try this:
<?php if(is_page(123)): ?>
This text
<?php endif; ?>
<?php if(!is_page(123)): ?>
That text
<?php endif; ?>
Using '123' makes it a string and searches for a post with a title or slug with that string.
You shouldn't need to Logical "Not" Operator between the two statements. A simple if/else would suffice. That said:
The is_page() function will return false if the current query isn't set up for an existing page on the site.
Possible Conditions:
The global query has been modified/overwritten. If you're using query_posts() before this code - stop! Use WP_Query() along with wp_reset_postdata().
The record with the name '123' isn't actually a page. is_page specifically checks for the page post type. Consider the sister functions is_single() or is_singular().
In that same vein, are you looking for ID: 123? If so, remove the quotes. ID's should be passed as an integer: 123.
You're using this in "The Loop", where it can have unexpected interactions. You'll instead want to use another qualifying statement such as if( get_the_ID() == 123 ) ){ or if( $post->ID == 123 ){.
This code is hooked to an action hook too early such as plugins_loaded where the global query and post objects may not be set up yet.
Check to make sure whether any of those conditions apply, and then you can modify your code to be a bit more succinct:
if( is_page( 123 ) ){
echo 'This is page ID 123';
} else {
echo 'This is NOT page ID 123';
}
Or even further with the Ternary Operator
echo is_page( 123 ) ? 'This is Page ID 123' : 'This is Not page ID 123';
you could try this:
global $post;
<?php if( $post->ID == 346) { ?>
<!-- do your stuff here -->
<?php } ?>
or
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>

function for post from category in wordpress

I have the following function in functions.php page
function viewpost($num)
{
echo $num;
query_posts('order=dsc&cat=$num & showposts=2');
while (have_posts()) : the_post();
?> <span> <?php the_title(); ?>
<?Php
echo get_the_post_thumbnail();
the_excerpt();
?>
<?Php
endwhile;
wp_reset_query();
}
When I call viewpost function for values of viewpost(1)(to view post from category one ) it shows correct values, but when I put the same function again viewpost(2) (to view post from category 2) it shows the previous function values i.e. from category. What can I do to get the post from different categories by changing the passing value
Without trying your code, I think the most likely problem is you're using single quotes. Variable names won't get expanded to their values. See this answer.
Try
query_posts("order=dsc&cat=$num&showposts=2");
instead of
query_posts('order=dsc&cat=$num & showposts=2');
This might be worth a read too. Using query_posts is normally not recommended.

Get ID for static homepage, not post

I am starting to write a custom theme for Wordpress. I have created a new page called 'Home' and set the front page to be a static page, selecting 'Home'. I want this page to show all posts with a category of 'news' plus a couple of images.
I then added front-page.php with the following:
<?php get_header(); ?>
<div class='detail'>
<?php if ( have_posts() ) {
query_posts( 'category_name=news' );
while ( have_posts() ) : the_post(); ?>
<h4><?php the_date('d/m/Y'); ?> - <?php the_title(); ?></h4>
<div class='post'><?php the_content(); ?></div>
<?php endwhile; }?>
</div>
<?php get_footer(); ?>
I've uploaded a couple of images and attached them to the 'Home' page. I now want to get the URL for the attached images. I've tried something like:
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID());
but this only returns the ID of the most recent post being shown, even if I put the above code outside the loop. If I remove the static front page and navigate to the Home page then I get the correct results, any ideas how I can get the images for the Home page when I use it as a static page?
Forgive me if this is simple, first foray into PHP and wordpress development
I think your issue is that you're using get_the_ID(); outside of The Loop. the_ID(); and get_the_ID(); grab the current post ID from the loop; if used outside of The Loop, all you'll get is the last one. See: http://codex.wordpress.org/Function_Reference/get_the_ID
To get the ID of the current page, try:
$page=get_page_by_title($page_name);
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent='.$page->ID);
If that doesn't work, there's a function at http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5 (Where I found the above code) that does the same thing.
Hope this helps!

Categories