WP_Query change code - wordpress

In my theme WordPress to display the post uses this (code #1):
function inner($posts, $td_column_number = '') {
$buffy = '';
if (!empty($posts)) {
foreach ($posts as $post) {
$td_module_17 = new td_module_17($post);
$buffy .= $td_module_17->render($post);
}
}
return $buffy;
}
I need to show posts using this code (code #2):
<?php
$custom_query = new WP_Query('posts_per_page= 5');
while($custom_query->have_posts()):
$custom_query->the_post();
?>
<div class="post-1">
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h3><?php the_title(); ?></h3>
<div class="float-tags">
<div class="bb-td-post-small-box clearfix">
<?php the_tags('','',''); ?>
</div>
</div>
<div class="post-2">
<?php the_content(); ?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata(); // reset the query
?>
If I insert code #2 into module 17, then the posts are output immediately all in one block, and not one at a time.

Solved a problem. The code was looped. Here is the working code.
<?php
class td_module_17 extends td_module {
function __construct($post) {
//run the parrent constructor
parent::__construct($post);
}
function render($Post = false) {
ob_start();
?>
<div class="post-1">
<div
<?php //post_class(); ?> id="post-<?= $Post->ID; ?>">
<h3><?= $Post->post_title; ?> </h3>
<div class="float-tags">
<div class="bb-td-post-small-box clearfix">
<?php $tags = get_the_tags($Post->ID); ?>
<? foreach ($tags as $tag) : ?>
<?= $tag->name; ?>
<? endforeach; ?>
</div>
</div>
<div class="post-2">
<?= apply_filters('the_content', $Post->post_content); ?>
</div>
</div>
</div>
<?php return ob_get_clean();
}
}

Related

Why if loop is causing unexpected end of file (code attached) (Wordpress)

This code is causing
Parse error: syntax error, unexpected end of file in C:\wamp64\www\pss\wp-content\themes\pss-theme\index.php on line 18
<?php
get_header();
if (have_posts()) :
$count = 1;
while (have_posts()) : the_post();
get_template_part ('content');
get_sidebar();
get_footer();
?>
But if I move if, count and while from index.php,
<?php
get_header();
get_template_part ('content');
get_sidebar();
get_footer();
?>
to the top of the content template part like this:
<?php if (have_posts()) :
$count = 1;
while (have_posts()) : the_post(); ?>
<div class="post-content u-cf">
<?php if (has_post_thumbnail()) { ?>
<div class="post-thumbnail u-cf">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('small-thumbnail') ?>
</a>
</div>
<?php } ?>
<h2 class="post">
<?php the_title(); ?></h2>
<p class="post-info">
<i class="fa fa-folder-open" aria-hidden="true"></i>
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories) {
foreach ($categories as $category) {
$output .= '' . $category->cat_name . '' . $separator;
}
echo trim ($output, $separator);
}
?>
|
<i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_time('j/m/Y'); ?>
</p>
<?php the_content(); ?>
<hr>
<?php if ( 2 === $count ) { ?>
<div class="main-content-advert">
<p>Lorem ipsum</p><p>Lorem impsum</p>
</div>
<hr>
<?php }
$count++; ?>
</div>
<?php endwhile; ?>
<?php
else :
echo '<p> No Content found</p>';
endif;
?>
</article>
</div>
The error is not present.
Now, I understand that it has got something to do with the loop or the count?
That is completely normal, that error its expected because you are missing the end tags of the if and the while.
You cant have those in a different file, PHP expects the start and end to be in the same file.
Its not like its going to do the get_XXXX functions and create a big file with all those parts and then start with the content of them.
If you want to have the variable $count to be in the template part, move the end tags to your index, and have only the content in the content template, that is his purpose ;)
<?php
get_header();
if (have_posts()) :
$count = 1234;
while (have_posts()) : the_post();
get_template_part('template-parts/post/content');
endwhile;
else :
echo '<p> No Content found</p>';
endif;
?>
</article>
</div>
<?php
get_sidebar();
get_footer();
?>
then in content.php use global to get the variable that is not in scope:
<?php
global $count;
var_dump($count);//THIS WILL SHOW THE NUMBER 1234
?>
<div class="post-content u-cf">
<?php if (has_post_thumbnail()) { ?>
<div class="post-thumbnail u-cf">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('small-thumbnail') ?>
</a>
</div>
<?php } ?>
<h2 class="post">
<?php the_title(); ?></h2>
<p class="post-info">
<i class="fa fa-folder-open" aria-hidden="true"></i>
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories) {
foreach ($categories as $category) {
$output .= '' . $category->cat_name . '' . $separator;
}
echo trim ($output, $separator);
}
?>
|
<i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_time('j/m/Y'); ?>
</p>
<?php the_content(); ?>
<hr>
<?php if ( 2 === $count ) { ?>
<div class="main-content-advert">
<p>Lorem ipsum</p><p>Lorem impsum</p>
</div>
<hr>
<?php }
$count++;
?>
</div>
you can find more info here variable scope

How to paginate custom post type in wordpress of custom taxonomy

I want to implement pagination on custom post type of a custom taxonomy.
but it is not showing the pagination links only shows on default posts but not working. When I click on next link it redirect me to the index page.
Any help.(New to Wordpress)
Code
<?php $term = get_queried_object();
$taxonomy = get_taxonomy($term->taxonomy);
?>
<div class="panel-heading"><h4><?php echo 'محصولات : '.$term->name;?></h4></div>
<div class="panel-body">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type'=>'my_product', 'taxonomy'=>$taxonomy->name,
'posts_per_page'=> 1,
'term'=>$term->slug, 'paged'=>$paged);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()): $query->the_post(); ?>
<div class="thumbnail">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('featured');
} ?>
<div class="caption caption-content">
<h3><?php the_title(); ?></h3>
<p class="text-muted">
<!-- <strong>نویسنده:</strong> <?php //the_author();?> -->
تاریخ: <?php the_date(); ?></p>
<p> <?php the_excerpt(); ?> </p>
<div>
<p class="price-box">
<i class="fa fa-circle"> </i>قیمت:
<?php if (the_field('price') == '') {
// echo "00.00";
} else {
the_field('price');
} ?>
</p>
</div>
</div>
<hr>
</div>
<?php endwhile;
}
else {
echo '<h3>هیچ موردی درین بخش یافت نشد.</h3>';
}
?>
<!-- pagination here -->
<p> <?php
if (function_exists("custom_pagination")):
custom_pagination($custom_query->max_num_pages,"",$paged);
endif;
?>
<?php wp_reset_postdata(); ?>
</p>
</div>

Wordpress Custom user Display Page

First I want to preface this with the page was working this morning and I left the page open and came back to the information of the user not displaying at all
This page is a custom written authors display page. Anyone who is a contributor of the site is listed here with a bio, Avitar and nickname, can someone please tell me what I am missing because I can't see what has been deleted.
<?php
/*
Template Name: Display Authors
*/
// Get all users order by amount of posts
$allUsers = get_users('orderby=post_count&order=DESC');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(in_array( 'contributor', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
?>
<?php get_header(); ?>
<section class="content-area" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="authors">
<?php foreach($users as $user) { ?>
<div class="page-author author-<?php echo ($curauth->ID & 1 ? 'odd' : 'even'); ?>">
<div class="author-avatar">
<?php echo get_avatar( $curauth->user_email, '200' ); ?>
</div>
<div class="author-info">
<h2 class="author-name"><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?></h2>
<h3 class="author-username"><?php echo $curauth->nickname; ?></h3>
</div>
<div class="clearfix"></div>
<p class="author-descrption"><?php echo get_user_meta($curauth->ID,'description', true); ?></p>
</div>
<?php
}
?>
</div>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
UPDATE:
So here is an update in my code, and still no luck, all the user information on the page remains empty. So its not showing the Name, Nickname, Bio...nothing
<?php
/*
Template Name: Display Authors
*/
// Get all users order by amount of posts
$allUsers = get_users('orderby=post_count&order=DESC');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(in_array( 'contributor', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
?>
<?php get_header(); ?>
<section class="content-area" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="authors">
<?php foreach($users as $user): ?>
<div class="page-author author-<?php echo ($curauth->ID & 1 ? 'odd' : 'even'); ?>">
<div class="author-avatar">
<?php echo get_avatar( $curauth->user_email, '200' ); ?>
</div>
<div class="author-info">
<h2 class="author-name"><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?></h2>
<h3 class="author-username"><?php echo $curauth->nickname; ?></h3>
</div>
<div class="clearfix"></div>
<p class="author-descrption"><?php echo get_user_meta($curauth->ID, 'description', true); ?></p>
</div>
<?php endforeach; ?>
</div>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

pagination won't show in Wordpress

I have this code to show all posts in the blog by 3 per page (for specific reasons I have to use Wp_Query). I can't figure out how to add pagination to this. Here is my code for the post listing:
<?php
$all_posts = new WP_Query(array(
'posts_per_page' => 3,
));
if ($all_posts->have_posts()): while ($all_posts->have_posts()) : $all_posts->the_post();
?>
<div class="row leftNestedRow whiteWrapper boxyShadow">
<div class="large-12 columns">
<h2><?php the_title(); ?></h2>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}?>
<p class="authorDate">Av <a>Fryk</a>, <?php the_time('Y-m-d'); ?></p>
<p><span class="homepageBlogExerpt"><?php echo get_the_excerpt(); ?></span>[Läs mer]</p>
<ul class="tags">
<?php
$links = array();
foreach(get_the_tags() as $this_tag) {
if ($this_tag->name != "featured"){
$links[] = '<li>'.$this_tag->name.'</li>';
}
}
$ai = 0;
while ($links[$ai]) {
echo $links[$ai];
$ai++;
}
?>
</ul>
</div>
</div>
<?php endwhile; ?>

Wordpress category template is showing posts from all categories instead of specific category posts

I have an archive page that's set up to display category specific posts. However, instead of showing only the posts from a given category on the category page, it's showing all posts. For an example, see here.
Here's the code I'm using on my archive.php page. I know it's improper use of the loop, but I'm not sure how to fix it. Thanks for the help.
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<h1 class="blogTitle" style="margin:10px 0 3px 0;">Blog Title</h1>
<nav class="blogNav" role="navigation">
<?php bones_blog_nav(); // Adjust using Menus in Wordpress Admin ?>
</nav>
<div id="main" class="eightcol first clearfix" role="main">
<div class="catwhiteBg">
<?php if (is_category()) { ?>
<h1 class="catTitle">
<span><?php _e("", "bonestheme"); ?></span> <?php single_cat_title(); ?>
</h1>
<?php echo category_description( $category_id ); ?>
<?php } elseif (is_author()) { ?>
<div class="authorTop">
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
<div class="author-pic"><?php echo get_avatar( $curauth->user_email, '80' ); ?></div>
<div class="author-name"><span style="font-weight: 200; color: #575757;">POSTS BY:</span>
<?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?>
</div>
<div class="author-bio"><?php echo $curauth->description; ?></div>
<div class="author-twitter"><span>twitter</span></div>
</div>
<?php } elseif (is_day()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Daily Archives:", "bonestheme"); ?></span> <?php the_time('l, F j, Y'); ?>
</h1>
<?php } elseif (is_month()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Monthly Archives:", "bonestheme"); ?></span> <?php the_time('F Y'); ?>
</h1>
<?php } elseif (is_year()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Yearly Archives:", "bonestheme"); ?></span> <?php the_time('Y'); ?>
</h1>
<?php } ?>
</div>
<div class="psts">
<?php
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
query_posts(array('posts_per_page' => '8','paged'=>$paged));
?>
<?php
$count = 1;
while (have_posts()) : the_post(); ?>
<div class="sixcol small pst<?php if ((isset($count)) && ($count % 2 == 0 )) { echo ' last';} // same logic to add class of last to last item in row of two ?>" id="post-<?php the_ID(); ?>">
<article id="post-<?php the_ID(); ?>" role="article">
<div class="thumb-wrapper mobile">
<?php if(has_post_thumbnail()) { $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'post-thumb' ); echo '<img src="' . $image_src[0] . '" width="100%" class="post-thumb" />'; } ?>
<header class="post-thumb-header">
<h2 class="post-title"><?php the_title(); ?></h2>
</header> <!-- end article header -->
<p class="meta"><?php the_category(', '); ?></p>
</div>
<section class="mobile-content">
<?php the_excerpt(); ?>
</section>
</article> <!-- end article -->
</div>
<?php $count++; ?>
<?php endwhile; ?>
<nav class="wp-prev-next">
<?php echo rb_pagination(); ?>
</nav>
</div> <!-- end .psts -->
</div> <!-- end #main -->
<?php get_sidebar(); // sidebar 1 ?>
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>
I think you're overriding the original query with this line:
query_posts(array('posts_per_page' => '8','paged'=>$paged));
Can you swap this line with this, and let me know the results?
global $query_string;
query_posts( $query_string . "&posts_per_page=8&paged=$paged" );
if that fails try this:
query_posts(array('posts_per_page' => '8','paged'=>$paged, 'cat' => $category_id ));

Resources