WordPress: Can't get title to not display on home page - php

This should be so simple, but in this case, it isn't. In the templates/content-page.php of my theme I want the title to display on all pages, except one... the homepage. I have tried various versions of this code, but nothing works.
<?php if ( !is_front_page() || !is_home() ) { ?>
<header class="page-header">
<h1 class="page-title"><?php echo get_the_title(); ?></h1>
</header>
<?php } ?>

First, we don't need to put the echo with the the_title() function. It will echo automatically.
if ( !is_front_page() && is_home() ) {
// Default homepage ( both the front page and the recent posts page){
<header class="page-header">
<h1 class="page-title"><?php the_title(); ?></h1>
</header>
}

Do you really need to do this? I have in my page.php file
<?php get_header();
$p = get_post();
?>
<div id="main">
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class='pageTitle'><?php the_title();?></h1>
<p><?php the_content(__('(more...)')); ?></p>
<hr>
<?php endwhile; endif; ?>
</div>
<?php get_sidebar('Right Sidebar');?>
</div>
<?php get_footer(); ?>

Related

Add if statement to a class in php in wordpress

I have the following code:
<?php if ( is_post_extra_title_meta_enabled() ) { ?>
<div class="post-header">
<h2 class="entry-title"><?php the_title(); ?></h2>
</div>
<?php } ?>
I'd like to make the class H1 IF it is on the frontpage/homepage in wordpress. How would I add an if/else command to this? So basically:
If on front page:
<h1 class="entry-title"><?php the_title(); ?></h1>
Else:
<h2 class="entry-title"><?php the_title(); ?></h2>
To determine if you're on the home page in wordpress, you can use the is_home() function.
So if you're looking to add an if else inside the original if statement you provided, your code would go as such,
<?php if ( is_post_extra_title_meta_enabled() ) { ?>
<div class="post-header">
<?php if ( is_home() ) { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<?php else { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } ?>
</div>
<?php } ?>
I'm biased towards using the alternative syntax, so here's the code rewritten.
<?php if ( is_post_extra_title_meta_enabled() ): ?>
<div class="post-header">
<?php if ( is_home() ): ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else: ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php endif; ?>
</div>
<?php } ?>
Depends on your WP configuration you might use is_front_page() or is_home() (as Ryan mentioned)

adding content above recent posts in wordpress

I am trying to insert meta slider to display above my posts in wordpress.
I correctly inserted the code in the theme index.php file above the loop. it works correctly, but it also displays on top of the side bar as well, and i would like it just above the posts.
is there any advice on how i can do this? where in the loop would i need to insert it?
here is what i have
<?php
/**
* The main template file
*
* #package SimpleMag
* #since SimpleMag 1.0
**/
get_header();
global $ti_option;
?>
<?php $archive_sidebar = get_field( 'page_sidebar', get_option('page_for_posts') ); ?>
<section id="content" role="main" class="clearfix animated">
<?php
echo do_shortcode("[metaslider id=437]");
?>
<?php if ( $ti_option['posts_page_title'] == 'full_width_title' ) : ?>
<header class="entry-header page-header">
<div class="wrapper title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="wrapper">
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
<div class="grids">
<div class="grid-8 column-1">
<?php endif; ?>
<?php if ( $ti_option['posts_page_title'] == 'above_content_title' ) : ?>
<header class="entry-header page-header">
<div class="title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="grids <?php echo $ti_option['posts_page_layout']; ?> entries">
<?php
if ( have_posts() ) : while ( have_posts()) : the_post();
get_template_part( 'content', 'post' );
endwhile;
?>
</div>
<?php ti_pagination(); ?>
<?php else : ?>
<p class="message">
<?php _e( 'Sorry, no posts were found', 'themetext' ); ?>
</p>
<?php endif;?>
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
</div><!-- .grid-8 -->
<?php get_sidebar(); ?>
</div><!-- .grids -->
<?php endif; ?>
</div><!-- .wrapper -->
</section><!-- #content -->
<?php get_footer(); ?>
As long as the sidebar is deactivated, your placement is fine. When the sidebar is active you should place it just below <div class="grid-8 column-1">. That's the beginning of the main area of your page, next to the sidebar. (I'm guessing the div is using something similar to bootstrap, taking up 8 of 12 available grids).
To prevent future updates of your theme to overwrite your customizations, if this is in fact a supported theme, you should also consider creating a child theme. It's real easy and is explained here: https://codex.wordpress.org/Child_Themes

Customizing WordPress theme (Twenty Thirteen). How to remove one title from the header without removing them all

I am working on a WordPress theme and I want to edit the page where the title headers are located. (To be more specific, I am working on the'twenty thirteen' theme). Now, I only want to remove the title in the home page which says 'Home'. However, I cannot do that because that would remove all the titles in all the pages. So I have to make an if/else statement. The problem is nothing is working with me - pasted below is the code:
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
As I found out function the_title() give you the page title. simply use this and put your html code between if block to customize it.
<?PHP if(the_title() === 'home' /* is_home() */): ?>
... <!-- your html -->
<?PHP endif; ?>
You can achieve that by using the is_home() method.
Here is the reference to the method.
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif;if(!is_home()):?>
<h1 class="entry-title"><?php the_title(); ?></h1></header><!-- .entry-header --><?php endif ?>

Combining wordpress admin page content with custom template?

Is there a way to combine content from a wordpress page's main textarea with the content in its custom template?
In this case I have a custom template that displays all posts from a single category, but I would also like to have a section that displays what is written in the wordpress admin Page area.
This is how I have the custom template set up to display relevant posts:
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Above this I would like to have the wordpress pages admin area content display, what a user would normally write into the textarea to display on the page, is this possible?
This is what I've come up with:
<?php get_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content() ;?>
<?php endwhile; ?>
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Is this acceptable? It's working but I'm not sure how classy it is!

Sticky Post appears twice in homepage loop. Is there a way to make it appear only once?

So in WordPress, if you run the regular loop, Sticky Posts appear twice. Once at the beginning (as desired) and then once in the chronological order among regular posts. Is there a way to make a post sticky and not appear the second time?
Here's the code from the index.php file.
<?php get_header(); ?>
<div id="content">
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Default') ) : ?>
<?php endif; ?>
</div>
<div class="textContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<div class="featuredImage">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'main-thumb' ); } ?>
</div>
<div class="readMore">
<?php // the content of the post
the_content('Read More'); ?>
</div>
<div class="comments">
<?php _e('', 'surplus'); ?> <?php comments_popup_link('{0 Comments}', '{1 Comment}', '{% Comments}'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="nav3">
<?php posts_nav_link(); ?>
</div>
<?php get_footer(); ?>
Thank you!
Liz

Categories