is_page() is not working - php

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;
?>

Related

Hide content in posts and category by id in Wordpress

I am trying to hide a script in some posts and specific categories, here I leave a demonstration of what I have done but it is not working, since when using! Is_single (1001) what it does is hide all the content in it site.
My codes:
<?php
if ( !in_category(118) && !in_category(137) && !in_category(121)) {
?>
<script></script>
<?php } ?>
This is what I've been doing:
<?php
if ( !in_category(118) && !in_category(137) && !in_category(121) && !is_single(1001)) {
?>
<script></script>
<?php } ?>
For categories it does work but when I add! Is_single (1001) the script is hidden all over the site.
I hope your help, I am starting to learn php in WP.
first of all your conditions looks little doubtful, cause it's comparing all the conditions when you use && instead please use || (OR) condition if you are trying to enable or disable script on those conditions.
Second: i dont think WP has function like in_category to check, but i guess you should use is_category()
Third, please add id in array if you have multiple
for example:
!is_category( array( 9,12,19,30 ) );
!is_single( array( 1009,102,119,330 ) );
hope that will work!

Home Template Loop customization - should fetch content based on post template

In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!

How to create different post layout for different categories and subcategories in wordpress

I've modified the single.php file in order to change layout for each category. In single.php I have this code
<?php
$post = $wp_query->post;
if ( in_category( '26') || get_term_children('26') {
include(TEMPLATEPATH . '/single-ric.php');
}
elseif ( in_category( '36') || get_term_children('36') {
include(TEMPLATEPATH . '/single-parl.php');
}
else {
include(TEMPLATEPATH . '/single-all.php');
}
?>
Is there something wrong in this code?
Each post article is shown with single-ric.php layout
Your question address few issues. First, as Pieter stated, you should use get_template_part instead of TEMPLATEPATH as it is deprecated.
What you need to do is to rename your templates to something like singlepost-ric.php, singlepost-parl.php and singlepost.php (instead of singlepost-all.php - that will be your fallback), this to prevent any fallback loop conflict - you need to place them at the same level as single.php to make this work. Then, use get_template_part in this way:
get_template_part('singlepost', 'ric');
If this doesn't work, use this to get the possible error messages:
assert("locate_template( array('singlepost-ric.php', 'singlepost.php'), false, false )");
The other issue is, and why your first condition is always true, is that you don't use right get_term_children. This function is used to get all the childrens of a term in a single array. If it don't find any, it will return an empty array, not false. This is why this (in_category( '26') || get_term_children('26')) is always true. By the way, you're missing a closing parenthesis in all your conditions.
I assume that what you want to do, is to know if the current post is in the category 26. Just remove the get_term_children part - it address something else. You probably added this because a category is a taxonomy, which would be valid then, but in_category is enough.
Last thing, you don't need this:
$post = $wp_query->post;
It is redundant. As you're within a template, the global var $post already contains the post you query.

Enclosured php element in p-tag is ouside p, how is this possible?

I am using advanced custom fields in wordpress and having this line of code:
<p class="small"><?php the_field('somefield', 'options'); ?></p>
And it is printed like this in the browser:
<p class="small></p>
<p>the content of somefield</p>
Why does this happen and how do I fix it?
it's because you are using paragraph inside and paragraph you can fix it by using the fix below , if you are inserting the <p> inside this field , please use span or something else it will cause this problem if you use <p>:
<?php
$myfield = the_field('somefield', 'options');
echo strip_tags($myfield, '<p></p>');
?>
#Arsh gave a good answer.
Let me explain the inner-workings of it in WordPress Methodology.
Most function which starts with the_{function_name} most of the time has a function get_the_{function_name}.
Since the_{function_name} is a function to output the field and not threat it has data unlike get_the_{function_name}.
Here is a simple example with the_ID()
function the_ID() {
echo get_the_ID();
}
And here is an example with get_the_ID()
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
Understanding those pattern better your understanding of WordPress conventions and its inner workings.
You can always check official documentation to understand WordPress functions.
https://developer.wordpress.org/
or check in with plugin developers docs.

WordPress function for detecting what page has loaded?

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.

Categories