How to Achieve Something Like This In WordPress - php

Hey there, I'm trying to create a page showing specific pages (hope that makes sense), probably by calling their post ID's or something.
I want to pull in the page thumbnail/featured image, the page title, the page's description, and then a link to that page.
Something along the lines of this.
<ul>
<li>
<?php the_post_thumbnail(); ?>
<h2>Page Title</h2>
<p>Page Description</p>
Link to page
</li>
</ul>
Any help will be appreciated, thanks in advance.
UPDATE: At the moment I've got something like this. Using a custom field to bring in the description. I'm still trying to work out how I'd only show pages that are under a parent page called "Culture".
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
UPDATE 2: Solved it! Used the following if anyone's interested.
Pulled in all subpages from parent page (id=7).
Then the post thumbnail, followed by the page title, description using a custom field called description and finally the permalink.
Hope this helps anyone in a similar situation.
<?php query_posts('post_type=page&post_parent=7'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('culture-page-listing'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>

I wrote a loop in WP some time ago which I'm sure isn't perfect, but it did basically something like that (delimited by categories).
http://www.kyleboddy.com/2010/10/14/wordpress-code-attachment-category-loop/
<?php
$areas = array(1 => 'Seattle','East Side & Mercer Island','North Side','South Side');
$slugs = array(1 => 'seattle-jobs','east-side-and-mercer-island-jobs','north-end-jobs','south-end-and-west-seattle-jobs');
$i = count($areas);
$n = 1;
while ($n <= $i)
{
global $post;
$myposts = get_posts('numberposts=-1&offset=0&category_name=' . $slugs[$n]);
echo '<div id="imageList">';
echo '<a name="' . $areas[$n] . '"></a><h2>' . $areas[$n] . '</h2>';
echo '<table id="ourwork"><tr>';
$x = 1;
foreach($myposts as $post)
{
setup_postdata($post);
echo '<td>';
$args = array(
'post_type' => 'attachment',
'numberposts' => '-1',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
$y = count($attachments);
$y--;
echo '<a href="' . $post->guid . '">';
echo wp_get_attachment_image($id = $attachments[$y]->ID, $size=array(200,133), $icon = false);
echo '<strong><br><br>';
echo apply_filters('the_title', $attachments[$y]->post_title);
echo '</strong></a>';
echo '</td>';
if ($x == 4)
{
echo '</tr><tr>';
$x = 0;
}
$x++;
}
}
echo '</tr></table>';
echo '</div><div class="blog"></div>';
$n++;
}

Related

Echo a class if current page matches the li name in wordpress

I'm trying to find some logic that will echo a class only if the current page title matches the title in the li. I'm having trouble coming up with an 'if' statement that won't apply to all the li in this query.
This is in wordpress.
Here's the context:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" <?php if(NEED LOGIC HERE) echo 'class="current"'; ?>><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); endif; ?>
Any help would be much appreciated!
If you know that you are on a single of some sort (ie. a page, post or similar), then I suppose you could save the ID of that before your custom loop and compare to that:
global $post;
//Make sure you are on a single, otherwise you'll get funny results
if (is_single()) {
$saved_id = $post->ID;
} else {
$saved_id = false;
}
if (have_posts()) {
while (have_posts()) {
the_post();
echo '<li><a href="' . get_the_permalink() . '"';
if($saved_id === $post->ID) {
echo 'class="current"';
}
echo '>';
the_title();
echo '</a></li>';
}
wp_reset_query();
}
...sorry for changing the code style completely, I couldn't make it readable using the template tags.
Oh, and of course you could compare the titles instead if that is actually more desirable (though obviously less precise), just use get_the_title() or $post->post_title instead of $post-ID.
Ok, I figured it out. I needed to go outside the loop to create a valid if statement that read the link in the query AND the current page. Notice the $titleID.
<?php
$titleID = (get_the_title());
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parentID = (wp_get_post_parent_id( $post_ID ));
$args= array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $parentID,
'paged' => $paged
);
query_posts($args);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" <?php if(get_the_title() == $titleID ) echo 'class="current"'; ?>><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); endif; ?>

get_the_date() not repeating date

<div class="page-content">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php the_title(); ?>
<br />
<?php
the_content();
echo get_the_date('l,d');
endwhile;
endif;
?>
</div>
This example is not repeating the date for each post. I am using the code in a page template, namely services-template.php and I have read the note that states get_the_date() will repeat the date for each post. Can someone help me figure out what I am doing wrong?
I could not make the original code work, I do not know all the internal workings of Wordpress but my guess is that what ever variable holds post data (most likely $post) was empty. The following code did what I was looking to do. Again I am using this code on a custom page. services-template.php.
$args = array( 'category' => '4' );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><h2>' . get_the_title() . '</h2></li>';
echo '<li>' . get_the_date() . '</li>';
echo '<li>' . get_the_excerpt() . '</li>';
the_tags();
echo '<br /><br />';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
;
?>

How to add any text box after every 5 latest posts in wordpress

I have updated 20 posts in wordpress theme. And i need to add my own text box after every 5 posts. So that i can add 4 text box after every 5 post.
I think it can be done by $post_counter please any one give me a query for my question.
My code simply seems to,
<?php
query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
I believe this is what you need
<?php
$query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
$p = 1;
while ( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
$p++;
?>
<?php endwhile; ?>
I read your comment and i updated #zameerkhan code.
<?php
$query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
$p = 1;
while ( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
//this will create text box after 5 post with name mytext1,mytext2 etc.
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
$p++;
?>
<?php endwhile; ?>
You should never use query_posts, forget that it exists or ever existed. Use WP_Query or pre_get_posts
FROM THE CODEX ON query_posts
Note: This function isn't meant to be used by plugins or themes. As
explained later, there are better, more performant options to alter
the main query. query_posts() is overly simplistic and problematic way
to modify main query of a page by replacing it with new instance of
the query
Your code should look like this
$args = array(
'order' => 'DESC',
'posts_per_page'=>20,
'orderby' => 'post_date',
);
$the_query = new WP_Query( $args );
$p = 1;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
//this will create text box after 5 post with name mytext1,mytext2 etc.
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
$p++;
?>
<?php endwhile; ?>
From your code
<?php
query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
$p++;
?>
<?php endwhile; ?>
Instead of <input type="text" name="mytext[]"/> i replace <h3>This is First text box</h3>
So, now my output is,
Title 1Title 2Title3Title4Title5This is First text box
Title 6Title 7Title 8Title 9Title 10This is First text box
Title 11Title 12Title 13Title 14Title 15This is First text box...
Here see that "This is First text box" returns 4 times. My question is, i need after Title 10 the text should display as "This is Second text box" and so on.. I think now its clear

List Wordpress categories in sidebar with image

I'm to list categories with images in the sidebar this is how I do it (and it works) I do this because I've certain categories I do not want to display!
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php echo get_cat_name(12); ?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
but I need to copy past that code for every single category... and all this code for only changing a number is a good practice I guess. Is there another way it could be done?
I've tried with a foreach but it seems to be wrong
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php foreach($latests as $latest) :?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
Well, You can do it like this:
<ul>
<?php
$cat_args=array(
// 'include' => '3,6,9', // display only these categories
'exclude' => '3,6,9', // display all categories except categories 3,6,9
'orderby' => 'name', // the order
'order' => 'ASC' // asc or desc
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => 2, // how many posts you want to display
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<h3> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in: %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
foreach($posts as $post) {
setup_postdata($post);
?>
<li>
<div>
<div><?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?></div>
<div><?php the_title(); ?></div>
</div>
</li>
<?php
} // close foreach
} // close if
} // close foreach
?>
</ul>
The easiest way is like that
<?php wp_list_categories('orderby=name&exclude=3,5,9,16'); ?>
So, this will return you all the categories excluding the one you specified. After this, you can get the actual image you want for your categories and all.

CSS Styling within some Wordpress PHP?

I have a wordpress code that I want to use, but I am trying to give it some styling.
<?php global $post;
$current_post = $post; // remember the current post
for($i = 1; $i <= 1; $i++):
$post = get_previous_post(); // this uses $post->ID
setup_postdata($post);
// do your stuff here
the_excerpt();
endfor;
$post = $current_post; // restore ?>
Namely where it says //do your stuff here, I need to add the permalink and CSS styling to the_excerpt. Currently the_excerpt pulls up thumbnails on my blog.
This is the code I use for getting 2 random thumbnails, hopefully this will give you an idea of what I want for the above code.
<?php
$args = array( 'numberposts' => 2, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<div class="postgallery" id="post-<?php the_ID(); ?>">
<a class="title" href="<?php the_permalink() ?>" rel="bookmark"><?php the_excerpt(); ?>
<?php
$thetitle = $post->post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 35;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a><?php if ($options['tags']) : ?><?php endif; ?>
</div>
<?php endforeach; ?>
Wrap the_excerpt() in a set of div tags with an id or class and that will give you the hooks you need to style the content of the excerpt via the stylesheet.

Categories