Setting content for multiple pages in wordpress - wordpress

Hi What i'm trying to do is in the page.php file set up a bunch of if statements for each page for example if page == aboutus display the about us content and so on. at the moment no matter what page i'm on it's displaying everything in the page.php file i'm sure its just something with the way i have written it. does anyone know what i'm doing wrong?
heres what i have so far
<?php get_head(); ?>
<?php is_page($page); ?>
<?php is_page('about-us'); ?> {
<div id="container">
<?php get_header(); ?>
<?php get_banner(); ?>
<div class=" center content" role="main">
<div id="posts">
<span class="title">About Us</span>
<div class="msghead">
<?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content();
endwhile;?>
</div>
</div>
<div id="sidebardiv">
<?php get_sidebar(); ?> </div>
<div class="clear"> </div>
</div>
</div>
<?php get_footer(); ?>
</div>
}
<?php is_page('classes'); ?> {
<div id="container">
<?php get_header(); ?>
<?php get_banner(); ?>
<div class=" center content" role="main">
<div id="posts">
<span class="title">Classes</span>
<div class="msghead">
<?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content();
endwhile;?>
</div>
</div>
<div id="sidebardiv">
<?php get_sidebar(); ?> </div>
<div class="clear"> </div>
</div>
</div>
<?php get_footer(); ?>
</div>
}

You are not actually checking the page, you are just running the is_page() function, then displaying the content.
Additionally, when you use The Loop, you should first check that there are posts to display, to avoid errors.
And just as a tip - if the only difference in the pages is the posts that are shown, you can set your arguments before the loop, that way you can cut out quite a lot of your code. Here is a link to a Pastebin with an example of that method, taking the below. Furthermore, if the template is page.php, then the Page that you are viewing is already in the Loop, so you can make use of that to show the Page title. This is untested, but should get you started.
<?php get_head(); ?>
<?php if(is_page('about-us')) : ?>
<div id="container">
<?php get_header(); ?>
<?php get_banner(); ?>
<div class=" center content" role="main">
<div id="posts">
<span class="title">About Us</span>
<div class="msghead">
<?php
query_posts('cat=7&showposts=1&orderby=date');
if(have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile;
endif;
?>
</div>
</div>
<div id="sidebardiv">
<?php get_sidebar(); ?> </div>
<div class="clear"> </div>
</div>
</div>
<?php get_footer(); ?>
</div>
<?php elseif(is_page('classes')) : ?>
<div id="container">
<?php get_header(); ?>
<?php get_banner(); ?>
<div class=" center content" role="main">
<div id="posts">
<span class="title">Classes</span>
<div class="msghead">
<?php
query_posts('cat=7&showposts=1&orderby=date');
if(have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile;
endif;
?>
</div>
</div>
<div id="sidebardiv">
<?php get_sidebar(); ?> </div>
<div class="clear"> </div>
</div>
</div>
<?php get_footer(); ?>
</div>
<?php endif; ?>

An easier approach will be to use is_page('about-us') to check your current page.
Codex: http://codex.wordpress.org/Function_Reference/is_page

You're missing an if statement.
e.g.
<?php if( is_page('about-us') ): ?>
<p>about-us page here</p>
<?php elseif( is_page('classes') ): ?>
<p>classes page here</p>
<?php endif; ?>
A more straightforward way is to create new templates for page-about-us.php and page-classes.php

Related

<?php wp_link_pages(); ?> is working on posts but not on pages to display pagination

I created a theme from scratch and have pagination working on the post pages using , it's working on single.php.
<?php get_header(); ?>
<?php
while(have_posts()) {
the_post();
?>
<div class="mainConent">
<div class="leftSidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('left_sidebar') ?>
</div>
</div>
<div class="recentBlogsWrapper">
<div class="blogWrapper">
<h2><?php the_title(); ?></h2>
<p><?php the_time('F j, Y') ?></p>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php comments_template(); ?>
<?php } ?>
<div class="backarrowwrap">
<a class="backhomelink" href="<?php echo site_url(); ?>" <?php if(is_front_page()) echo 'class="active"' ?>>
<img class="backarrow" src="<?php echo get_template_directory_uri(); ?>/img/backarrow.png" alt="back arrow" />
Go Back Home
</a>
</div>
</div>
</div>
</div>
<div class="rightSidebar" id="sidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('right_sidebar') ?>
</div>
</div>
I put it right below the_content(); and it's working exactly like I want it to. However, it's not working on page.php, even though it's in the same place on the page.
<?php get_header();
while(have_posts()) {
the_post();
?>
<div class="pageWrapper">
<h2><?php the_title(); ?></h2>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>
I've included the
<!–-nextpage-–>
code in one of my pages just like I did for the blogs, but it's not paginating between the paragraphs.
What am I doing wrong?
You need at least one <!--nextpage-->, if the content of a page (or post) has at least one <!--nextpage--> tag (and this code is in The Loop), this prints linked page numbers (“Pages: 1 2 3 4 and so on...”), without a link on current page number, and by default within tags:.

<! --nextpage--> pagination is not working in Wordpress

I'm trying to make it so posts can be easily paginated in a custom theme, but I'm doing something wrong. I've included wp_link_pages(); in the while like which I believe is required, but maybe I have it in the wrong place or I'm missing something else?
<?php get_header(); ?>
<?php
while(have_posts()) {
the_post();
wp_link_pages();
?>
<div class="mainConent">
<div class="leftSidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('left_sidebar') ?>
</div>
</div>
<div class="recentBlogsWrapper">
<div class="blogWrapper">
<h2><?php the_title(); ?></h2>
<p><?php the_time('F j, Y') ?></p>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php comments_template(); ?>
<?php } ?>
<div class="backarrowwrap">
<a class="backhomelink" href="<?php echo site_url(); ?>" <?php if(is_front_page()) echo 'class="active"' ?>>
<img class="backarrow" src="<?php echo get_template_directory_uri(); ?>/img/backarrow.png" alt="back arrow" />
Go Back Home
</a>
</div>
</div>
</div>
</div>
<div class="rightSidebar" id="sidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('right_sidebar') ?>
</div>
</div>
<?php get_footer(); ?>
Another odd thing that's happening is that the code block is showing the code on the page instead of hiding it like it should.
The wordpress editor
My blog post page which shows the code
Gutenburg was preventing me from writing the comment as code, even in a code box. Once I switched to the classic view the problem went away since I could write the comment in the html directly.

How to put page title over featured image in wordpress?

How to put page title over featured image in wordpress?I want the page title and page content over the featured image .
Please suggest.
You should use <div> tag in image and title; Use this code:
<?php
/*
* Template Name: yourtie
*/
get_header();
?>
<section id="cir-content-area" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php while ( have_posts() ) : the_post(); ?>
<?php $featured_img_url = get_the_post_thumbnail_url($post->ID, 'full'); ?>
<div class="col-md-12" style="background:url('<?php echo $featured_img_url; ?>') no-repeat scroll center center / cover">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>

Simplify while loop containing posts code in wordpress

The code below should display the content of the page followed by certain page content.
<!-- Section -->
<section>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- Article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Posts for homepage -->
<?php
if ( is_front_page() ) { ?>
<?php the_content(); ?>
<!-- Show page content according to page ID -->
<div class="title-home clearfix">
<div class="four title-home-text">Services Spotlight</div>
<div class="four title-home-text" style="margin-left: 115px;">Industry Expertise</div>
<div class="four title-home-text" style="margin-left: 125px;">Features & Benefits</div>
</div>
<div class="four-wrapper clearfix">
<div class="four-container">
<div class="four-col line">
<?php
query_posts('page_id=40');
while (have_posts()): the_post();
the_content();
endwhile;
?>
</div>
read more
</div>
<div class="four-container">
<div class="four-col line">
<?php
query_posts('page_id=41');
while (have_posts()): the_post();
the_content();
endwhile;
?>
</div>
read more
</div>
<div class="four-container">
<div class="four-col line">
<?php
query_posts('page_id=42');
while (have_posts()): the_post();
the_content();
endwhile;
?>
</div>
read more
</div>
<div class="four-container">
<div class="four-col line">
<?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-3')) ?>
</div>
</div>
</div>
<?php
}
else {
?>
<h1><?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php } ?>
<!-- end post homepage -->
<br class="clear">
<?php edit_post_link(); ?>
</article>
<!-- /Article -->
<?php endwhile; ?>
<?php else: ?>
<!-- Article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /Article -->
<?php endif; ?>
</section>
<!-- /Section -->
<?php get_footer(); ?>
However, it displays the last query as loop.
Output Code:
<article id="post-6" class="post-6 page type-page status-publish hentry">
<article id="post-42" class="post-42 page type-page status-publish hentry">
post-42 should not be displayed in the article.
Also, I know the code is not simplified for using while loop. I want to fix the article problem and simplify this code.
The codex advises against using query_posts for secondary loops as per Codex Query Posts Page
It looks like you are stomping on the main loop (article) since query_posts alters the main loop. If you want to use query_posts it is recommended that you call wp_reset_query() when done. The preferred method is to use WP_query().

Wordpress next/prev post on single custom post page?

So I've got a custom post type, all working fine.
Goes to single-portfolio, but it won't paginate.
I can't, no matter what I try get pagination to work on the single page.
Here's my code.
<?php get_header(); ?>
<div class="main-content">
<div class="portfoliopost">
<div class="col-full">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="port-left-single">
<h2><?php the_title(); ?></h2>
<div class="projecttype">
<ul>
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '<li>' .$tag->name. '</li>';
}
}
?>
</ul>
</div>
<?php the_field('main_portfolio_meta'); ?>
</div> <!-- end div port-left-single -->
<div class="port-right-single">
<?php the_content(); ?>
</div>
<div style="clear:both;"></div>
</div> <!-- end div post-class -->
<div class="navigation">
<div class="next-posts"><?php next_posts_link('Older Posts') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Posts') ?></div>
</div>
<?php endwhile; endif; ?>
</div> <!-- end div col-full -->
</div> <!-- end div portfolipost -->
<div style="clear:both;"></div>
</div> <!-- end div main content -->
<?php get_footer(); ?>
It's inside the loop, so what gives?

Resources