get_template_part - Wordpress not working - php

I am trying to place the content of the about page inside a div on the header , the template part is located on folder template-parts/content-about.php
on the header the code is:
<div id="slideOut">
<div class="slideout-content">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'about' );
endwhile; // End of the loop.
?>
<?php include 'content-about.php'; ?>
</div><!-- .slideout-content -->
</div>
And the content-about.php looks like this:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<?php
the_content();
wp_link_pages(
array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'about' ),
'after' => '</div>',
)
);
?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
The issue is that is showing on the div the same content as the current page for example if I'm located on home page it shows only the content of the home page inside the div when it should always show the content of the about page.
why is not working?
Thanks

get_template_part() is working as expected. Your content-about.php is included normally.
However, it prints the content of the current post/page because you're calling the_content().
You could replace the_content() with get_the_content(), passing the post id of your about page as the third parameter.
Note that:
An important difference from the_content() is that get_the_content() does not pass the content through the the_content filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.
So, you might want to use apply_filters() like this:
<?php
echo apply_filters( 'the_content', get_the_content( null, false, $about_page_id ) );
// where $about_page_id is the post id of your about page

You don't even need the get_template_part for this if you don't want.
You can do this:
<div id="slideOut">
<div class="slideout-content">
<?php echo get_the_content('','','your-about-page-id-here'); ?>
</div><!-- .slideout-content -->
</div>
get_template_part() is a way to include files with markup that may rely on the loop, but keep things separated. You don't necessarily need it here.
You can also use:
echo get_post_field('post_content', your-post-id-here );

Related

Add Columns to Understrap / Underscores Blog Page Template Posts

I'd like to customize the Underscores Wordpress Theme Blog Template to include columns around each blog post entry.
Ideally, these would become a grid of posts with pagination, but right now I'm just trying to get the grid to work.
This is from the content.php file:
<?php
/**
* Post rendering content according to caller of get_template_part.
*
* #package understrap
*/
?>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ),
'</a></h2>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php understrap_posted_on(); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
<div class="entry-content">
<?php
the_excerpt();
?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'understrap'
),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php understrap_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-## -->
</div>
Thanks for your help!
i assume you talking about UnderStrap, not about Underscores, right?
To add the Bootstrap grid (http://getbootstrap.com/docs/4.1/layout/grid/) to your article/blog view you have to do two things:
Step 1
Wrapping an outside row around your loop.
To do this:
open the themes index.php and search for:
<main class="site-main" id="main">
Add the opening right behind this so that you have this:
<main class="site-main" id="main"><div class="row">
Now add the closing tag right before the closing tag:
</div></main>
Step 2
While the outside row wrapper needs to be added just once around all articles you need to add a Bootstrap col class plus the right size variable to your loop-templates/content.php file. So that it applies to all articles in your loop.
Open the file and add this:
<div class="col-6">
right before the opening <article> tag.
The col-6 class means "use 6/12 of space, e.g. 50%.
So you will have two articels side-by-side.
Of course you can use col-4 (4/12=33.33%) to have three articles side-by-side etc.
Depending on your needs another good starting point would be to utilize the Bootstrap card-deck component:
http://getbootstrap.com/docs/4.1/components/card/#card-decks

WP; get_sidebar not playing nice with get_content when inside loop querying pages

So I hope that title isn't too confusing, let me try to break it down.
I have a content-page.php file which has both the get_content and get_sidebar functions in it. If I navigate to that page on the front end, I see both my content and sidebar.
On the home.php page, I'm loading a series of pages by using $the_query = new WP_Query and then inside that query using the loop and inside that loop, calling the content-page.php.
The problem is that if the page that loads on the home.php page has a sidebar, for some reason, nothing loads after the sidebar ie. get_content() returns nothing, comments_template() returns nothing, etc.
Here's a (very) simplified version of the markup, Homepage:
$the_query = new WP_Query( array(
'post_type' => 'page'));
$x = 0;
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'content', 'page' );
$x++;
endwhile;
wp_reset_query();
content-page.php:
<article <?php post_class(); ?>>
<header>
<h1><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php include( TEMPLATEPATH . '/sidebar.php'); ?>
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
sidebar.php:
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' );
dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary .widget-area -->
Is there something obvious that I'm missing here? Been working on this one for a while. As always, any help is greatly appreciated.
There are two types of general Loop queries in WordPress:
the main query, which is set based on the HTTP request, and
'sub loops' which can change the main query.
Since the main query controls many things like "is there a sidebar?" or "what page template should be used?" you can only submit one of these types. You can't call a separate sidebar (or call a separate page template for that matter) from within a sub loop. To do this, you would have to re-do the basic WordPress sidebar system.
I think the best bet for what you are trying to do, would be to just serve up some kind of dynamic content and call it a sidebar. In other words, WordPress has already decided what's happening with the sidebar the first time it was called. The system doesn't have a default way to handle calling the sidebar over and over. Using a shortcode or a filter after each iteration of the sub loop would be a better approach.
Close ")" open brackets.
$the_query = new WP_Query( array( 'post_type' => 'page') );
Paste this code in content-page.php
<?php get_template_part( 'sidebar' ); ?>
<?php the_content(); ?>
Try removing do_action( 'before_sidebar' );
You could try:
include( locate_template( 'content-page.php', false, false ) );
instead of:
get_template_part('content', 'page');

Not able to remove WordPress page title without removing the content aswell

I am making a WordPress theme with the framework from Underscores.me.
I do not wish to show the page title on pages. So I thought I could use some simple CSS to do it:
.entry-title {display:none;}
But doing this also removes all content on the page basically just showing an empty page.
Then I thought I could remove the title by deleting this in my theme:
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
But by removing the above also removes all content from showing.
Its like the title has to be shown in order to show content. I never had this problem before and I have developed a few themes with Underscores.me. But it is like the new version has to show page titles in order to show the content.
Have anyone any idea what to do?
I am running on a localhost so I cant show my problem but I am using the newest theme from Underscores.
Even if I install a "disable page title" plugin the plugin also removes the content when disabling the title.
I really hope someone had this problem before! :)
Extra info:
This is how the page.php looks like:
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // End of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
This is the template it calls for:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'open2day' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
As I said, if I remove the title with CSS it also removes the content.
please use !important in your css like below
hope this will work.
.entry-title {display:none !important;}
When you comment out the following line:
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
your page no longer has an h1 element of class "entry-title".
Your CSS is expecting this element to be on your page, so unless you want to edit your CSS, you can just add the following empty element:
<h1 class="entry-title"></h1>
This should solve your problem and allow the content to be displayed whilst hiding the page title.

Wordpress front-page.php template

I have my front page set to a static page and am trying to build my custom template. How do I actually show the selected front page in front-page.php? I have googled and googled but can't seem to figure out how to do it.
The front-page.php actually loads like it should, but I can't seem to find documentation on exactly how to show the page that is assigned as the static home page. Any suggestions?
I have tried
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
but that didn't seem to work...
Your static page uses a page template (usually page.php for the default template)
You can create a new one for the homepage if you wish. see : Creating_Your_Own_Page_Templates copy page.php to homepage.php and change the template name
Example template (homepage.php) :
<?php
/*
Template Name: Homepage
*/
//the content of page.php and now you can do what you want.
?>
$id = 0; /* The id of your page */
$page = get_page($id);
echo apply_filters('the_content', $page->post_content);
If its a static page, I should not use a loop.
First, take a look to topic to show something only on home page. a related question is Wordpress Post Thumbnail Issue (Only 1 thumbnail on frontpage). Also, it can be useful how to create a static front page in wordpress.
I was missing something obvious. The loop I was using I had copied out of wordpress template. It actually called another template file. What I should have used was:
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php endwhile;?>

Wordpress Single post content not showing up

I've got a wordpress blog theme which shows the content of all posts on the index fine but when I click into one of the posts the content of the post is blank and I can't seem to figure out why. If I'm correct the single.php controls that page.
http://pastebin.com/afLVxMPb = My single.php
an example of what I mean would be http://www.ndesign-studio.com/demo/wordpress/blog/how-about-a-blog-post-with-longer-title but on this site the content of the blog post does show up but on mine it doesn't.
I think the problem is somewhere here...
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>
</div><!-- .entry-content -->
You should add "the loop" somewhere in your single.php file and call setup_postdata($post) or the_post() so you can access the post data inside that loop.
Read more about the loop here: http://codex.wordpress.org/The_Loop
For example, your single.php file will look something like this (simplified):
........
<div id="content">
<?php if(have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
// etc.. all post info
............
<?php endforeach; ?>
Hope that helps! Good luck.

Categories