Display posts in WordPress - php

I need to display my first two posts initially in two columns and the other three in three columns. See screenshot below:
This is what I have so far:
<section class="cases">
<div class="container">
<h4 class="title">Cases</h4>
<?php
$args = array(
'post_type' => 'cases',
'posts_per_page' => 5,
);
$the_query = new WP_Query( $args );
if ( $the_query -> have_posts() ):
?>
<div class="row">
<?php
while ( $the_query->have_posts() ): $the_query->the_post();
$slug = get_post_field( 'post_name', get_post() );
?>
<div class="col-sm-6">
<?php the_title(); ?>
<?php the_field('case_content') ?>
Bekijk de case
</div>
<?php endwhile; wp_reset_postdata();?>
</div>
<?php endif;?>
</div>
</section>
And this is what I end up with:
How to proceed from here?

Like explained in my comment I would recommend you to make the markup for all 6 elements the same and on the same level and then style them differently with css. Have a look at flexbox. You can make the first 2 elements 50% width and the others 33%. Use media queries to make them responsive.
Here is some basic example code:
.container {
display: flex;
flex-wrap: wrap;
margin: 0 auto;
max-width: 900px;
}
.item {
box-sizing: border-box;
border: 4px solid #fff;
background: #ddd;
flex: 1 1 50%;
min-height: 200px;
}
.item:nth-child(3),
.item:nth-child(4),
.item:nth-child(5) {
flex: 1 1 33%;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
This should be a good starting point to make this layout.

Related

Wordpress retrieve thumbnail in theme support

I got this problem which my limited capabilities can´t handle. I tried to use the near solutions but does not work the way I expected.
In the post nav previous link I want to retrieve the thumbnail image link and text over the image and make the whole thing clickable and zoom in on it while hovering. Like in the end of http://swordandscale.com/sword-and-scale-episode-100/. But I only manage to get the text. Here is my function,
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-10 col-lg-8 col-lg-offset-2 col-md-offset-1">
<?php
if( have_posts() ):
while( have_posts() ): the_post();
sidekick_save_post_views( get_the_ID() );
get_template_part( 'template-parts/single', get_post_format() );
echo sidekick_post_navigation();
if ( comments_open() ):
comments_template();
endif;
endwhile;
endif;
?>
</div><!-- .col-xs-12 -->
</div><!-- .row -->
</div><!-- .container -->
</main>
</div><!-- #primary -->
/*
========================
SINGLE POST CUSTOM FUNCTIONS
========================
*/
function sidekick_post_navigation(){
$nav = '<div class="row">';
$prev = get_previous_post_link( '<div class="post-link-nav">%link</div>', '%title' );
$nav .= '<div class="col-xs-12 text-center">' . $prev . '</div>';
$next = get_next_post_link( '<div class="post-link-nav">%link</div>', '%title' );
$nav .= '<div class="col-xs-12 text-center">' . $next . '</div>';
$nav .= '</div>';
return $nav;
}
First thing you need to do is open your theme’s single.php file and add the following code inside the loop most likely after the_content() area:
<div id="cooler-nav" class="navigation">
<?php $prevPost = get_previous_post(true);
if($prevPost) {?>
<div class="nav-box previous">
<?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100)
);?>
<?php previous_post_link('%link',"$prevthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php } $nextPost = get_next_post(true);
if($nextPost) { ?>
<div class="nav-box next" style="float:right;">
<?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) );
} ?>
<?php next_post_link('%link',"$nextthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php } ?>
</div><!--#cooler-nav div -->
Next thing you need to do is open your style.css file and add the following styles:
#cooler-nav{clear: both; height: 100px; margin: 0 0 70px;}
#cooler-nav .nav-box{background: #e9e9e9; padding: 10px;}
#cooler-nav img{float: left; margin: 0 10px 0 0;}
#cooler-nav p{margin: 0 10px; font-size: 12px; vertical-align: middle;}
#cooler-nav .previous{float: left; vertical-align: middle; width: 250px;
height: 100px;}
#cooler-nav .next{float: right; width: 250px;}
Feel free to change the styling to match it with your theme. Often our users like to style the code, so it makes it easier for them to tweak it. This is just basic styling which you should be able to customize easily.
If you want to change the thumbnail size, then simply change the array(100,100) to whatever you like.
hope that is usefull for you .

Masonry in wordpress only showing one column

So I know masonry in a single column has been covered a few times on stack but I'm not very familiar with jquery and I'm not sure of the adjustments I need to make. I'm also not extremely proficient in wordpress to know if I am making an obvious mistake here. I'm editing a theme and I'm trying to make the blog use a masonry layout. The theme calls the post loop from it's own php file so the blog is kind of broken up in to several php files. I hope I am including the right info.
The posts are showing up in blocks but it's just one column straight down. It seems the container is going all the way across the page on each post. I'm not sure if it's not stopping the loop or what I need to add so that each post spreads across the container width. Any help or tips on what I am doing wrong would be greatly appreciated. Thanks in advance.
I added this to my functions.
function wdv_enqueue_scripts() {
wp_enqueue_script( 'jquery-masonry' ); // adds masonry to the theme
}
add_action( 'wp_enqueue_scripts', 'wdv_enqueue_scripts' );
This to my footer.php
<script>
jQuery( document ).ready( function( $ ) {
$( '#container2' ).masonry( { columnWidth: 220 } );
} );
</script>
Here is my code for the loop.
<div id="container2">
<?php
global $ae_post_factory;
$ae_post = $ae_post_factory->get('post');
$post = $ae_post->current_post;
?>
<div class="brick">
<div class="brick_header">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<h4 class="media-heading title-blog"><?php the_title(); ?></h4>
</a>
</div>
<div class="brick_featured_image">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail (); ?>
</a>
<?php endif; ?>
</div>
<?php the_excerpt(); ?>
Read More
</div>
</div><!-- container -->
And this is the CSS
* masonry brick layout */
#container2 {
width: 100%; /* width of the entire container for the wall */
margin-bottom: 10px;
}
.brick {
width: 30%; /* width of each brick less the padding inbetween */
padding: 0px 10px 15px 10px;
background-color: #fff;
margin-top: 5px;
}
.brick_header{
border-bottom: solid 1px #1d1d1d;
padding-bottom: 10px;
padding-top: 10px;
}
.brick_header a{
color: #ffff00;
}
.brick_header a:hover{
color: white;
}
.brick_featured_image{
width: 100%;
margin-top: 10px;
}
.brick_featured_image img{
width: 100%;
height: auto;
}
Here you're providing Column-Width in JS 220px.
And in CSS : 30%.
That might be creating problems.
Make sure your HTML Structure Looks like this.
<div id="container2">
<div class="brick">...</div>
<div class="brick">...</div>
<div class="brick">...</div>
</div>
And CSS :
.container2{
width:100%;
}
.brick{
width:25%;
/*
This should be respective of the columns you want
Like for 4 columns, 100%/4 = 25%
*/
}
And JS.
var $container2 = $('#container2');
$container.masonry({
itemSelector: '.brick'
});
For more detailed explaination : http://masonry.desandro.com/#getting-started
To know more details on the problem you're having, kindly provide the URL so I can have a look at it and can provide exact solution.
I personally use the "js-masonry" class mainly because if you are using a framework such as Bootstrap or Modest Grid, it will keep the gutter settings, etc.
Here is an example:
<div class="js-masonry">
<?php if ( have_posts() ): ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="dt-6 tl-6 tp-6">
<article>
<h2>
<a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark">
<i class="fa <?php echo strtolower(str_replace(" ", "-", get_field('project_type'))); ?>"></i>
<?php the_title(); ?>
</a>
</h2>
<?php the_excerpt(); ?>
<div class="download">
<a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark">
View <?php the_title(); ?>
</a>
</div>
</article>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
Notice that the <div class="js-masonry"> is outside of the while.

arcticle resize automatically by screen size

i have a menu in the left and i want the site-content resize by changing the screen size. actually like in https://myspace.com/
what i have now:
the menu in the left side. this is the code:
.main-navigation {
background-color: #000000;
border-left: 1px solid #cccccc;
display: block;
float: left;
font-family: "Open Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: normal;
max-width: 50%;
position: absolute;
top: 85px;
width: 150px;
height: auto;
text-align: right;
text-transform: capitalize;
}
the site-content i putted margin-left 150px to put the content after the menu because if not, the content is under the menu. :
.blog .site-content,
.archive .site-content,
.search .site-content {
margin: 0 auto;
max-width: 885px;
position: relative;
left: 0px;
margin-left: 150px;
}
and the article that inside:
.blog .site-content .hentry,
.archive .site-content .hentry,
.search .site-content .hentry {
float: left;
margin: 0;
overflow: hidden;
width: 295px;
height: 295px;
}
the main-index is:
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', 'home' );
?>
<?php endwhile; ?>
<?php pictorico_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
the nav(menu) is in the header so the header:
<?php wp_head(); ?>
>
<header id="masthead" class="site-header" role="banner">
<div class="site-header-inner">
<div class="site-branding">
<h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
</div>
<nav id="site-navigation" class="main-navigation" role="navigation">
<h1 class="menu-toggle"><span class="screen-reader-text"><?php _e( 'Menu', 'pictorico' ); ?></span></h1>
<a class="skip-link screen-reader-text" href="#content"><?php _e( 'Skip to content', 'pictorico' ); ?></a>
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</nav><!-- #site-navigation -->
<div class="header-search">
<?php get_search_form(); ?>
</div>
</div>
</header><!-- #masthead -->
<?php if ( is_home() && pictorico_has_featured_posts( 1 ) ) : ?>
<?php get_template_part( 'content', 'featured' ); ?>
<?php elseif ( get_header_image() && ( is_home() || is_archive() || is_search() ) ) : ?>
<div class="hentry has-thumbnail">
<div class="entry-header">
<div class="header-image" style="background-image: url(<?php header_image(); ?>)">
<span class="screen-reader-text"><?php bloginfo( 'name' ); ?></span>
</div>
</div>
</div>
<?php endif; ?>
<div id="content" class="site-content">
It's a bit late to implement now, but next time look at something like Bootstrap or Zurb Foundation for frameworks that make it really fast to build responsive web apps. They take away the need for complicating things with media queries. Just a thought
Your content is not going to re-size because you have given it a fixed width (295px). Since you floated them, they should just re-arrange themselves based on how much room there is in site-content.
If you actually want the articles to re-size, give them percentage widths. Then, as site-content changes width, they will also.
If you want to keep the aspect ratio (height/width), then you need a wrapper that maintains the aspect ratio using % padding and put the content in it using postion: absolute. See, for example, Maintain the aspect ratio of a div with CSS.

How do I display four category posts that align next to each other? - Wordpress

I'm trying to pull the 4 most recent posts from category "3" to be displayed within a 1000px div, but have each single post display in a 280px div and have them displayed inline.
Right now, my code pulls the recent posts, but only styles the one most recent post.
Here is the current URL: http://dylanpolniak.com/clients/c&c/
Here is my code:
<div id="music">
<h1>Music</h1>
<div class="hr_dark"></div>
<div id="posts">
<?php query_posts('showposts=4&cat=3'); ?>
<div class="single_post">
<?php the_content(); if (have_posts()) : while (have_posts()) : the_post(); if ( has_post_thumbnail() ) {
// check if the post has a Post Thumbnail (featured image) assigned to it.
the_post_thumbnail(); the_title('<h4>', '</h4>');
} ?>
</div>
<?php endwhile; else : endif; ?>
<div class="hr_dark"></div>
<h2>View All</h2>
<style>
#posts { width: 1000px; margin: auto; }
.single_post { width: 280px; height: 400px; position: relative; display: inline-block; float: left; }
h4 { font-size: 20px; font-family: 'Satisfy', cursive; color: #202e31; text-align: center; font-weight: 400; }
</style>
Try putting you class div inside of the while loop, and you have you#posts div unclosed.
Change your code like this:
<div id="posts">
<?php query_posts('showposts=4&cat=3');
the_content(); if (have_posts()) : while (have_posts()) : the_post();
if(has_post_thumbnail() ) { ?>
<div class="single_post"> <?php
// check if the post has a Post Thumbnail (featured image) assigned to it.
the_post_thumbnail(); the_title('<h4>', '</h4>'); ?>
</div>
<?php
} ?>
</div> //add this closing div tag!

How do I align two objects vertically that aren't in the same div

I have a wordpress page with a login widget <div id='widget-sidebar' class='widgets_on_page'> above the forum content. I would like to float it to the right and have the forum title to align vertically on the left, like this:
The problem I am having is that the forum title is generated dynamically and so I can't place them in the same div.
Any ideas?
Here is the source code:
<div id="primary">
<div id="content" role="main">
<div id='widget-sidebar' class='widgets_on_page'>
<ul><li id="wp_sidebarlogin-2" class="widget widget_wp_sidebarlogin"><h2 class="widgettitle">Welcome Admin</h2><div class="avatar_container"><img src="http://localhost/wordpress/wp-content/plugins/user-avatar/user-avatar-pic.php?src=http://localhost/wordpress/wp-content/uploads/avatars/1/1344255249-bpfull.jpg&w=38&id=1&random=1344255249" alt="" class=" avatar avatar-38 photo user-1-avatar" width="38" height="38" /></div><ul class="pagenav"><li class="page_item">Dashboard</li><li class="page_item">Profile</li><li class="page_item">Logout</li></ul></li></ul>
</div><!-- widgets_on_page -->
<article id="post-2901" class="post-2901 forum type-forum status-publish hentry">
<header class="entry-header">
<h1 class="entry-title">NA Forum</h1>
</header><!-- .entry-header -->
Here is the part of my forum page template that I'm working with:
<div id="primary">
<div id="content" role="main">
<?php widgets_on_template("widget-sidebar"); ?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
Using absolute positioning for the widget should work. Something like:
div#content {
position: relative;
}
div#widget-sidebar {
position: absolute;
top: 10px;
right: 10px;
}
If you don't care for IE7 you can align container vertically using display: table-cell;. As this is rendered as a table, it looses the ability to float. So you have to set a width explicitly on at least one of the elements.
CSS
div {
display: table-cell;
vertical-align: middle;
}
div:first-child {
width: 80%;
}
HTML
<div>
first line<br>
second line<br>
last line
</div>
<div>
just one line<br>
</div>
Fiddle
http://jsfiddle.net/y96hb/

Categories