I want to show the page title in single post page of wordpress theme. I tried to use <?php echo get_the_title() ?> but it return the post title, not the page title.
basically I want to show MY page title, in this case "Blog" below my header area in single.php file. how do I make it?
In order to get page title you need to use WordPress API function use the code below
<?php
$post_7 = get_post($id);
$title = $post_7->post_title;
?>
In wordpress both posts and pages have ids. So same function will work to get title of either post or page.
You have to pass this parameter to get the title of the page.
For reference see this link
http://codex.wordpress.org/Function_Reference/get_post
Related
I have a page on my Wordpress website called Blog, this is set to a static page and is the Blog page so it has a list of blog posts. On the blog page I have a featured image set. I want to use this featured image as a background image on the Blog page.
home.php (template for blog page):
At the top of home.php before the Loop that pulls the list of blog posts I have:
<?php
$page_id = get_queried_object_id();
if ( has_post_thumbnail( $page_id ) ) :
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page_id ), 'optional-size' );
$image = $image_array[0];
else :
$image = get_template_directory_uri() . '/images/default-background.jpg';
endif;
echo $image;
?>
<div class="feature" style="background-image: url('<?php echo $image; ?>')"></div>
This works and it sets the featured image. However it seems to have set the featured image on every single page on the website to the featured image from the Blog page. Even after I changed the featured image on the About Us page it still pulls the URL for the Blog page's featured image and sets that as the featured image on the About Us page.
I have the website here: http://www.cqwebdesign.co.uk/Action-Harpenden-Physiotherapy/
As you can see by these screenshots I have set different featured images on the Blog and About page:
http://i.imgur.com/DnU8V9F.jpg
http://i.imgur.com/qshulWT.jpg
Anyone know how I can fix this?
As the home.php template affects only your Blog Page, it would be interesting how your page.php template code looks like (Notice: page.php is the default template for pages like your 'About' Page) and why the featured image isn't shown correctly on this page(s).
Try to debug or echo your id values for the Blog Page and the About Page and verify if the corresponding page id is the same as the one referred by get_queried_object_id();.
I think you're having a page.php template but just in case, if you don't have one please notice Wordpress' Template Hierarchy
Wordpress Codex:
When the static front page feature is configured properly, WordPress will not use a Custom Page Template to display the blog posts index, even if a Custom Page Template is assigned to the page designated as the "Posts page". WordPress will only use either home.php or index.php.
I am working on wordpress site, I want to make all pages content editable from backend i.e admin panel,every page site have many sections so not easy to handle only with page content area.
What is the most easiest and preferable way to make all content editable.
add page in the admin panel with the contents that you needed to show in the front page. then using the page ID you can get the contents in your template page.
<?php
$id=47;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
How to change header in Wordpress Blog page and all Post pages only.
I want a custom header in Blog page and all single posts page.
Thanks
You can use conditional tags within Wordpress:
http://codex.wordpress.org/Conditional_Tags
Basically, you will need to create the content you want only for the blog and post pages and wrap this within the conditional tags:
P.S Don't forget to include the category, and archive pages - as well as the post pages.
is_single(), is_archive(), is_category()
<?php if (is_single()) { ?>
// This is a post page
<?php } else { ?>
//This is not a post page
<?php } ?>
Hope this helps.
To include different header for specific pages change get_header($name) function call inside templates and add $name attribute. Wordpress will load header-{name}.php if file exists, if not, default header header.php will be loaded. Check out template hierarchy for templates where you need to change get_header() function call ( single.php, page.php ).
Wordpress documentation:
get_header
template hierarchy
I've created a 404 error page in Wordpress and am trying include a contact form in the 404.php template. So, I've installed the "Fast secure contact form" plugin and have created a post that includes the shortcode for said plugin. When I view the post for this plugin the contact form shows properly. When I try to manually inject the post into the 404.php template using:
$page_id = 4015;
$page_object = get_post( $page_id );
echo $page_object->post_content;
The shortcode just displays as text. Any help with this would be greatly appreciated ...
Why not do it like this, add the shortcode to your 404 page.
<?php echo do_shortcode('[si-contact-form form="1"]'); ?>
You need not have to create a new post for the contact form.
I use Barecity Theme for WordPress. i built everything, but now i need to add a single.php file to theme. now it displays all post content at homepage, I created a short_desc Custom Field. and I call it from code with;
<?php //get_post_meta($post->ID, 'short_desc', true); ?>
it is fine. but i need to display this short desc at home page listing, and the main content at details page. how can do that?
I appreciate helps!!
It sounds like what you are trying to do is show a custom field that you have set on a post on the index page (which lists all the posts).
To do that you'll need to modify index.php by adding your snippet where you would like to have the short description.
<?php echo get_post_meta($post->ID, 'short_desc', true); ?>
You need to use echo to display the results from the get_post_meta function.
Depending on how your posts are setup you can also use the More button when you write your posts. This will cut off your post at a certain point that you decide and only show that short part on the index and archive pages.
Another option would be to use
<?php the_excerpt(); ?>
Which shows the first 55 words (this can be adjusted though) of the post.
Hope that helps,
Paul