i have a single page website built on wordpress (twenty-thirteen-child template).
header content footer.
and 3 posts categories :category A category B category C.
i need to split the content into these 3 categories/sections -like this
<div class="section_top">
all the posts from category A
</div>
<div class="section_mid">
all the posts from category B
</div>
<div class="section_bot">
all the posts from category C
</div>
i got a little confused when i started to read about the wordpress main loop ,query_posts and WP_Query,eventually i have this code replacing the main loop ,so my questions are :
1 is this the correct way to do it?
2 how to give each section a unique class since i need to style each section diffrently?
here is the code (index php(in child theme)
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$args = array('category__in' => array(catA,catB,catC));
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div id="<?php
foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>"
class="post-item">
<div class="post-info">
<h2 class="post-title">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
<div class="post-content">
<?php the_content(); ?>
</div>
</div>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
The problem with how you're trying to solve it is that all posts are still shown chronologically, so this could happen:
Category A post 12/01
Category B post 09/01
Category A post 05/01
What I'd recommend: make 3 different WordPress loops querying each category separately like so:
<div class="section_top">
WordPress loop for all the posts from category A
</div>
<div class="section_mid">
WordPress loop for all the posts from category B
</div>
<div class="section_bot">
WordPress loop for all the posts from category C
</div>
Example of such a loop:
// The Query
$the_query = new WP_Query('category_name=categoryA');
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Difference between get_the_...() and the_...()
When you use functions like get_the_title() you are able to store them in a PHP variable like so:
$title = get_the_title();
echo strtoupper($title); //do something with it later
When you use functions like the_title() then the title is immediately printed on the page, so there's no need for an echo statement:
the_title();
Notice: the_permalink() has a function get_permalink(), the function get_the_permalink() does not exist. Don't ask my why ;)
Related
Is it possible to add anchor links to Advanced Custom Fields?
I've created a page that shows images that when you click on the image a lightbox opens with the description. I have it all working without using Advanced Custom Fields but I'm trying to make it user friendly for the client so I added fields for them to just fill in the information and everything is already styled.
The problem is the tabs use an Anchor Link to link to the ID DIV that opens up the lightbox with the description - it works but not when I use the fields I created.
I created a url field for them to fill in the #div name for the anchor url and a field for the name of the #div for the anchor to link to. (Hope that makes sense!)
I'm also using repeater fields so they can add/delete flavors.
This is the code I am using -
<?php
// check if the repeater field has rows of data
if( have_rows('flavors') ):
// loop through the rows of data
while ( have_rows('flavors') ) : the_row();
// display a sub field value
?>
<li>
******** This is the anchor link *************
<?php
$link = get_sub_field('flavor_name');
if( $link ): ?>
<a href="<?php echo esc_url( $link ); ?>" class="wplightbox" data-width=900 data-height=800>
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
</a>
<?php endif; ?>
********* This is the div that needs to pick up the anchor name typed in as the Div ID name *****
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
<?php the_sub_field('flavor_name'); ?>
<?php endif; ?>
<div class="flavors">
<div class="flavorsleft">
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="flavorsright">
<h3><?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?></h3>
<div class="percent">
<?php if( get_sub_field('alcohol_percentage') ): ?>
<?php the_sub_field('alcohol_percentage'); ?>
<?php endif; ?>
</div>
<?php if( get_sub_field('beer_description') ): ?>
<?php the_sub_field('beer_description'); ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="clear"></div>
</li>
<?php
endwhile;
else :
// no rows found
endif;
?>
</ul>```
This is the dev site where it's at - https://headley.tfm-dev.com/beers
The page is working right now because it's currently built not using the ACF fields (I took them off for now and rebuilt it) but I would like to change it so the owner can make changes.
Thank you!
[![Screenshot of Advanced Custom Fields][1]][1]
[![Screenshot of Advanced Custom Fields Redone][2]][2]
[1]: https://i.stack.imgur.com/NeKYe.jpg
[2]: https://i.stack.imgur.com/uUN0G.png
On your dev site in the source code the anchor href is an id but you are using a page link field to match to the get_sub_field('flavor_bottom_section_name') so this would return a url so won't work.
Correct me if I'm wrong but you are using 2 repeaters, does that not reply on each of them to match up to each other? I would do this wiht one repeater field and you'd be able to avoid mistakes being made and would probably require less fields e.g you would only need one field for the anchor and one field for the div. You can have 1 repeater field but you can have 2 separate loops through the same repeater just getting what you want out of it on each loop.
ADDITIONAL
This line of code nothing is being ouput into the id=""
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
I think you need to echo the sub field like this maybe
<div id="<?php echo get_sub_field('flavor_name') ?>" style="display: none;">
Yon can see in the red boxes I've marked that the id attribute is empty on the div, even if I change it locally in the inspector it works, not sure why you got an error earlier but I also think that the value in your field might have # before it but the # is only only required in the href not the id.
Final Solution (Hopefully)
Ok I used your code and I see why you were getting the critical error this:
<a href="#<?php if( get_sub_field('beer_title') ): ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
Needs to change to this:
<a href="#<?php the_sub_field('beer_title'); ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
You have and if in there which isn't needed so removing that and echoing the sub field fixes it, we just needed to remove the endif also. I should have asked what the error was originally.
What you are saying here is
<a href="#
// IF THERE IS A SUB FIELD VALUE FOR BEER TITLE
<?php if( get_sub_field('beer_title') ): ?>"
// OUTPUT THIS
class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
get_sub_field() doesn't output anything it has to be echoed or else use the_sub_field() instead.
how can i create a grid like image? for show last posts
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php $latest_post = get_posts( 'numberposts=4' ); ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?><br />
<?php the_content(); ?>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
One option is to use PHP to swap around the position of the image based on whether or not the increment is odd or even, for instance:
$i = 0;
while ( have_posts() ) : the_post();
if ($i % 2 == 0 ) {
// Display two columns with image on left
}
else {
// Display two columns with image on right
}
$i++;
endwhile;
If you're building your theme from scratch I'd suggest using a grid framework to handle your columns, otherwise see if the theme you're using has a grid framework already.
Edit:
There are also ways of doing this without actually having to change the markup of the page. For instance:
Swapping columns (left / right) on alternate rows
In this case you'd be able to generate your post markup without the if statement and just use CSS to swap the position of the image/video.
You can use the masonry technology or you can try this plugin
In your code above you fired two WordPress content loops. I am not sure why you had to fire two loops, although they both will work. First will print the recent posts depending on the number of Posts Per Page you selected via Settings->Reading tab from your WordPress Dashboard and the second one will again print the top 4 recent posts. I am using the first loop to tell you how you can create the attached image like grid.
Below is the PHP/HTML modifications that you have to make:
<?php $count = 1; if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="one-half <?php if( $count++ % 2 == 0 ): echo 'last-col'; endif; ?>">
<?php
// the function below will print the featured image attached to the post
the_post_thumbnail( 'your-featured-image-sizename' ); ?>
</div>
<!-- one-half -->
<div class="one-half">
<span class="post_stamp"><?php the_time( 'j m Y' ); ?></span>
<span class="post_cat"><?php the_category(', '); // This will print News if you filled the post under News Category ?></span>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); // Insteading of printing the whole content this function will print exceprt only ?>
</div>
<!-- one-half -->
<div class="clearfix"><!-- --></div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You would have to put the below given links to your style file:
<style>
.one-half{width: 50%; float: left;}
.last-col{float: right;}
.clearfix{clear: both; overflow: hidden;}
</style>
After making the above changes your posts will be displayed as the attached image. Good Luck (y)
I really need some help. I've been banging my head since yesterday morning trying to build a custom design for my girlfriend's baking and cooking recipe blog in wordpress and I'm getting pretty frustrated at this point.
I followed the Lynda.com tutorial on making a custom design in wordpress and I managed to separate the template into header, main section and footer, but I kinda got stuck after that.
Here is a raw html page to see what the site should look like:
http://natalija.co.nf/index.html
And here is where I need some help. I want the main page to be divided into sections, each with a unique ID and data-stellar-background-ratio="0.5" for parallax background effect. These sections would represent categories (cakes, cookies, drinks, dishes etc.). Each category should contain an article with it's own class and data-stellar-ratio="1.5". Within an article there would be an h1 tag with the category title and a jquery slider that would contain links to recipes from that category.
So the structure of a section should look like this:
<section id="teroteikolaci" data-stellar-background-ratio="0.5">
<article class="torteikolaci" data-stellar-ratio="1.5">
<h1>TORTE I KOLAČI</h1>
<div class="wrapper">
<ul class="slider">
<li class="slide">
<a href="#">
<img src="wp-content/themes/Natalija/images/torte-i-kolaci-thumb01.jpg" alt="random">
<div class="bubble">
<h5>Čoko mousse torta 1</h5>
</div>
</a>
</li>
<li class="slide">
<a href="#">
<img src="wp-content/themes/Natalija/images/torte-i-kolaci-thumb02.jpg" alt="random">
<div class="bubble">
<h5>Čoko mousse torta 2</h5>
</div>
</a>
</li>
<li class="slide">
<a href="#">
<img src="wp-content/themes/Natalija/images/torte-i-kolaci-thumb03.jpg" alt="random">
<div class="bubble">
<h5>Čoko mousse torta 3</h5>
</div>
</a>
</li>
</ul>
</div>
</article>
</section>
As I said, I managed to separate the template into header.php, footer.php and home.php,
however I only managed to place the raw html code into home.php which I would like to replace with dynamic content, but I got lost following the guy from the tutorial.
I don't want the categories to be separate pages. I want them all to be displayed within the home page. How do I accomplish this?
I'm new to wordpress so the dashboard kinda confuses me and I'm not familiar with the wordpress php functions so I would really appreciate if someone could give me some guidelines on how to pull this through.
edit:
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
?>
<section id="<?php echo $category->slug; ?>" data-stellar-background-ratio="0.5">
<article class="<?php echo $category->slug; ?>" data-stellar-ratio="1.5">
<h1><?php echo $category->name; ?></h1>
<div class="wrapper">
<ul class="slider">
<?php
}
$args = array (
'post_status' => 'publish',
'category_name' => $category->slug,
'nopaging' => true,
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
// begin your slider loops in here
?>
<li class="slide">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="bubble">
<h5><?php echo get_the_title(); ?></h5>
</div>
</a>
</li>
<?php }
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</ul>
</div>
</article>
</section>
WordPress fetches posts / pages using the WP_Query object (and the nice thing is that it's well documented and you have access to use it and customize it). I would recommend that you create a dedicated loop (using WP_Query) for each section. Let me show you how...
Using "TORTE I KOLAČI" as the first example, just before you begin that section you can create a new loop like so:
// WP_Query arguments allow you to filter out / sort which posts you want
// In this example, I'm just telling WordPress to give me ONLY posts under the
// TORTE I KOLAČI category and NOT to use paging (so you get ALL posts in that
// category without limits)
$args = array (
'post_status' => 'publish',
'category_name' => 'torte_i_kolaci',
'nopaging' => true,
);
// The Query
$custom_query = new WP_Query( $args );
Note: The category_name is actually the slug of the category (and you can find that in the admin section under each category you've defined).
Now it's time to use the $custom_query.
Each section will still have the wrapper code like so:
<section id="teroteikolaci" data-stellar-background-ratio="0.5">
<article class="torteikolaci" data-stellar-ratio="1.5">
<h1>TORTE I KOLAČI</h1>
<div class="wrapper">
<ul class="slider">
Except now you'll switch into PHP and utilize functions such as the_post_thumbnail, get_the_title and get_permalink like so:
<?php
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
// begin your slider loops in here
?>
<li class="slide">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="bubble">
<h5><?php echo get_the_title(); ?></h5>
</div>
</a>
</li>
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
And now that you've looped all your posts, it's time to close out the wrapper...
</ul>
</div>
</article>
</section>
Now you can repeat this pattern for each category you plan to list (SITNI KOLAČI, NAPICI, etc).
Some Notes
I've assumed that your posts are actually posts and not pages (for each item in the loop). If you need pages, you can alter your $args array like so:
$args = array (
'post_type' => 'page',
'post_status' => 'publish',
'category_name' => 'torte_i_kolaci',
'nopaging' => true,
);
Dig into the WP_Query page on the codex to see how flexible these queries can be.
You might want to make your code even more dynamic and easy to maintain by first fetching your categories and then looping those to output the "outer wrapper". WordPress has a function called get_categories that I use all the time. get_categories also has it's own $args array, so take a look at the docs to see what you can do with it. You would structure your code like so:
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
?>
<section id="<?php echo $category->slug; ?>" data-stellar-background-ratio="0.5">
<article class="<?php echo $category->slug; ?>" data-stellar-ratio="1.5">
<h1><?php echo $category->name; ?></h1>
<div class="wrapper">
<ul class="slider">
<?php
}
And now the cool part! You can drive all your inner loops (using WP_Query) from the $category->slug value like so...
$args = array (
'post_status' => 'publish',
'category_name' => $category->slug,
'nopaging' => true,
);
Putting It All Together
So this is what the entire code snippet would look like (I added some comments for some of the braces that help you see where certain loops begin and end).
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
?>
<section id="<?php echo $category->slug; ?>" data-stellar-background-ratio="0.5">
<article class="<?php echo $category->slug; ?>" data-stellar-ratio="1.5">
<h1><?php echo $category->name; ?></h1>
<div class="wrapper">
<ul class="slider">
<?php
$args = array (
'post_status' => 'publish',
'category_name' => $category->slug,
'nopaging' => true,
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
// begin your slider loops in here
?>
<li class="slide">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="bubble">
<h5><?php echo get_the_title(); ?></h5>
</div>
</a>
</li>
<?php } // end $custom_query loop
} else {
// no posts found
}
// reset the postdata
wp_reset_postdata();
?>
</ul>
</div>
</article>
</section>
<?php
} // end $categories loop
?>
Hope this helps get you unstuck, have fun!
I need every three divs of class "loopcontent" to be wrapped in a new div of class "threeacross". I'm new to WordPress and really not a PHP guy yet, but I'm learning as I go.
Here's my loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="loopcontent">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else {
// the current post lacks a thumbnail
}
?>
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else : ?>
<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the categories, archives, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
Something like this should work. This keeps a running count and places your div wraps every three posts. Let me know if this helps. If not, let me know what it's outputting, and I can tweak it:
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="threeacross">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="loopcontent">
<?php
if(has_post_thumbnail()) the_post_thumbnail();
else{
// the current post lacks a thumbnail
}
?>
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
</div>
<?php endfor; else : ?>
<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the categories, archives, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
I have a page with many items pulled from posts.
I have it set up to only display 10 posts at a time, but my previous/next button isn't actually displaying the next or previous posts - it justs keeps displaying the same posts.
Here's the function I wrote:
function add_shop() {
if (is_page('shop') || is_page('7')) { ?>
<div id="content">
<div class="post_box">
<div id="column_1">
<div id="shop_wrapper">
<?php query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10');
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="shop_item"> <img src="<?php getCustomField('shop_thumbnail_image'); ?>" alt='photo of <?php getCustomField('title'); ?>' class="shop_thumb" />
<div class="shop_content">
<h4><a href="<?php getCustomField('link_to_project_page'); ?>">
<?php getCustomField('title'); ?>
</a></h4>
<?php getCustomField('duration'); ?>
<?php getCustomField('paypal_code'); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php posts_nav_link(); ?>
</div>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
</div>
<div id="sidebars">
<div id="sidebar_1" class="sidebar">
<ul class="sidebar_list">
<li class="widget">
<div class="widget_box">
<?php dynamic_sidebar(5); ?>
</div>
</li>
</ul>
</div>
</div>
<?php } }
Based on the above help, I just just got it working with this:
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($page == 1) {
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged=$page');
} else {
$numposts = get_option('posts_per_page');
// work out offset
$offset = (($page -1) * $numposts); // i.e. page 2 - 1 = 1 * 10 (10 for the number of posts)
query_posts("tag=shop&orderby=title&order=ASC&offset=$offset&paged=$page&showposts=$numposts");
}
if (have_posts()) : ?>
You're manually calling query_posts(), which will overwrite any variables related to fetching posts that wordpress tries to send itself. If you want to keep the query string that it's already sending, you should concatenate to it instead of replacing it:
query_posts($query_string.'&tag=shop&orderby=title&order=ASC&posts_per_page=10');
Alternatively, if you only want to include the "page" variable, include it with $paged:
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged='.$paged);