I'm currently developing my own wordpress theme and so far so good. The problem is I'm using a permalink and a category. The category is inside the permalink. The following code is what I'm using (content.php):
<?php if ( has_tag ('half_half') ) : ?>
<div class="gridHalves">
<article class="project" >
<a class="projectLink" href="<?php the_permalink(); ?>">
<div class="projectwrapper">
<div class="projectOverlayInset">
<div class="projectOverlay">
<div class="projectInfo">
<h4 class="categorie"><?php the_category(', '); ?></h4>
<h2 class="onderwerp"><?php the_title(); ?></h2>
<p class="writer">Written by <strong><?php the_author(); ?></strong></p>
</div>
</div>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</div>
</a>
</article>
</div>
<?php endif; ?>
The following is happening when I leave the_category in there:
If i leave the_category tag out everything is working fine. I've already checked the function in wp-includes of the_category but couldn't find anything about the permalink. So any suggestions? My loop is the following:
<?php get_header(); ?>
<main id="projectengrid" class="gridmenu-closed">
<section class="gridRightSmall">
<?php if ( have_posts() ) : ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</section>
</main><!-- .site-main -->
<?php get_footer(); ?>
I left the post navigation in there because I want to use it later on.
Hope someone knows the solution to this! Seems like I'm having some nested loops in there!
Kind regards,
Wouter
If you need any additional info please let me know!
Related
Pagination template part includes common pagination function with style. The template part works for archive.php (it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the ‘services’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
First of all, you don't need to include that template inside while loop. That's wrong.
Then, if you want to have an archive page for your team post type, you need to provide 'has_archive' => true within register_post_type() function args.
Also, consider changing archive page default slug if needed. If you do so, you need to open Settings > Permalinks for resetting your permalinks structure.
Then you could either use standard archive.php for the whole team archive page and standard template-parts/content.php for one post inside the loop or rewrite either of those by creating archive-team.php or content-team.php. And the_posts_pagination() function will work on proper archive page (either archive.php or archive-team.php).
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
);
Put your pagination template outside the loop :
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
I'm using stanleywp theme for WordPress to display some post, actually I've inserted 20 posts, but it shows only 10posts.
This is what I did:
Portfolio -> Portfolio categories -> I added "brands" category
Portfolio -> add new -> and I've added 20 posts and set "brands" category
when I go to .../portfolio-category/brands/ I should see all my 20 posts, but I see only 10.
How can I solve that?
This is taxonomy-portfolio_cats.php:
<?php
/**
* #package WordPress
*/
?>
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<div class="container pt">
<div class="row mt">
<div class="col-lg-6 col-lg-offset-3 centered">
<h1><?php echo single_term_title(); ?></h1>
<hr>
<?php if(category_description()) { ?>
<?php echo category_description( ); ?>
<?php } ?>
</div>
</div>
<div class="row mt centered">
<?php $cont = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-lg-4">
<?php if ( has_post_thumbnail()) : ?>
<a class="zoom green" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<?php if(bi_get_data('project_title', '5')) {?>
<p><?php the_title(); ?></p>
<?php } ?>
</div> <!-- /col -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
I think it's due to the default pagination (10 posts per page): you could increase the value on Settings > Reading > Blog pages show at most, or insert the pagination after <?php wp_reset_query(); ?>, something like:
<?php
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'your-textdomain' ),
'next_text' => __( 'Next page', 'your-textdomain' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'your-textdomain' ) . ' </span>',
) );
?>
Or if you want to change the number of posts per page only for that taxonomy archive you can use the pre_get_posts action:
function portfolio_cats_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( is_tax( 'portfolio_cats' ) ) {
// Display all posts in one page
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'portfolio_cats_posts_per_page', 1 );
In my wordpress I have many posts, every page I am showing 5 posts.
Bottom of my page I have next and prev button. When I click one the next button it will go to /page/2/ link but this page title is showing Page not found. And it's not showing other posts in page 2.
My next and prev code :
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
My index.php code :
<div class="center-content">
<ul>
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else {
?>
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
You do have a few issues here
NEVER EVER use query_posts. It is slow, reruns queries, breaks and fails silently with pagination and worse of all, it breaks the main query object. If you break the main query object, you break page functions. So, please never use query_posts. Mke if it never existed
You have replaced the main query loop with a custom query, which you must not do. If you need to show a different amount of posts on a particular page ( not on page templates and a static front page though as this will not work there ), then use pre_get_posts. You need to go and read how helpful that action is and how to use it
Remove this line
<?php query_posts('posts_per_page=5'); ?>
Then add the following in your functions file
add_action( 'pre_get_posts', function ( $query )
{
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
});
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
Just add below code: I added 'paged' variable and set in next_posts_link() and previous_posts_link()..please see below code:
<div class="center-content">
<ul>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
global $wp_query;
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else { echo 'test';
?>
<div class="prev-next-btn">
<?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<div class="prev-next-btn">
<?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
I'm creating a custom theme and in the single.php file, i have the main loop, however if i navigate to different posts, all that shows up is the latest post. I could go to the different permalinks, but the only post that shows up is the latest one. How do I make it so that the correct post shows up?
I tried changing the permalinks and deleting and adding pages, but I get the same result.
Here is single.php
<?php while(have_posts()): the_post();?>
<section class="main-section">
<header>
<h1 class="section-header"><?php the_category('/');?></h1>
<ul class="section-nav">
<?php
$cat = get_the_category()[0];
$categoryPosts = get_posts(array('category' => $cat->term_id));
foreach($categoryPosts as $post): setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
</header>
<h2 class="section-blurb"><?php the_title();?></h2>
<div class="info">
<?php the_content(); ?>
</div>
</section>
<?php endwhile;?>
Everything works..but only for the latest post. How do I make it so that the appropriate post is shown?
you can use orderby for sorting order
'orderby'
$categoryPosts = get_posts(array('category' => $cat->term_id, 'orderby' => 'post_date', 'order' => ASC));
For reference see this link
http://codex.wordpress.org/Template_Tags/get_posts
Hi it sounds like a simple fix. Is this a child theme. Try saving the current single.php file and replace it with a new single.php file use a wp theme the following is from wp 2012 single.php markup
<?php
/**
* The Template for displaying all single posts
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<nav class="nav-single">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
</nav><!-- .nav-single -->
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I've wrote this code to display a box with pageviews which is inside of the arrow button on the right side of every title, but the thing is, the counter is not counting pageviews on pages and I don't know why.
I added this code in index.php
I tried to put my code for pageviews inside of <?php is_singular( $post_types ); ?> like this:
<?php is_singular(
<div class="square">
<div class="pageviews-icon"></div>
<div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
</div>
); ?>
but is not working, the site crashed. Can anybody help me with this?
Here's the code above this as well, maybe it could help and I could wrap all together
<div class="frame">
<!-- uses the post format -->
<?php {
if(!get_post_format()) {
get_template_part('format', 'standard');
} else {
get_template_part('format', get_post_format());
};
?>
<div class="square">
<div class="pageviews-icon"></div><div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
</div>
</div>
The website: The link for the website
Here's the pageview code too:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 Unique Views";
}
return $count.' Unique Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
You cannot put HTML in PHP. You can put STRINGS in PHP, but they need to be put in quotes.
if(is_single() || is_page() || is_attachment()) {
?>
<div class="square">
<div class="pageviews-icon"></div>
<div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
</div>
<?php
}
As the documentation says:
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
You should do it like this:
<?php if is_singular() { ?>
<div class="square">
<div class="pageviews-icon"></div>
<div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
</div>
<?php }; ?>
Edit
You have to do some changes to your css:
div.frame{position:relative;}
div.square{display:block;}
Your view counter is functional. too see it live, please correct your php code as I mentioned above
live on your site :
You're going to want to do this in the file where you are looping through all your posts. This can be in different locations depending on your theme etc .
For the twentyfourteen theme this is in index.php and looks like this
PHP
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?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
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
..... code carries on
The last line here: get_template_part tells us that it's pulling in a template called content if we go and open content.php we can see that this is where it actually outputs things specific for each page/post. This is where you will need to do your logic.
So for example, instead of this in twentyfourteen content.php
PHP:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php twentyfourteen_post_thumbnail(); ?>
<header class="entry-header">
<?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
<div class="entry-meta">
<span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
</div>
<?php
endif;
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h1 class="entry-title">', '</h1>' );
endif;
?>
<div class="entry-meta">
<?php
if ( 'post' == get_post_type() )
twentyfourteen_posted_on();
if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
?>
<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
<?php
endif;
edit_post_link( __( 'Edit', 'twentyfourteen' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
You would do something like this
PHP:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php twentyfourteen_post_thumbnail(); ?>
<header class="entry-header">
<?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
<div class="entry-meta">
<span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
</div>
<?php
endif;
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h1 class="entry-title">', '</h1>' );
endif;
?>
<div class="square">
<div class="pageviews-icon"></div>
<div class="pageviews"><?php echo getPostViews(the_ID()); ?></div>
<div class="entry-meta">
<?php
if ( 'post' == get_post_type() )
twentyfourteen_posted_on();
if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
?>
<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
<?php
endif;
edit_post_link( __( 'Edit', 'twentyfourteen' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
and then make sure that your getPostViews is accessible in your functions.php file.
This will make sure that your postViews are being displayed. But how to count them in the first place right?!
Go back to the content.php and have a look at this part of the code
PHP:
<?php if ( is_search() ) : ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php
the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
The is_search() function says whether to display Excerpts for Search, home, category and tag pages. However we don't want to set a pageview each time the post shows up in the search or home etc, so we must only put the setPostViews call in the other part of that if i.e. when the post/page is being viewed on its own.
That'll give you code like this
PHP:
<?php if ( is_search() ) : ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<!-- Now we can add a pageview -->
<?php setPostViews(the_ID()); ?>
<div class="entry-content">
<?php
the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
Again making sure that setPostViews is accessible in your functions.php file.
This should get you what you want.
I've figured it out were the code were messing up thanks to #RobSchmuecker, I was looking for bugs on the wrong place until he tells me about other pages so then I looked into all this code to see his statement, and I discovered where the code were messing up which was on a very bottom of a code page where I set it up the php views counter which the code was:
<?php if(is_single ()) { ?>
<?php setPostViews(get_the_ID()); ?>
<?php } ?>
instead of:
<?php if(is_singular ()) { ?>
<?php setPostViews(get_the_ID()); ?>
<?php } ?>
With many many hours of digging in code and trying stuff, I completly forgot about this part of the code. Thanks to all for helping me on this