PDA

View Full Version : Wordpress coding help please


SFD
11th March 2010, 17:24
I am currently using a plugin on wordpress, the code is:

<?php
/*
Plugin Name: List category posts
Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/
Description: List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments. Usage: [catlist argument1=value1 argument2=value2].
Version: 0.8.1
Author: Fernando Briano
Author URI: http://fernandobriano.com/
*/

/* Copyright 2008-2010 Fernando Briano (email : fernando@picandocodigo.net)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

//Sidebar Widget:
include('list_cat_posts_widget.php');
//Filters and actions:
//add_action('plugins_loaded', 'lcp_load_widget');

//Shortcode [catlist parameter="value"]
function catlist_func($atts, $content=null) {
$atts=shortcode_atts(array(
'id' => '0',
'name' => 'default',
'orderby' => 'date',
'order' => 'desc',
'numberposts' => '5',
'date' => 'no',
'author' => 'no',
'dateformat' => get_option('date_format'),
'template' => 'default',
'excerpt' => 'no',
'exclude' => '0',
'excludeposts' => '0',
'offset' => '0',
'content' => 'no',
'catlink' => 'no'
), $atts);
return list_category_posts($atts);
}
add_shortcode('catlist', 'catlist_func');

function list_category_posts($atts){
if($atts['name']!='default' && $atts['id']=='0'){
$category = 'category_name=' . $atts['name'];
$category_id = get_cat_ID($atts['name']);
}else{
$category = 'cat=' . $atts['id'];
$category_id = $atts['id'];
}

//Link to the category:
$cat_link_string = '';
if ($atts['catlink'] == 'yes'){
$cat_link = get_category_link($category_id);
$cat_data = get_category($atts['id']);
$cat_title = $cat_data->name;
$cat_link_string = '<a href=' . $cat_link . ' title="' . $cat_title . '">' . $cat_title . '</a>';
}
//Build the query for get_posts()
$catposts = get_posts($category.'&numberposts=' . $atts['numberposts'] .
'&orderby=' . $atts['orderby'] .
'&order=' . $atts['order'] .
'&exclude=' . $atts['excludeposts'] .
'&offset=' . $atts['offset'] );
//Template code:
$tplFileName = $atts['template'] != 'default'?dirname(__FILE__).'/templates/'.$atts['template'].'.php' : null;
if ((!empty($tplFileName)) && (is_readable($tplFileName))) {
require($tplFileName);
}else{
if ($cat_link_string != ''){
$output = '<p><strong>' . $cat_link_string . '</strong></p>';
}else{
$output = '';
}
$output .= '<ul class="lcp_catlist">';//For default ul
foreach($catposts as $single):
$output .= '<li><a href="' . get_permalink($single->ID).'">' . $single->post_title . '</a>';
if($atts['date']=='yes'){
$output .= ' - ' . get_the_time($atts['dateformat'], $single);//by Verex, great idea!
}
if($atts['author']=='yes'){
$lcp_userdata = get_userdata($single->post_author);
$output.=" - ".$lcp_userdata->user_nicename . '<br/>';
}
if($atts['content']=='yes' && $single->post_content){
$output .= "<p>$single->post_content</p>";
}
if($atts['excerpt']=='yes' && $single->post_excerpt && !($atts['content']=='yes' && $single->post_content) ){
$output .= "<p>$single->post_excerpt</p>";
}
$output.="</li>";
endforeach;
$output .= "</ul>";
}
return $output;
}

function lcp_add_option_page(){
add_options_page('List Category Posts', 'List Category Posts', 'manage_options','list-category-posts/list_cat_posts_options.php');
}

?>The plugin allows me to list all the pages from a specific category on a static page, such as 'About Us'

The link anchor text is the title of the post.

For each of my posts I also have a thumbnail image associated, this is called in other parts of my site by:

<?php the_post_thumbnail(); ?>Please can someone show me how to alter the above code so only the thumbnail is displayed and links through to the relevant post.

I've been working on this today and have managed to display thumbnails next to my posts and also on related posts but am struggling with this.

Thanks a lot

RBS
11th March 2010, 17:35
Have you tried Wordpress official forum?
http://wordpress.org/support/

Have you tried asking plugin developer?

Kev Jaques
11th March 2010, 17:42
This should work


change line
$output .= '<li><a href="' . get_permalink($single->ID).'">' . $single->post_title . '</a>';
to
$output .= '<li><a href="' . get_permalink($single->ID).'">' . get_the_post_thumbnail($single->ID) . '</a>';


and you will probably need to use this function as you already have the id, you can pass it by value
get_the_post_thumbnail( $post_id = NULL, $size = 'post-thumbnail', $attr = '' )

You will be losing out on internal linking with anchor texts, you sure you want to do that?
I can't remember if that just returns the img path, so you may need to wrap it in an img tag

SFD
11th March 2010, 17:48
Cheers Kev, you're a star, works perfectly.

I'm only using it for a couple of landing pages, the rest of the website navigation is text.

Kev Jaques
11th March 2010, 17:57
nps :)
I would add in the title attr too, so you can still use the title on hover of the thumbnail.