Get all post in Wordpress - wordpress

I am building a custom wordpress theme. I want to get my posts on my frontpage
I added this in my front-page.php
<?php
if ( have_posts() ) {
// Start the Loop.
while ( have_posts() ) {
the_post();
/*
* 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( 'template-parts/content', 'archive' );
}
}
?>
content-archive.php
I have this
<div class="container">
<article class="post">
<div class="post-preview col-10 no-gutter">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<p class="meta">
<?php the_modified_author();?> in <?php the_category();?> <i class="link-spacer"></i> <i class="fa fa-bookmark"></i> 23 minute read
</p>
</div>
<div class=" col-2 no-gutter">
<img src="img/profile-1.jpg" class="user-icon" alt="user-image">
</div>
</article>
When I visited the current homepage, I get the homepage as post and its snipper, then read more button.
What I am trying to pull are the posts.

Update content-archive.php to:
<div class="container">
<article class="post">
<div class="post-preview col-10 no-gutter">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<p class="meta">
<?php the_modified_author();?> in <?php the_category();?> <i class="link-spacer"></i> <i class="fa fa-bookmark"></i> 23 minute read
</p>
</div>
<div class=" col-2 no-gutter">
<img src="img/profile-1.jpg" class="user-icon" alt="user-image">
</div>
</article>

Related

Wordpress hierarchy issue with categorized post not using specific template

I'm trying to implement a dedicated template for any post with a category slug of 'rentals'. There are posts in the database with a category of rentals set up and applied. I've created the template with a standard loop and called it category-rentals.php, saving it in my root folder.
It's just not working. It keeps reverting to single post template (index.php specifically).
My rendered body classes are as such:
post-template-default single single-post postid-278 single-format-standard
Am I missing a step here?
category-rentals.php:
/*
* Rental property template
*/
?>
<?php get_header(); ?>
<div class="jumbotron jumbotron-fluid" style="background-image: url('<?php bloginfo( 'template_url' ); ?>/img/bg-semi-trans.png'), url('<?php bloginfo( 'template_url' ); ?>/img/bg-jumbotron.jpg');">
<div class="container">
<div class="headline text-center animated fadeInUp">
<p><?php wp_title(''); ?></p>
</div>
</div>
</div>
<section>
<div class="container">
<div class="row">
<div class="col-12">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no content matched your criteria.' ); ?></p>
<?php endif; ?>
<hr>
← Go Back | Contact Us
</div>
</div>
</div>
</section>
<?php get_footer(); ?>

Display WordPress posts in grid using bootstrap not aligned properly

Blog Posts with Grid Layout even number columns doesn't display correctly .The problem occurs with 3 column layout also: for each group of 3 posts
<div class="main" role="main">
<div id="content" class="content full">
<div class="container">
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post();
$title = get_the_title();?>
<div class="row">
<?php if ( has_post_thumbnail() ) { ?>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
<a href="<?php echo esc_url(get_permalink(get_the_ID())) ?><?php
the_post_thumbnail('imic_600x400'); ?></a>
<?php } ?>
<?php if ( has_post_thumbnail() ) { ?><?php } ?>
<h3><a href=" <?php echo esc_url(get_permalink(get_the_ID())); ?>
<?php
echo esc_attr($title); ?>
</a>
</h3>
</div>
<?php endwhile; ?>
<!-- Pagination -->
<?php if(function_exists('imic_pagination')) { imic_pagination(); }
else { next_posts_link( 'Older Entries');
previous_posts_link( 'Newer Entries' ); } ?>
<?php endif;?>
</div>
<?php if(is_active_sidebar($pageSidebar)) { ?>
<!-- Sidebar -->
<div class="sidebar" id="sidebar-col">
<?php dynamic_sidebar($pageSidebar); ?>
</div>
<?php } ?>
</div>
</div>
</div>
Try changing the created HTML structure as follows (in Inspect Element view to verify it works):
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
</div><!--
--><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
</div><!--
--><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
</div>
(note the <!-- --> HTML comments)
If that solves the problem, try adding the HTML comments into your PHP code.
The reason for this behaviour is the resulting new line being interpreted as a "space" and thus the third item won't fit into the same row because of the added horizontal space.
You could also output the HTML-<div class="row">...</div> without line-breaks, eg. by concatenating the markup as one PHP string.

wordpress development how to make theme options for home page

I'm trying to figure out a way to make a dynamic home page on wordpress, i want the ability to edit the front page, and add content without hardcoding it.
Basically i want to have my home page to look similar to this.
https://html5up.net/forty
and i am aware that i need theme options to do this, how would i integrate that in my wordpress theme.
I want to be able to make columns, add rows, add hero image, etc.
and no, i dont want to use the visual composer. I am currently using Advanced custom fields.
Here is current front page on my theme, i dont want to use advanced custom fields because i still have to hard code.
In a nutshell how to make theme options for wordpress theme
template-frontpage.php
<?php
/**
* Template Name: Front Page
*
* #package Eli
*/
$image = get_field('hero_image');
get_header();
?>
<div class="row">
<div class="hero" style="background-image:url(<?php echo $image;?>); width:100%; min-height:350px; background-size: cover;">
<div class="container">
<div class="col-md-12">
<header class="hero-text">
<?php if (get_field('hero_title') ):?>
<h1 style="color:#fff;"><?php the_field('hero_title'); ?></h1>
<?php endif;?>
<?php if (get_field('hero_span') ):?>
<span><?php the_field('hero_span'); ?></span>
<?php endif;?>
<?php if (get_field('hero_span_2') ):?>
<span id="move"><?php the_field('hero_span_2'); ?></span>
<?php endif;?>
</header>
</div>
</div>
</div>
</div>
<section class="section-home">
<div class="row">
<div class="container">
<div class="line"></div>
<?php if (get_field('content_block_left') ):?>
<div id="cbl" class="col-md-4 col-xs-12">
<?php the_field('content_block_left_icon'); ?>
<?php the_field('content_block_left'); ?>
</div>
<?php endif;?>
<?php if (get_field('content_block_left2') ):?>
<div id="cbl" class="col-md-4 col-xs-12 ">
<?php the_field('content_block_left_2_icon'); ?>
<?php the_field('content_block_left2'); ?>
</div>
<?php endif;?>
<?php if (get_field('content_block_left3') ):?>
<div id="cbl" class="col-md-4 col-xs-12">
<?php the_field('content_block_left_3_icon'); ?>
<?php the_field('content_block_left3'); ?>
</div>
<?php endif;?>
</div>
</div>
</section>
<div class="section-about">
<div class="row">
<h1>Beat Your Rivals</h1>
<div class="line"></div>
<div class="container">
<?php if (get_field('image_left') ):?>
<div id="cbl2" class="col-md-6 offset-md-3 col-xs-12">
<img src="<?php echo the_field('image_left'); ?>" width:"400px" height:"300px">
</div>
<?php endif;?>
<?php if (get_field('caption_text') ):?>
<div id="cbl2" class="col-md-6 offset-md-3 col-xs-12">
<?php the_field('caption_text'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<?php
$image2 = get_field('test_image');
?>
<div class="section-test" style="background-image:url(<?php echo $image2['url'];?>); width:100%; min-height:300px; background-size: cover;">
<div class="row">
<div class="container">
<?php if (get_field('test_text') ):?>
<div id="cbl3" class="col-md-12 col-xs-12">
<?php the_field('test_text'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<div class="about-us">
<div class="row">
<div class="container">
<?php if (get_field('about_us') ):?>
<div class="col-md-12 col-xs-12">
<?php the_field('about_us'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<?php
$image3 = get_field('cons_image');
?>
<div class="consult">
<div class="row">
<div class="my-block-left" style="background-image:url(<?php echo $image3['url'];?>); background-size: cover;" >
<div class="container">
<?php if (get_field('consult_us') ):?>
<div class="col-md-12 col-xs-12">
<?php the_field('consult_us'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
</div>
<?php if (get_field('contact_us') ):?>
<div class="contact-us">
<div class="row">
<div class="container">
<h1 class="contact-h1">Contact Us</h1>
<div class="line"></div>
<div class="col-md-12 col-xs-12">
<?php the_field('contact_us'); ?>
</div>
</div>
</div>
</div>
<?php endif;?>
<?php get_footer(); ?>
Ok, It seems you have already done the basic theme options. Now if you want to create real theme options like the ones everyone creates in their themes then you have three cool options. WP Theme Customizer API , Redux and Codestars
You need to study them. Anyone of the above you can use in your project.

Nested repeater field ACF - Showing content from field one

I have a nested repeater field.
The nested part is a modal window, which when open expands on the second repeater field. But when everything is filled out the modal window displays the info from the first item in the loop.
This is how it should be working:
http://pagedev.co.uk/hoppings/product-sector.php#
Here is my code:
<div class="container">
<?php while( have_rows('product_sections') ): the_row(); ?>
<div class="sector-heading">
<img src="<?php the_sub_field('section_logo'); ?>">
<div><?php the_sub_field('section_intro'); ?></div>
</div>
<?php if( have_rows('products') ): ?>
<div class="grid-wrapper">
<?php while( have_rows('products') ): the_row(); ?>
<!--Start Single Product-->
<div class="grid-item">
<div class="image-hovers">
<img src="<?php the_sub_field('thumbnail'); ?>">
<a class="js-open-modal" href="#" data-modal-id="popup"><div class="product-hover"></div></a>
</div>
<div class="grid-title">
<h2><?php the_sub_field('product_name'); ?></h2>
</div>
</div>
<!--Start Single Product Modal-->
<div id="popup" class="modal-box">
×
<div class="modal-wrap">
<div class="modal-img">
<img src="<?php the_sub_field('thumbnail'); ?>">
</div>
<div class="modal-content">
<h2><?php the_sub_field('product_name'); ?></h2>
<p><strong><?php the_sub_field('product_size'); ?></strong></p>
<hr>
<?php the_sub_field('product_description'); ?>
<div class="modal-stockist">Find a Stockist</div>
<div class="modal-literature">Literature</div>
</div>
</div>
</div>
<!--Close Single Product Modal-->
<?php endwhile; // end of the loop. ?>
</div>
<!--End Grid Wrapper-->
<?php endif; ?>
<!-- Close product repeater field -->
<?php endwhile; // end of the loop. ?>
</div>
<!--End Container-->
Any help would be great!
Lee
I figured it out. Something so simple!
My modal window needed a unique ID for each product!
All solved :)

Custom template for category WordPress

Hello I'm new in WordPress custom templates. Can anybody tell me how I can load a different template for a category.
Here is the index.php file
<?php
/**
* The main template file
*
* No comment for now
*
* #package WordPress
* #subpackage site
* #since site
*/
get_header(); ?>
<?php
$strFr='Featured';
$strFinancial='Financial Reports';
$strResearch='Research';
?>
<div id="page-body" role="main">
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 SearchTop">
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" class="form-control customsearchfiled" placeholder="Search of anything related to Site" />
</div>
</form>
</div><!-- SearchTop -->
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-12 col-sm-12 col-xs-12 mb20 no-gutter">
<div class="col-md-12 col-sm-12 col-xs-12 no-gutter">
<?php if (have_posts()) : ?>
<?php /* Start the Loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title mt20"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<!--<div class="entry-content">
<p><?php //printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div> .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
As far I know the index file is calling content php file that shows the post layout but don't know how to do this. Tried a lot of things but only get php error. I don't know php syntax much
<?php
/**
* Custom template with better structure than WP default.
*
* Used for both single and index/archive/search.
*
* #package WordPress
* #subpackage site
* #since Site 1.0
*/
# for one post
if (is_single()) {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if(function_exists('pf_show_link')){echo pf_show_link();} ?>
<p class="text-center topcategory">
<?php foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
</p>
<h1 class="entry-title"><?php the_title(); ?></h1>
<p class="postmetadown">By :<span class="bluecolor"><?php the_author(); ?></span> | <span><?php the_date() ?></span></p>
</header>
<div class="entry-content col-md-9 col-sm-9 col-xs-12">
<?php the_content(__('Continue reading <span class="meta-nav">→</span>', 'twentytwelve')); ?>
<?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
</div>
<div class="col-md-3 col-sm-3 col-xs-12">
<?php get_sidebar(); ?>
</div>
<?php if (comments_open()) : ?>
<!--<div id="disqus_thread"></div>

<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'site'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type =
'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')
[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/? ref_noscript">comments powered
by Disqus.</a></noscript>
comments powered by <span class="logo- disqus">Disqus</span>-->
<?php endif; ?>
<footer class="entry-meta col-md-12">
<?php if ($googlePlusAcc = get_the_author_meta('googleplus')) { ?>
<?php hr_single_entry_meta(); ?>
<?php } else { ?>
<?php twentytwelve_entry_meta(); ?>
<?php } ?>
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
<?php if (is_singular() && get_the_author_meta('description') && is_multi_author()) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries. ?>
<div class="author-info">
<div class="author-avatar">
<?php
/** This filter is documented in author.php */
$author_bio_avatar_size = apply_filters('twentytwelve_author_bio_avatar_size', 68);
echo get_avatar(get_the_author_meta('user_email'), $author_bio_avatar_size);
?>
</div>
<!-- .author-avatar -->
<div class="author-description">
<h2><?php printf(__('About %s', 'twentytwelve'), get_the_author()); ?></h2>
<p><?php the_author_meta('description'); ?></p>
<div class="author-link">
<a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"
rel="author">
<?php printf(__('View all posts by %s <span class="meta-nav">→</span>', 'twentytwelve'), get_the_author()); ?>
</a>
</div>
</div>
</div>
<?php endif; ?>
</footer>
</article>
<?php
#for listing page
} else {
$category = get_the_category();
if (!empty($category))
$category = $category[0];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php //if (is_sticky() && is_home() && !is_paged()) : ?>
<!--<div class="featured-post">
<?php //_e('Featured post', 'twentytwelve'); ?>
</div>-->
<?php //endif; ?>
<div class="col-md-4 col-sm-4 col-xs-12 categoryimg no-gutter">
<?php if (has_post_thumbnail()) { ?>
<div class="post-thumbnal-wrapper-multiple">
<?php the_post_thumbnail(); ?>
</div>
<?php } ?>
</div>
<div class="col-md-8 col-sm-8 col-xs-12 categoryContent">
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header>
<?php if (is_search()) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<?php else : ?>
<div class="entry-content">
<?php the_excerpt_more(); ?>
</div>
<?php endif; ?>
<div class="col-md-12">
<footer class="entry-meta">
<?php twentytwelve_entry_meta(); ?>
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
</footer>
</div>
</div><!-- categoryContent -->
</article>
<div class="clearfix"></div>
<?php
}
If you want a template for the category page, see https://codex.wordpress.org/Category_Templates. If you want a different template for the content, based on the category, you could use (in your content.php)
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
If there's a lot of difference between categories, I would place this in index.php, and create different templates for every category
Check out this page Template Hierachy. Basically if you want to target a specific category you create a file like so category-{slug}.php {slug} being the category slug you want to target.
I suggest you to read about Wordpress Template Hierarchy in https://developer.wordpress.org/themes/basics/template-hierarchy/
To answer your question, you need to create a category-{id}.php or category-{slug}.php file. in your theme folder (Choose only one)
id = your category ID
slug = your category slug
After creating the file, then you can put your custom code if you want.
Add your code inside archive.php which will list all the posts in the category and add the detail page template inside single.php .
https://codex.wordpress.org/Category_Templates

Resources