WordPress Blog, only use archive for current page? - php

I am building a WP site, which will have a News and Blog.
They will be in separate pages, One for news and one for Blog, which will be separated by categories.
So for example, I have this code on 'News', which stops the loop getting posts:
<?php
$uncat = get_cat_ID('uncategorised');
$uncat2 = get_cat_ID('blog');
$args = array(
'posts_per_page' => 3,
'category__not_in' => array($uncat, $uncat2)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h4><?php the_title(); ?></h3>
<span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
This works fine, but I also have 'Archives' on the right hand side, which filters by date posted. The issue is, this will get posts from News AND blog, which defeats the idea of splitting them up.
Is there a way to split these up, so if the user clicks 'March 2015' on the archive, it will only get the posts from this month from NEWS?
Here is my current code for Archive.php
<?php if (have_posts()) : ?>
<!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->
<!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-12 col-sm-12 col-xs-12">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<h5 class="captext"><?php the_title(); ?></h5>
<span class="entry-date-orange"><?php echo get_the_date(); ?></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Try this , it will echo list
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
full example code
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php get_search_form(); ?>
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Related

Show a list of posts in category page Wordpress

So basically I'm trying to show the list of posts of a category page. I need to do it dynamically, so each category page has it own posts list. I ended up with this code, but it shows all post from all categories.
<?php
$lastposts = get_posts(array(
'posts_per_page' => 5
));
if ($lastposts)
{
foreach ($lastposts as $post):
setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php
endforeach;
wp_reset_postdata();
}
?>
I just found a solution
<div class="entry-content">
<div class=" my-5">
<h2><a href="<?php the_permalink(); ?>" class="section-heading__title" rel="noopener">
<?php the_title(); ?>
</a></h2>
<div>
<div class="attachment__description my-2"><?php echo get_the_excerpt(); ?></div>
<hr/>
<span class="uppercase">Published on <?php echo get_the_date(); ?></span>
<span><?php echo site_posted_by();?></span>
</div>
</div>
</div><!-- .entry-content -->

repeated loop in wordpress blog page how to solve this?

how to list only 10 post in one page and solve pagination, i found repeated loop in this.
i want to apply css in comment also . how can i separate comments each part and can give individual css?
<?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>
<div id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<?php the_post_thumbnail( array(200,220),'thumbnail', array( 'class' => 'alignleft' ) ); ?>
<h2><?php the_title(); ?></h2>
<span class="meta"><?php //author_profile_avatar_link(48); ?> <strong><?php the_time('F jS, Y'); ?></strong> / <strong><?php the_author_link(); ?></strong> / <span class="comments"><?php comments_popup_link(__('0 comments','twentythirteen'),__('1 comment','twentythirteen'),__('% comments','twentythirteen')); ?></span></span>
<?php the_excerpt(__('Continue reading »','twentythirteen')); ?>
</div><!-- /#post-<?php get_the_ID(); ?> -->
<?php endwhile; ?>
<div class="navigation">
<span class="newer"><?php previous_posts_link(__('« Newer','twentythirteen')) ?></span> <span class="older"><?php next_posts_link(__('Older »','twentythirteen')) ?></span>
</div><!-- /.navigation -->
<?php else: ?>
<div id="post-404" class="noposts">
<p><?php _e('None found.','twentythirteen'); ?></p>
</div><!-- /#post-404 -->
<?php endif; wp_reset_query(); ?>
</div><!-- /#content -->

Why isn't my WP Query showing posts from a specified category?

I'm working on a Wordpress site. I'm trying to make a category with the id 968 show up on the Wordpress site under the stories and successes div. However for some reason the stories and successes div is not having the content from the category inserted into it. Any information on how I can improve my query or PHP is much appreciated. If I need to update this post with more information please let me know. Below you will see the php template file I am using.
<?php
/**
* Template name: Random Template
*
*
*/
// Custom styles
wp_enqueue_style('stories-styles', get_bloginfo('stylesheet_directory') . '/assets/css/stories.css');
// TEMP -- Get the category from the slug
the_post();
$categorySlugs = array(
'religious-freedom' => 'religious-freedom',
'social-and-economic-justice' => 'social-and-economic-justice',
'civil-and-human-rights' => 'civil-and-human-rights'
);
// Get category
$categorySlugs = $categorySlugs[$post->post_name];
$storyQuery = new WP_Query(array(
'post_type' => array('stories', 'press-releases'),
'category_name' => $categorySlugs,
'posts_per_page' => 4,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
$successesQuery = new WP_Query(array(
'post_type' => 'stories',
'cat' => 968,
'posts_per_page' => 1,
'offset' => 5
));
// Remove yarpp
remove_filter('the_content', array($yarpp, 'the_content'), 1200);
?>
<?php get_header(); ?>
<div class="category-title">
<div class="container">
<h2><?php the_title() ?></h2>
</div>
</div>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
<article class="featured-issue-article">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('full', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
</div>
<?php endif; ?>
<div class="content">
<?php the_content() ?>
</div>
</article>
</div>
</div>
<hr class="large blue" />
<div class="row">
<div class="col-md-8">
<ul class="section-list">
<?php while ($storyQuery->have_posts()) : $storyQuery->the_post(); ?>
<li>
<h3><?php the_title() ?></h3>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
<?php endif; ?>
<?php if( get_field('custom_excerpt', $values_id)): ?>
<p><?php echo the_field('custom_excerpt'); ?></p>
<?php endif; ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
<?php
set_query_var('category_slug', $categorySlug);
get_template_part('issues-nav', 'bottom')
?>
<hr class="small blue" />
<div class="success-stories">
<h2>Stories & Successes</h2>
<ul>
<?php while ($successesQuery->have_posts()) : $successesQuery->the_post(); ?>
<li>
<h4>
<?php the_title() ?>
</h4>
<?= strip_tags($post->post_excerpt) ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

Removing a tag from custom post type loop

I am currently using this code below to display all the pages in my category:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'testclass')); ?> </div>
<div class="col-md-6 col-sm-6 col-xs-12 categorytiletext2">
<div class="testdiv">
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<!-- If there is no posts, display an error message -->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->
This works really well, but I have now added tags to these pages. What I need to do, is remove certain tags from the loop. For example, my tag is called maintag.
I have tried to add the following:
<?php $my_query = new WP_Query(array('tag__not_in'=>array('maintag'), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC')); ?>
but this didn't work.
Does this need to be done by ID's?
EDIT:
New Code, still not working??
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $tag_object = get_term_by('slug', 'sector1', 'post_tag');
$tagID = $tag_object->term_id;
$my_query = new WP_Query(array('tag__not_in'=> array($tagID), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC')); ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'testclass')); ?> </div>
<div class="col-md-6 col-sm-6 col-xs-12 categorytiletext2">
<div class="testdiv">
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<!-- If there is no posts, display an error message -->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->
if maintag is slug then use 'slug' in first parameter if it is name then use 'name' instead of slug
link: get_term_by()
$tag_object = get_term_by('slug', 'maintag', 'post_tag');
$tagID = $tag_object->term_id;
$my_query = new WP_Query(array('tag__not_in'=> array($tagID), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC'));

Working with external loop in two different situations in same site (WP)

In my theme I have a blog page and a homepage (without blog loop). Now I would like to add some blog posts on my homepage (like most recent).
I am using the blog page as a template for the homepage ( I copied and renamed the original), wich works good. But I can't make any changes to it without changing the original blog page (the loop is called in an external php).
I Have no idea were to start.
This is the piece on the homepage calling the blog loop:
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<?php if($gallery_select && $gallery_position == 1) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
<?php if($quickfinder_position == 2) : ?>
<?php codeus_quickfinder($quickfinder_select); ?>
<?php endif; ?>
<?php if($portfolio_position == 2) : ?>
<?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
<?php endif; ?>
<?php if($gallery_select && $gallery_position == 3) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->
And this is the piece in the external php called blog.php
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"> </div>
<img src=" <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
<?php codeus_pagination($page_num, $pages_count); ?>
</div>
<?php endif; ?>
Remember, I don't want to make changes in the original blog.php, because the normal blog page needs to work as it is now. I would like to make the blog work on the homepage also, but just with a maximum of 3 posts showing, perhaps of a certain category.
Help would be so welcome!!
I think I found a solution, I edited the homepage template like this:
<!-- wrap start --><div class="content-wrap">
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php
$page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
$items_per_page = 2;
$blog_loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => $items_per_page,
'paged' => $page_num
));
$pages_count = ceil($blog_loop->found_posts/$items_per_page);
global $post;
$portfolio_posttemp = $post;
?>
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"></div>
<img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->

Categories