I'm creating a wordpress top-down website by sections and i don't know which is the best way to get the pages into different sections.
I also want to take into account the charging performance
<section id="about">
<!-- Content of page -->
</section>
<section id="services">
<!-- Content of page -->
</section>
<section id="contacts">
<!-- Content of page -->
</section>
Thank's
I would use a simple WP_Query instance here, and use the following code:
<?php
// Set the WP_Query arguments
$args = array(
'post_type' => 'page',
// You need to change this to match your post IDs
'post__in' => array( 10, 20, 30 ),
'order_by' => 'post__in',
'order' => 'DESC' // Might need to change this to "ASC"...
);
// Create the page section query
$page_section_query = new WP_Query( $args );
// Simple check...
if( $page_section_query->have_posts() ) :
while( $page_section_query->have_posts() ) :
$page_section_query->the_post();
global $post;
// Print out the section!
?>
<section id="<?php echo esc_attr( $post->post_name ) ?>" <?php post_class( array( esc_attr( 'page-section-' . $post->post_name ) ) ); ?>>
<!-- contents of the page section -->
</section>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Simple and effective, 1 query is all that this needs. Just keep adding more post IDs if you want more sections.
If you want to avoid using post IDs as an ordering parameter you could use: menu_order instead. And if you would like to avoid using post__in parameter you could add all the pages to a page parent and use the parent post ID and get all the page children for the sections. This would make the solution a little more modular.
Read more about WP_Query here: https://codex.wordpress.org/Class_Reference/
Related
Dear PHP / Wordpress / Dev Experts,
Ive build a plugin, with a custom post type and some advanced custom fields. The main goal is to list the members in my band, with pictures and name.
You can see it here: http://www.lucky13.nl/test/
I've managed to get everything working to my taste, however.. I have 5 bandmembers in my band which i've added, but i'm only seeing 4. Where is the 5th entry / post? I find that the first added bandmember is not displaying.
I assume this has to do with the loop, and the array not listing all items? But i'll leave this to the experts.. I would appreciate any comments of help!
My code:
<?php
/*
Plugin Name: VrolijkWebdesign - Bandmembers
Description: For a bandwebsite to show bandmembers.
*/
/* Start Adding Functions Below this Line */
/* NAME FUNCTION */
function new_section_1(){
$bandmembers = new WP_Query(array(
'post_type' => 'bandmembers'
));
while($bandmembers->have_posts()) : $bandmembers->the_post();
if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!-- START HTML -->
<div class="span2">
<figure class="snip1104">
<img src="<?php echo $image[0] ;?>" alt='sample33'/>
<figcaption>
<h5> <?php the_field('firstname'); ?> <span> <?php the_field('lastname'); ?>
</span></h5>
</figcaption>
</figure>
</div>
<!-- END HTML -->
<?php endif;
endwhile;
}
add_shortcode('band', 'new_section_1');
?>
$bandmembers = new WP_Query(array(
'post_type' => 'bandmembers',
'posts_per_page' => '5'
));
Try setting the posts_per_page argument. Since the default might be set to '4' by other filters.
If you want to get all posts in a single query use '-1' instead of '5'
You could also try the below for debugging purposes only:
-Try to set post_status to 'any' to make sure that post statuses are not a problem.
-Try var_dump($bandmembers) after doing the query to see the fetched posts before the loop starts.
Hi I am having an issues with custom post type.
So what I am trying to do, list all posts from subcategories while calling a main category. Usualy this worked with no problem with normal post type from Wordpress, but since I tried to use custom post type it's not working...
My category structure is like this:
Category
Sub category
( Posts inside )
Sub category
( Posts inside )
Any help or tips are appreciated. Thanks
<?php
$categories = get_categories('title_li=&hide_empty=1&parent=1430');
foreach($categories as $category) {
echo "<div class='col-12' style='border-bottom: 0'><h1 class=''>".$category->name."</h1></div>";
$args = array('cat'=> $category->term_id);
if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<!-- article -->
<article class="col-3">
<div class="image">
<span class="helper"></span><?php the_post_thumbnail('full');?>
</div>
<h1><?php the_title(); ?></h1>
<?php the_content();?>
</article>
<!-- /article -->
<?php endwhile; endif; }?>
</main>
There are a couple of problems going on here:
First, you're not declaring a loop, or calling get_posts.
Second, if you check out the documentation for WP_Query (which is the "backbone" behind get_posts, so the arguments are essentially the same), you'll see that if you do NOT pass in an argument for post type, the default is post.
So, since you've not shared with us the post type, you'll have to adjust the below as needed:
// .. your code above ....
$args = array(
'cat'=> $category->term_id,
// Include the post_type in the query arguments
'post_type' => 'custom-post-type' // Change this as needed
);
// Now we need to actually query for the posts...
$custom_posts = new WP_Query( $args );
// These are modified to use our custom loop...
if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
// .. your code below ... the_title(), etc will work here...
I have set up a form with advanced custom fields on the frontend in order to allow posts being generated from the frontend.
The form works but every time I visit the page with the form the form is prefilled with values however in the custom field setup I have no default value specified.
here is the code I am using.
<?php acf_form_head();
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php the_post(); ?>
<?php
$options = array(
'post_id' => 'new',
'field_groups' => array(36),
'submit_value' => 'Create Quote Request' ,
'updated_message' => 'Quote Created!'
);
acf_form( $options ); ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Any advice is appreciated.
Change 'Create Quote Request' to ' ' because that's the text populating your text box... so removing it will stop populating it.
I ran into the same problem. I couldn't figure out how to unset it properly, so I jQueried it. Hope this helps.
$(".reset").ready(function() {
$('.acf-form').find("input[type=text], textarea").val("");
});
Replace .acf-form with either your form id or class
I altered to my needs from this:
Clear form fields with jQuery
i am creating a related post article feature for my wordpress enabled site.being new to php and wordpress facing some issue.
though i can user wordpress build in feature to show related post of author but since site is hosting articles of authors who don't have any profile with us and there are many such authors so when a new post is being created for the author we tend to save a custom field as author_email.
so based on this we want to show all post being published of a particual author.i tried using get_posts() method
<?php
$args = array( 'meta_key' => 'author_email', 'meta_value' => 'XYZ#XYZ.com');
$authorposts=get_posts($args); ?>
<div id="content">
<span class="breadcrumbs">Home » <?php the_category(', ') ?></span>
<?php if (count( $authorposts ) > 0) {
foreach ( $authorposts as $post ): setup_postdata($post) ?>
<div id="headline_author">
/*
showing post tilte with image and some part of it
*/
<div class="clearfloat"></div>
</div>
<?php endforeach; ?>
<div class="post-nav">
<div class="previous"><?php previous_posts_link('‹ Previous Page') ?></div>
<div class="next"><?php next_posts_link('Next Page ›') ?></div>
</div>
<?php
} else {
echo '<p>No articles by this user</p>';
}
?>
</div>
it showing the first 5 results and a link to the next page but when i click on the next page it show me that it has gone to second page as its also visible on the URL but its showing the same 5 results what it has shown in the first page.
i am an idea that the query i have written on the same page is getting executed again from the beginning but not sure how to fix it.
since i am using cutom fields to get posts i can use simple query to fetch post based on the limit or offse etc.
can any one help me to fix the things or can point me hw to do it in right way
thanks in advance
I am not word-press/php expert just started learning about it. it seems that you have not set the paged global variable by which it can track the page number.
you need to do something like this.
$post_per_page = get_option('posts_per_page');
if (!is_paged()) {
$custom_offset = 0;
} else {
$custom_offset = $post_per_page*($paged-1);
}
$args = array( 'meta_key' => 'author_email', 'meta_value' => xyz#xyz.com' ,'numberposts' => $post_per_page,'offset' => $custom_offset);
I am using WordPress 3, and I created a custom post type called article, which gives me the URL format of mywebsite/articles/article-title. How do I see all the article entries in the URL mywebsite/articles?
Assuming you set up everything correctly and you want to see the post type on a public template page, try this code into mycustomtemplate.php or the equivalent.
<?php $loop = new WP_Query( array( 'post_type' => 'article', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
You can customize the loop just like you would blog posts and pages. If you want to get all on one page you'll want to remove the limit of 10 on post_per_page I wouldn't suggest it though. I would say set it to 50 or 100 and still use pages.
Source: Custom post types in WordPress
I have the simplest solution. Just create file archive-{custom post type}.php and then, just do loop content as usual.
If you already set the permalink, just type yourdomain.com/{custom post type}.
You can easily achieve this from the definition of your custom post type, starting with WP 3.1. Just set has_archive to true.
Source: http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types