post_per_page in wordpress not rendering properly on the page - php

Hi i have this wordpress site and the pages will display images on it. Now my problem is when i add post_per_page => '50' then when i refresh the page the number of posts per page is not rendering correctly. Here is my code below.
<?php
query_posts( array(
'post_per_page' => 50,
'cat'=> '7',
'order' => 'ASC'
) );
?>
<?php while(have_posts()) : the_post(); ?>
<div class="single-gallery anim-5-all interoors masonryImage mix span-4">
<div class="img-holder">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<img src="<?=$url; ?>" alt="">
</div>
</div><!-- /.single-gallery -->
<?php endwhile; wp_reset_query(); ?>
Now it will display 9 images on the page. Can someone help me figured this thing out? Any help is muchly appreciated. TIA

For a first, I fond mistake in your query arguments
'post_per_page' => 50,
There is no such argument for wp_query loop use instead it
'posts_per_page' => 50,
For a second there is quote from a wodpress codex about query_posts function
Note: This function will completely override the main query and isn’t
intended for use by plugins or themes. Its overly-simplistic approach
to modifying the main query can be problematic and should be avoided
wherever possible. In most cases, there are better, more performant
options for modifying the main query such as via the ‘pre_get_posts’
action within WP_Query.
I can recomend you to use standard wordpress loop
<?php
$args = [
'posts_per_page' => 50,
'cat' => '7',
'order' => 'ASC',
];
// The Query
$query = new WP_Query( $args );
// The Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="single-gallery anim-5-all interoors masonryImage mix span-4">
<div class="img-holder">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<img src="<?=$url; ?>" alt="">
</div>
</div><!-- /.single-gallery -->
<?php
endwhile;
wp_reset_postdata();
?>

Related

Modification of category template in WordPress using PHP

I am having some trouble building the category template i want. Here is an example of one of my categories: http://transcorrect.bg/pi-de/category/versicherungen/
I want this field http://prntscr.com/k2d5xg to show posts only from the current category. I am attaching the code i am using right now, but it is only for specific category. What i want is to be on archive.php file as a code, that takes dynamically the category ID.
Minimal example of code
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?php
// get the top-category posts
$the_query = new WP_Query(array(
'category_name' => 'Versicherungen',
'posts_per_page' => 10,
));
if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div id="list-news-home" class="col-lg-12" style="padding-top:10px;padding-bottom:10px;">
<img src="http://transcorrect.bg/pi-de/wp-content/uploads/2018/07/arrow1.png" style="padding-right:20px;"><?php the_title(); ?>
[ mehr ]
</div>
<hr style="margin-top:50px;margin-bottom:10px">
<?php endwhile; ?>
<?php endif; ?>
</div>
I can't figure out how to get it so if someone has made something similar i will be thankful to know how :)
I have worked it out.
If anyone needs similar thing i am posting how i converted the code i posted earlier.
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?php $current_cat_id = get_query_var('cat'); $showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
" style="color:#00315C">
" style="float:right"> [ mehr ]
<?php endwhile; ?>
<?php endif; ?>

Custom post type pagination is not working

I am creating my custom theme and there's Custom Post Type too.
However, pagination for the Custom Post Type is not working. I tried all the possible solution from Stack Overflow but all in vain.
Here's the code:
<?php global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 3,
'post_type' => 'services',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => false,
'paged'=>$paged
);
$the_query = new WP_Query( $args ); ?>
<div class="service-content clearfix">
<ul class="clearfix">
<?php if ( $the_query->have_posts() ) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $word_count = strlen( wp_strip_all_tags($post->post_content));
$id = get_the_ID();?>
<li class="col-sm-4 wow fadeInDown animated" data-wow-delay="300ms" data-wow-duration="500ms">
<figure class="image">
<?php the_post_thumbnail( 'medium' ); ?>
</figure>
<?php if($word_count<269){ ?>
<h3><?php echo $post->post_title; ?></h3>
<p><?php echo $post->post_content; ?></p>
<?php } else{ ?>
<h3><?php echo $post->post_title; ?></h3>
<?php echo $post->post_content; ?>
<?php } ?>
</li>
<?php endwhile;
next_posts_link();
previous_posts_link();?>
<?php wp_reset_query(); ?>
<?php endif; ?>
</ul>
</div>
Here, posts_per_page is working but Pagination not working , any help?
Please use bellow codes as your need..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$next = $paged+1;
$prev= $paged-1;
<a href="<?php echo '/page/'.$next; ?>" >NEXT</a>
<a href="<?php echo '/page/'.$prev; ?>" >PREV</a>
Just use paginate_links function
to display the pagination based on your query and paging
$paginate_args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'total' => $the_query ->max_num_pages,
'current' => $paged,
'prev_text' => __('«'),
'next_text' => __('»'),
);
echo paginate_links( $paginate_args );
Make sure the base and format are correct. based on your permalink structure
One of the problems with pagination in Wordpress is that the Posts per page value is ignored and the Blog pages show at most setting in the Reading page of the Wordpress settings is taken as the actual value. So, you are trying to show 3 per page and on the first page that works. However, when you go to the second page, Wordpress is loading a different offset so your code will not work.
This issue has been explained extremely well over on the Wordpress Stack Exchange so I won't repeat that user's answer. You can read more here:
https://wordpress.stackexchange.com/questions/30757/change-posts-per-page-count
That should sort out the problem for you.

Display (Wordpress) Child Pages with Title, Thumbnail and Excerpt

Apologies in advance for a question, which to an expert, I've no doubt is relatively obvious. I've looked at WordPress Codex for the appropriate information (the_post_thumbnail, the_excerpt, etc) but am not well versed enough in .php yet to implement it properly. Still learning!
I am trying to display, within the of a standard (WP) page, the child-pages, including their Title, Thumbnail and Excerpt. I can get everything to work bar the THUMBNAIL and EXCERPT with the following:
<div class="child-pages">
<?php
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
?>
<!-- loop: child page -->
<div class="child">
<header class="entry-header">
<?php echo '<h3>'.$page->post_title.'</h3>'; ?>
</header><!-- .entry-header -->
<img src="<?php echo the_post_thumbnail_url( $page->ID ); ?>">
<?php echo $page->the_excerpt; ?>
</div>
<?php } ?>
</div>
So far, I can see the links/titles of the correct child-pages, and in the correct order, but not the Thumbnail or Excerpt. Obviously, I am not calling the Thumbnail or the Excerpt properly. Could someone please correct me?
I have also tried these lines, as supported by the twenty-sixteen theme:
<?php twentysixteen_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Any help would be much appreciated!
The thumbnail doesn't show because the function the_post_thumbnail_url(); shows the thumbnail of the current post, the parameter is to specify the size of the image, not the post.
$post->the_excerpt is not necessarily filled. If you look at the add/edit post screen you'll notice two textfields, one for editing the content of the post and one for the excerpt. The excerpt is optional, so the function the_excerpt() shows the content of that field, but when it is empty it will show the first X characters of $post->the_content.
Regarding your second attempt: the functions the_excerpt(), get_permalink() and twentysixteen_post_thumbnail() don't work because you don't set up the post properly.
The easiest way would be to add setup_postdata() to your code:
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
setup_postdata( $page );
// now you can use all the fancy functions like `the_excerpt()`
// add your output here
}
Or you can do it the recommended way, using a WP_Query object:
<?php
$args = array(
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Access the_post() contents in a custom sidebar

I have this code in single-legislacion.php page:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
<?php endif; ?>
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $thePostID,
'post_status' => 'inherit',
'numberposts' => 1
);
$attachments = get_posts( $args );
if ( $attachments ) :
foreach ( $attachments as $attachment ) : ?>
<div class="BaseIco">
<a class="IcoDescargas" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
<img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png"><br>
Descargar PDF
</a>
</div>
<?php endforeach;
endif;
?>
Because I am using Table of Content Plus plugin and have not found a way to display the generated TOC outside of the_content() itself, the only solution I have is to display it through a widget on a sidebar. Now, my question is: can I access the_post() content, like attachments for example, in a custom sidebar? How?
Also if any knows any variant to show the TOC outside the content, I'll be graceful if can share it.
No, I don't think so. the_post() only works inside of the loop, sidebars are usually rendered outside of it.
However, you can use get_post with global $post, i.e.:
global $post;
$p = get_post($post->ID);

WordPress Gallery on single page

My current WordPress theme has a built in gallery. Each gallery displays the first / main thumbnail and once clicked opens the entire content of this specific gallery in a shadowbox. My ideal goal now is to find a way to alter this that instead of displaying the shadowbox directly it would direct to a single page with the specific galleries content.
As below my current gallery code:
<div id="maincontentwrap">
<div class="main-sep">
</div><!-- .main-sep -->
<div id="contentwrap">
<div id="content-gallery" role="main">
<?php
$wp_query = new WP_Query();
$wp_query->query( array( 'post_type' => 'galleries', 'posts_per_page' => of_get_option('le_gallery_items'), 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$mod = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
if(has_post_thumbnail()) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$src = $src[0];
}
else{
foreach ( $attachments as $id => $attachment ) {
$src = wp_get_attachment_url( $id );
}
}
if ($mod % 3 == 0) {
echo ' <div class="gallery-entry-img-last">';
}
else{
echo ' <div class="gallery-entry-img">';
}
?>
<div class="gallery-entry-img-l"><a rel="next" href="<?php echo $src; ?>"><span class="rollover" ></span><img class="blog-entry-img" src="<?php echo get_bloginfo('stylesheet_directory'); ?>/library/tools/timthumb.php?src=<?php echo $src; ?>&w=270&h=198" alt="<?php get_the_title(); ?>"/></a>
<span class="gallery-entry-img-tab"></span>
</div>
<span class="gallery-index-title"><?php the_title(); ?></span>
<div class="hidden-gallery">
<?php
$pic_count = 0;
foreach ( $attachments as $id => $attachment ) {
$pic_count++;
$others_src = wp_get_attachment_url( $id );
?>
<a rel="next" href="<?php echo $others_src; ?>" title="<?php the_title(); ?>"></a>
<?php
}
?>
</div><!-- .hidden-gallery-->
</div>
<?php
$mod++;
endwhile;
?>
<div style="clear:both;"></div>
</div>
</div><!-- #contentwrap-->
</div><!-- #maincontentwrap-->
Would anybody have an idea how I can have this achieved? Some expert advise would be truly appreciated.
Use wp_get_attachment_link( $id, $size, $permalink, $icon, $text ); (see http://codex.wordpress.org/Function_Reference/wp_get_attachment_link). This outputs the link to the attachment page. So you just need to adapt your code with this function. Use value = 0 for the $permalink argument so your link brings you to the attachment page (value = 1 will bring you to the file itself).
Beware, though, that the "shadowbox" is probably triggered by event binding (clicking on the anchor) in a JavaScript, so you might have to adapt your HTML code for the links. If the selector in the script for shadowbox uses a class name or an ID to detect the click, you will probably need to change the ID or class name of the container of your link in order to be sure that your target page does not get displayed within the shadowbox.
You can also (and should, actually) use wp_deregister_script() to deregister the script that displays the shadowbox, considering you don't need it in this page.

Categories