Add Columns to Understrap / Underscores Blog Page Template Posts - php

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

Related

get_template_part - Wordpress not working

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 );

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.

Conditional Classes for Wordpress loop posts

The css is already declared all I have to do is add some div classes. My theme uses an almost empty index.php with no html but uses the get_template_part functions and calls various template parts for post types, so it has several loop{template-name}.php files. I have copied the one for the blog index and made a new loop with modifications. The index for my blog, calls the appropriate template with the modifications - hooray. However I have to be able to conditionally add div classes to make the conversion work completely.
As the index.php for the blog runs through the loop I would like to be able to conditionally add one div class to the 1st returned post, and another div class to the 2nd and if the last post is odd then add a 3rd div class.
Here is my loop-template4.php file.
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div class="one"> /*I added this line here and it works for not found.
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'smpl' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found
for the requested archive. Perhaps searching will help find
a related post.', 'smpl' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
</div><!-- #post-0 -->
<?php endif; ?>
<?php
<?php while ( have_posts() ) : the_post(); ?>
/**
* I need to conditionally add one of 3 div classes here:
* odd `<div class="one_half">`,
* even `<div class="one_half last">`or
* odd and last, `<div class="one">`
*/
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="
<?php printf( esc_attr__(
'Permalink to %s', 'smpl' ), the_title_attribute( 'echo=0' ) );
?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
<?php do_action('skeleton_post_meta'); ?>
<?php if ( is_archive() || is_search() ) : // Only display excerpts for
archives and search. ?>
<div class="entry-summary clearfix">
<?php if (ot_get_option('show_post_thumbnails') && has_post_thumbn
nil()){
echo '<div class="alignleft">';
skeleton_thumbnailer('fourthree');
echo '</div>';
}?>
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php
if (ot_get_option('show_post_thumbnails') && has_post_thumbnail()) {
echo '<div class="alignleft">';
skeleton_thumbnailer('thumbnail');
echo '</div>';
}?>
<?php //the_content( __(
'Continue reading <span class="meta-nav">→</span>', 'smpl' ) );
do_action('skeleton_content');
?>
<div class="clear"></div>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' .
__( 'Pages:', 'smpl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
</div> <!-- conditional div class -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) {
do_action('skeleton_page_navi');
}?>
So the Question:
Is it possible, as the loop hasn't looped yet, and I need to conditionally add the div class based on the number of results from the loop?
As far as I know, there's no way to know when the last post in the loop is being displayed, so first we need to add some code right before your line `while (have_posts()) : the post(). For context, I've copied your entire block of code here, and have added comments to show where the new code is:
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div class="one"> /*I added this line here and it works for not found.
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'smpl' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found
for the requested archive. Perhaps searching will help find
a related post.', 'smpl' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
</div><!-- #post-0 -->
<?php endif; ?>
<?php
/**
* BEGIN: Additional code to add conditional classes
*/
// How many posts are going to be displayed? Let's count here:
$total = 0;
while ( have_posts() ) : the_post();
$total++;
endwhile;
// Reset the loop to actually start displaying them
rewind_posts();
// Add this line before your loop to prevent notices being thrown
$count = 1;
while ( have_posts() ) : the_post(); ?>
<?php
// Add some conditions to determine the class
if ($count % 2) {
// If the post displayed is an odd number
$class = 'one_half last';
} else {
// If the post displayed is an even number
$class = 'one_half';
}
if ($count == $total && ($count % 2)) {
// If the post is the last post, and it is odd
$class = 'one';
}
// Finally, increment the count
$count++;
?>
<?php // If you need an extra div, just put it in like so... ?>
<div class="<?php echo $class; ?>">
<?php
// If you need the existing div to simply have the class added,
// Then we pass it into the post_class() function
?>
<div id="post-<?php the_ID(); ?>" <?php post_class($class); ?>>
<h2 class="entry-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;?>

Modify WordPress Jetpack Plugin To Use 'Class' Instead Of An 'ID' Attribute

What I am trying to achive: Enable seamless & simultaneous infinite scrolling for multiple columns on the same page, each of which is pulling in a different set of content i.e. one column shows the latest posts, while the other shows the latest posts from a specific tag.
Each column uses a different loop, so that they don't ever mess with each other, and this the code I've employed (only to give you an idea of how I'm doing it):
File: index.php (code also on pastebin)
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* #package Twenty Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<section id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<?php //twentytwelve_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix content-articles'); ?>>
<a class="archives-thumb-link" href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_post_thumbnail( 'archives-thumb' ); ?></a>
<div class="entry-text-wrapper">
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
</div>
</article><!-- #post-<?php the_ID(); ?> -->
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</section><!-- #content .site-content -->
<section id="highlights-container" class="site-content">
<?php $latest_highlights = new WP_Query('tag=highlights&showposts=20&paged=' . $paged); ?>
<?php if ( $latest_highlights->have_posts() ) : ?>
<?php while ($latest_highlights->have_posts()) : $latest_highlights->the_post(); $the_highlights_counter++; ?>
<article id="highlights-<?php echo $the_highlights_counter; ?>" class="highlights-wrapper">
<a class="highlights-link" href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark">
<?php the_post_thumbnail( 'highlights-thumb' ); ?>
<h1 class="highlights-title"><?php the_title(); ?> <span>/ <?php echo the_time('M. n'); ?></span></h1>
</a>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</section><!-- #content .site-content -->
</div><!-- #primary .content-area -->
<?php get_footer(); ?>
How I plan to do it: The 'Infinite Scroll' module in Jetpack only constitutes two files -- infinity.php and infinity.js, so for someone who knows JavaScript and PHP it'd be a little easier to look into.
Now the thing is, as stated here, to enable Infinite Scrolling, we need to first provide it with "the ID of the HTML element to which Infinite Scroll should add additional posts to." And since it doesn't accept multiple IDs, it's not possible to enable simultaneous infinite scrolling on multiple columns on the same page.
The idea: So, maybe if I'd modify it to accept a class instead of an id attribute, I can get infinite scrolling on multiple columns to work.
The question is, how do I do it?
While trying my best to solve the problem myself (which I couldn't), here are some important bits I've come across, which I think'd make it easier for you to help.
In [infinity.php][5]
'container' => 'content' says that content is the default ID for the container element.
'id' => self::get_settings()->container, defines id for the JavaScript to call.
In [infinity.js][6]
var id = $( this ).attr( 'id' ), makes sure that it's an id attribute and NOT a class.
Since, I do not know JS and enough PHP, I may have missed many other important bits. Just thought this info would aid those trying to help.
Do let me know if I am not clear.
NOTE: If you are running a test/dev WordPress site somewhere, and would like to help, please install the Slim Jetpack plugin (the version of Jetpack plugin that doesn't require you to connect to WordPress.com), and enable the 'Infinite Scroll' module from the 'Jetpack' menu. Further instructions can be found here.
Jetpack runs a partial template for the loop (e.g. content.php), retrieves the HTML output by AJAX, then appends it "live" to the current page. It never uses the original template (index.php) so it doesn't matter that you've written 2 distinct loops there.
You might be able write your own solution by borrowing Jetpack's scroll detection and AJAX requests and adapting the rest.
Will your server allow you to be listening for requests on two different ports? filter the objects that you dont want in each as they come into view?
What about binding the scroll event to four Ajax calls, using different ids.
Other than that you could try rewritting the plugin someone discusses that Here

Categories