Im trying to have different text appear under the footer bar, per page on my WP site.
I create a new footer(s) uploaded it and edited the page.php
and replaced the get_footer with this code here:
<?php if ( is_page('11') ) { ?>
<?php include(TEMPLATEPATH.'/footer2.php');?>
<?php } else { ?>
<?php include(TEMPLATEPATH.'/footer.php');?>
<?php } ?>
It works as I need it for doing the one page. I need to add a few more pages though. Everything I try adding to the above code for more pages doesnt work. Im not good at php so can someone explain the correct way to add to this?
Edit:
Changed it all to this. This is my page php.
<?php get_header(); ?>
<?php get_sidebar('top'); ?>
<?php
if (have_posts()) {
/* Start the Loop */
while (have_posts()) {
the_post();
get_template_part('content', 'page');
}
} else {
theme_404_content();
}
?>
<?php get_sidebar('bottom'); ?>
<?php get_footer( $page->11 ); ?>
This is the error I get
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in /home/content/path/removed/page.php on line 16
You can add different footers in pages using get_footer:
<?php get_footer('2'); ?>
This will include footer-2.php.
If you want more pages with footer2, pass array to is_page function:
<?php if ( is_page(array('11', '12', '13', '14')) ) { ?>
You can replace your whole if clause with a single line:
<?php get_footer( $post->ID ); ?>
Now PHP will look for a file named footer-<pageid>.php. For example: If you want to use a custom footer file for a page with the ID 11, just name the file footer-11.php. Wordpress will use it on this page, on all other pages it will fall back to the default footer.php
Related
I trying to loop through posts in a custom php page but no matter what I do, no posts are found
here is the code I wrote in my-custom-page.php
<?php
require_once("/wp-load.php");
get_header();?>
<div id="blog">
<?php if(have_posts()) : ?>
<?php echo"anything"; ?>
<?php endif; ?>
</div>
<?php get_footer();?>
You should require wp-load.php via the full path to this file.
Hardcoded example:
require_once("user/home/public-html/wordpress/wp-load.php");
Softcoded example (suposing your file is in the same directory as WordPress):
require_once(dirname(__FILE__)."/wp-load.php");
You have also to query the posts before you display them. So, you need to add this line to your code:
query_posts('post_type=post');
The query arguments may vary depending on what you want to display. Some of them are the member variables of the WP_Post class. Go to https://codex.wordpress.org/Class_Reference/WP_Post for reference.
Here you have a re-writing of your code that displays the titles of the 30 latest posts published:
<?php
require_once(dirname(__FILE__)."/wp-load.php");
query_posts('post_type=post&showposts=30');
get_header();?>
<div id="blog">
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_title();
echo '<br />';
endwhile;
else :
echo 'Sorry, no posts found.';
endif;?>
</div>
<?php get_footer();
wp_count_posts :
#return object Number of posts for each status.
you're trying to echo an object which ends in a fatal error. Furthermore if you want to see all posts the_post is not right. Look for it at the function reference : https://codex.wordpress.org/Function_Reference/the_post. I would do it other (google smth like "get all posts").
if you will use the code inside your theme
use the same code of Mr.Carlos but with out dir
require_once("/wp-load.php");
Am I doing this right? It works but wanted to ask if what I have done is bad practice?
Originally if the page is home then will get include a php file otherwise if it is a generic page will call in the featured image in WordPress.
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} ?>
<?php echo get_the_post_thumbnail($post->ID); ?>
Your code is wrong in my opinion based on what you requested in question. You only check if is home and if it is get template part 'hero'. After that no matter what page is you call get_the_post_thumbnail.
otherwise if it is a generic page will call in the featured image in WordPress
otherwise means else and your code should look like this:
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} else {
echo get_the_post_thumbnail($post->ID);
}
?>
Do not forget to be in loop to call $post->ID
You can aford endif by using <?php if(){ ... } ?> or <?php if(){ ?> ... <?php } ?>
no, it's okay. If the condition is not fulfilled, nothing will happen.
I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>
I'm trying to use the is_page function in wordpress and it's not working.
I read here that <?php wp_reset_query(); ?> should solve the problem, but it didn't help. Here is the code:
<?php wp_reset_query(); ?>
<?php if (is_page(379) ): ?>
<!-- Google Analytics Content Experiment code -->
<?php endif; ?>
Any idea/suggestions on this?
Thanks!
None of the other solutions worked for me. What did work was to use the wp hook.
add_action('wp', 'page_check');
function page_check() {
if (is_page('my-page-slug')) {
// code to run on this page
}
}
Try this
<?php if (get_the_ID()==379): ?>
<!-- Google Analytics Content Experiment code -->
<?php endif; ?>
this works fine for me
<?php if(is_page(379)): ?>
<?php echo 'x'; ?>
<?php endif; ?>
I had a similar problem recently and it's because I was using query_posts somewhere on the page.
The way I tested it was to echo the $post->ID at the very top of the template file and it gave me the correct ID.
when I echoed it at the bottom (after my query_posts), it was not getting the correct ID.
So you might want to test by doing this and changing any query_posts you have to use WP_Query instead: http://codex.wordpress.org/Class_Reference/WP_Query
can someone explain why the else statement is not working in WordPress for the sidebar.php?
<?php if(is_front_page() ) : ?>
**content shows on main page sidebar**
<?php elseif(!is_front_page() ) : ?>
<?php // else: ?> // tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php endif;?>
The is_front_page() conditional will always return false if it is used inside the loop or after the loop has ran (like in a sidebar). You can call wp_reset_query(); after your loop to reset the page query_vars.
See this answer on WPSE for more info.
Im not sure if is_front_page() exists, but just use {} around each condition result:
<?php if(is_front_page() ) { ?>
**content shows on main page sidebar**
<?php } else { ?>
// tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php } ?>