New to php, but I've learned how it works (I mean, I can write php and do the things in the YouTube tutorials). I'm having difficulty applying it to actual use though:
I want to shorten the amount of php I need to put on frontpage.php (WordPress, but I hear this question is not a WordPress question; just a php one).
I'm going to call the same php many times to display 1 post each time, just changing the category to display in the php - so, now I have:
lots of php cat=33 lots of php
I want to make a function so that on my frontpage.php, I just need to write:
whatever cat=33 whatever
or just
whatever 33
If it helps to have my code, it is (this has tag, but I use both tag and cat here and there):
<?php
$args=array(
'tag' => 'feature-left',
'showposts'=>1,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
$offset = 3;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting" itemprop="blogPost">
<h4><?php the_excerpt(); ?> </h4>
</a>
<a href="<?php the_permalink() ?>" >
<div class="front-first-article-title" itemprop="headline"><h2 style="color:#00589C; margin-bottom:5px;"><b><?php the_title(); ?></b></h2>
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
In your theme folder, find your functions.php file.
In that file, place this code (from your question) into a function, like so:
function my_custom_loop($category) {
$args=array(
'tag' => 'feature-left',
// showposts has been replaced, use 'posts_per_page' instead
// 'showposts' =>1,
'posts_per_page' => 1,
// this has been replaced, use 'ignore_sticky_posts'
// 'caller_get_posts' => 1,
'ignore_sticky_posts' => true,
'cat' => $category
);
$my_query = new WP_Query($args);
$offset = 3;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting" itemprop="blogPost">
<h4><?php the_excerpt(); ?> </h4>
</a>
<a href="<?php the_permalink() ?>" >
<div class="front-first-article-title" itemprop="headline"><h2 style="color:#00589C; margin-bottom:5px;"><b><?php the_title(); ?></b></h2>
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
}
Now, in your home page file, you can drop in my_custom_loop(33) to have it output your custom post loop.
NOTE
There's a few issues with your HTML inside your loop. You shouldn't put <div> or <h2> or <h4> elements inside of an <a> tag. Also, your <a> tag is not getting closed properly. Lastly, I'd suggest using classes / CSS rather than inline styles on your <h2>, as it's going to be output to the screen many times, and it's sorta silly to output the exact same inline CSS a bunch of times.
EDIT
Per your recent comments, yes, you could make the function also handle tags, and have the "number" be dynamic. Note that there are a variety of approaches to this sort of issue, but the most direct (given your existing function) would be something like so:
/* Note the "default values" for $tag and $offset.
* You can call this function in many ways:
* my_custom_loop(33); just get the categories, with an offset of 3
* my_custom_loop(33, NULL, 5); get the categories, offset of 5
* my_custom_loop(NULL, 'feature-left'); get the tags, offset of 3
* my_custom_loop(NULL, 'feature-left', 5); get the tags, offset of 5
*/
function my_custom_loop($category, $tag = NULL, $offset = 3) {
$args=array(
// showposts has been replaced, use 'posts_per_page' instead
// 'showposts' =>1,
'posts_per_page' => 1,
// this has been replaced, use 'ignore_sticky_posts'
// 'caller_get_posts' => 1,
'ignore_sticky_posts' => true,
);
if ($category) {
$args['cat'] = $category;
}
if ($tag) {
$args['tag'] = 'feature-left';
}
$my_query = new WP_Query($args);
// ... rest of function to output loop
}
if you dont want your functions to be crowded you can create folder lets call it templates/parts/
for each part you want to reuse put inside one .PHP file .
then any where you need this block you call it
<?php include(locate_template( 'templates/parts/slider-box.php' ));?>
this is better from design prospective . for example the main page for one website I am building looks like this
<?php $catNum = 23; ?>
<?php include(locate_template( 'templates/parts/slider-box.php' ));?>
<?php include(locate_template( 'templates/parts/ads3.php' ));?>
<?php $catNum = 2; ?>
<?php include(locate_template( 'templates/parts/catBlox-main.php' ));?>
<?php $catNum = 4; ?>
<?php include(locate_template( 'templates/parts/catBlox-main.php' ));?>
<?php $catNum = 3; ?>
<?php include(locate_template( 'templates/parts/catBlox-main.php' ));?>
and I have all the files for each parts in one folder I can move them to other project or when there is problem it easer to go to file other than go to function in 1 big function.php
Related
I am trying to display all the posts with the same category in WordPress however its not displaying correctly and instead is just showing everything.
Here is the php code:
<?php
$related = get_posts( array(
'category_in' => wp_get_post_categories($post->ID),
'numberposts' => 3,
'post_not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h3><?php the_title();?></h3>
</a>
</div>
<?php }
wp_reset_postdata();
?>
Its taken from here: https://wordpress.stackexchange.com/questions/41272/how-to-show-related-posts-by-category
And if it helps here is the link to the website in question where the code isnt working:http://u1f8aki.nixweb23.dandomain.dk/cat-4-post-test/
The code in question is further down the page under the red text. You can see the category at the top in the breadcrumbs.
There are quite errors in your code, and I'm sure atleast one of them are the culprit in giving you the wrong result.
I've refactored your code with some comments explaining what and why has been changed:
<?php
// For readability, save our categories in a variable for later use.
// $post->ID has been replaced with get_the_ID(), $post might not be accessible depending if you're exposing $post as a global or not.
$categories = wp_get_post_categories(get_the_ID());
/*
Instead of using get_posts(), use the recommended Wordpress loop in the form of WP_Query().
We start by defining our arguments for the loop
*/
$args = array(
'category_in' => $categories, // here we use variable for readability
'posts_per_page' => 3, //numberposts and posts_per_page has the same function, but posts_per_page is the more common of the two (IMO)
'post__not_in' => array(get_the_ID()) // you were missing a '_', ie post_not_in instead of post__not_in
);
// Start the loop
$query = new WP_Query($args);
if($query->have_posts()): while($query->have_posts()): $query->the_post();
// No need to setup or reset postdata when using this method, it does it for you!
?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php
// the_title() actually takes opening tag and closing tags as arguments in its function. So add the <h3> code like this.
the_title('<h3>', '</h3>');
?>
</a>
</div>
<?php endwhile; endif; ?>
Long story short, your code prolly isn't working because of the miss-named arguments. If you don't feel like replacing your code with my example, just change your arguments from numberposts to posts_per_page, and post_not_in to post__not_in.
If it still isn't working, check what wp_get_post_categories(get_the_ID()) is returning for each post, and make sure all posts aren't sharing some category you missed.
Edit: numberposts is actually a valid argument, changed my answer to reflect this.
Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>
This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>
I have a loop on my homepage showing 3 recent blog posts, this is being included in from the footer. If I go elsewhere on the site though such as a different blog post, the 'the_permalink' is instead of grabbing the featured 3 blog post links, it's grabbing the link from the page your currently on.
These 3 blog posts will change as new ones are added so I cannot define that the link be only a specific blog post(s).
Here is the code showing the first blog post. Found within footer.php and therefore shows on each page.
<div id="news-container-1">
<?php
// Create a variable to hold our custom Loop results
$excludes = array('4135');
$frontpageposts = get_posts( array(
'numberposts' => 1, // only the 3 latest posts
'post__not_in' => $excludes
) );
// Create output only if we have results
// Customize to suit your HTML markup
if ( $frontpageposts ) {
foreach ( $frontpageposts as $fppost ) {
// setup postdata, so we can use template tags
setup_postdata($fppost);
?>
<div <?php post_class(); ?>>
<h4><?php //the_title(); ?>Latest News #1</h4>
<div class="post-entry">
<?php
$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
/*Run preg_match_all to grab all the images and save the results in $pics*/
preg_match_all( $searchimages, $content, $pics );
// Check to see if we have at least 1 image
$iNumberOfPics = count($pics[0]);
if ( $iNumberOfPics > 0 ) { ?>
<!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
<img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
<?php }
?>
<?php //the_excerpt(); ?>
</div>
</div>
<?php }
}
?>
</div>
the_permalink() doesn't have any parameters so remove the '1'.
You need to change the way you're setting up the posts. setup_postdata() is currently being used incorrectly.
if ( $frontpageposts ) {
global $post;
foreach ( $frontpageposts as $post ) {
// setup postdata, so we can use template tags
setup_postdata( $post );
Then at the end of your foreach loop you need to reset the post object.
Change this:
<?php }
}
To this:
<?php }
wp_reset_postdata();
}
I'm attempting to have two loops on my archive-custom.php (it's for a custom post type) - one loop for featured post(s) and another for the rest of the posts.
This is the code I have come up with, however, it's not working correctly. At the moment, it doesn't display either loop and ends up actually breaking other PHP based elements.
Note: These loops are split up into different template parts - not sure if that matters or not. However, I have combined them into one chunk to make it easier to troubleshoot.
<?php $args = array (
'post_type' => 'community',
'category_name' => 'featured',);
// The Query
$community_posts_featured = new WP_Query( $args );
if ($community_posts_featured->have_posts()) : while ($community_posts_featured->have_posts()) : $community_posts_featured->the_post(); ?>
<div id="featured">
<--Featured Stuff Here-->
<?php the_content(); ?>
</div><!--End #featured-->
<?php endwhile; ?>
<?php $args = array (
'post_type' => 'community', );
// The Query
$community_posts = new WP_Query( $args );
if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
<div id="main-content">
<--Main Stuff Here-->
<?php the_content(); ?>
</div><!--#End Main-->
<?php endwhile; ?>
<?php else : ?>
<--Missing Content Stuff-->
<?php endif; ?>
There i can spot two problems:
1) You have opened 2 if statements and have just closed one of them
2) you'd better use wp_reset_query(); after the first loop