orderby menu_order show max 10 pages - php

trying to put together a wp site for a martial arts centre, and they need a list of their instructors.
i try to get all the children of the instructor page appear in list by their page order, and that works fine, except it only shows the first 10 pages, and there is currently 13 instructor pages...
this is the code i used:
<section id="instruktorer">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=instruktorer' );
$services_id = $query->queried_object->ID;
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$services_query = new WP_Query( $args );
// The Loop
if ( $services_query->have_posts() ) {
echo '<ul class="instruktorer">';
while ( $services_query->have_posts() ) {
$services_query->the_post();
echo '<li class="clear">';
echo '<a href="' . get_permalink() . '" figure class="instruktorer-thumb">';
the_post_thumbnail('instructor-pic');
echo '</a>';
echo '</figure>';
echo '<div class="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</div>';
echo '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div><!-- .indent -->
</section>

Change your query to this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC'
);

Related

Function to display Images based on category on WordPress

Hi There I am writing some PHP code to get some images assigned a custom category given to them via a WordPress Plugin which allows me to assign category's to individual images. The code is below is what I have at the moment
function showFooterImages() {
$footerImagesArray = array(
'post_type' => 'attachment',
'post_status' => 'published',
'posts_per_page' => 4,
'orderby' => 'rand',
'category' => 'footer-images',
);
// Query to get footer images
$footerImagesQuery = new WP_Query( $footerImagesArray );
// The Loop
if ( $the_query-> have_posts() ) {
echo '<ul>';
while ( $footerImagesQuery->have_posts() ) {
$footerImagesQuery->the_post();
echo '<li>' . get_the_post() . '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
If I replace the
echo '<li>' . get_the_post() . '</li>';
with the following
echo "<p> this is a test </p>";
I do get 4 lines of text from the echo showing it is working and if i change the posts_per_page to 6 for example I then get 6 however I am struggling to get the images even displaying and any assistance would be greatly appreciated.
Was able to get this working with some advice from CBroe for some reason cant tag him but he pointed me in the right direction
Got this working by doing the following
function showFooterImages() {
$footerImagesArray = array(
'post_type' => 'attachment',
'post_status' => 'published',
'posts_per_page' => 4,
'orderby' => 'rand',
'category' => 'footer-images',
);
// Query to get footer images
$footerImagesQuery = new WP_Query( $footerImagesArray );
$images = array();
foreach ( $footerImagesQuery->posts as $image ) {
?>
<img src="<?php echo wp_get_attachment_url( $image->ID ) ; ?>">
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
}

How can I add navigation links below WordPress posts using PHP?

I am working on a PHP/WordPress-based website and would like to add 3 navigation links (one previous link and 2 next links) below each post of a custom post type. The links should look like this:
[prev] [next] [next]
My current code is below, but it is not working at the moment. How can I get this code to display the desired links?
$currentID = get_the_ID();
$args = array (
'post_type' => array( 'guides' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'ignore_sticky_posts' =>true,
'post__not_in' => array($currentID)
);
$guides_query = new WP_Query( $args );
if ( $guides_query->have_posts() ) {
echo '<div class="grid-container nxt-article">';
echo '<div class="nxt-article-heading"><h2>Next article</h2></div>';
while ( $guides_query->have_posts() ) {
$guides_query->the_post();
echo '<div class="nxt-article-box"><div class="nxt-article-box-shadow">';
echo '<a class="nxt-article-link" href="'.get_the_permalink().'"></a>';
$gear_category = get_field('gear_category', $products->ID);
if ($gear_category) :
echo '<div class="g_pre_title" style="color:#fff;position:absolute;left:30px;top:25px;">'.$gear_category[0].'</div>';
endif;
echo '<div>';
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'post_grid_image' );
}
echo '</div><div style="padding: 0px 30px 30px 30px;"><div class="nxt-article-box-title">'.get_the_title().'</div>';
$intro_section = get_field('introduction_section', $post->ID);
if( $intro_section ) :
echo '<div class="pg_excerpt" style="height:85px;overflow:hidden;">'.wp_trim_words( $intro_section['wysiwyg'], 32, '...' ).'</div>';
endif;
echo '</div></div></div>';
}
echo '<div style="text-align:center;"><a class="ml_button_style" href="">Show ALL GUIDES</a></div>';
echo '</div>';
} else {
}
wp_reset_postdata();

How to add category option to shortcode

I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}

WordPress Custom Taxonomies WP_Query

I'm trying to create a query where I create multiple categories (taxonomies) in a custom post type, and then on the homepage query based on specific which is working fine. Currently I have 3 taxonomies:
current-specials
meineke-difference
featured
I have already written code that pulls these. The problem I'm running into is that on the homepage it needs to only pull these posts when they are also attached to the "featured" taxonomy. So an example of standard logic for this would be:
if taxonomy = current-specials AND featured then success else fail
But what it's doing is pulling them all because the current code is OR, and I need AND
Thoughts? (code below)
<?php
$post_type = 'coupons';
$tax = 'coupons_category';
$tax_terms = get_terms($tax);
if ($tax_terms):
foreach ($tax_terms as $tax_term):
echo '<div id="'.$tax_term->slug.'" class="coupon-box '.$tax_term->slug.'">';
$args = array(
'post_type' => $post_type,
"$tax" => array($tax_term->slug, 'featured'),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1
);
$myQuery = null;
$myQuery = new WP_Query($args);
if($myQuery->have_posts()):
while ($myQuery->have_posts()) : $myQuery->the_post();
$price = get_field('price_image', $myQuery->ID);
$print = get_field('print', $myQuery->ID);
$product = get_field('product_box_image', $myQuery->ID);
$title = get_the_title();
$content = get_the_content();
echo '<div class="fourty9 left box center">';
echo '<h1>'.$title.'</h1>';
echo '<p class="center"><img src="'.$price.'" /></p>';
echo '<p>'.$content.'</p>';
echo '<p class="center">Print Coupon</p>';
echo '<p class="center"><img src="'.$product.'" alt="Filter"></p>';
echo '</div>';
endwhile;
endif;
echo '</div>';
wp_reset_query();
endforeach;
endif;
?>
You may try this (tax - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query')
$args = array(
'post_type' => 'coupons',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'coupons_category',
'field' => 'slug',
'terms' => array( 'current-specials', 'featured' ),
'operator' => 'AND'
)
)
);
$query = new WP_Query( $args );

Wordpress - Including page title in 'wp get attachment image'

Okay, I've set up a bit of code which searching for all the pages which are a child of the ID 8, then outputs all the attachments (in the gallery) of these pages as unordered list items. You can see the effect so far here http://goo.gl/eq4UF.
The problem I'm having is that I need to include the title of each page before each so you can easily identify which images come below which page. Normally I would just add this in, but the list items al use masonry and are positioned all over the page using some JS so they never appear beside the first image in the list.
I therefore will add the title of the page to every
<li> in the <ul> which will allow the title to run with each image but I don't know how to include this in the wp get attachment image function. Both the_title and wp_title doesn't work inside this loop. apply_filters( 'the_title', $attachment->post_title ); obviously takes the image title, but is there any good to take the page title?
Thanks in advance and hope this made sense,
R
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>
You can try this:
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
$post_title = get_the_title($post->ID); // We get the post title
foreach ( $attachments as $attachment ) {
$img_title = apply_filters( 'the_title', $post_title . ' - ' . $attachment->post_title ); // We create the image title with the 2 strings
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' , false, array('title' => $img_title));
echo '<p>';
echo $img_title;
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>

Categories