---
title: "WordPress Category Posts modifiering"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/185207-wordpress-category-posts-modifiering"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "McD"
published: "2010-11-11T13:06:17.000Z"
updated: "2010-11-21T12:31:57.000Z"
replies: 8
views: 834
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/185207-wordpress-category-posts-modifiering"
---

# WordPress Category Posts modifiering

## #1 — McD, 2010-11-11T13:06Z

Hej

Skulle behöva ha lite hjälp av er php kunniga personer här... Jag skrev ett inlägg i wp-support.se forumet men det känns som om man där mer får hjälp med olika scirpt än att koda själv.

Jag har detta scirpt...

```php
<?php
/*
Plugin Name: WordPress Category Posts
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/
Description: List the posts in a specific category
Author: Watershed Studio, LLC
Version: 2.0
Author URI: http://watershedstudio.com/
*/ 

function wp_cat_posts( $catID = 0 ) {
$catID = (int) $catID;
$posts = get_posts(array('category' => $catID, 'numberposts' => -1, 'order' => ASC, 'orderby' => title));

foreach( (array) $posts as $post ) {
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />';
}
}

?>
```

**Jag skulle vilja få hjälp att addera 3 saker i detta**

1\.
Att poster visas från kategori 1, 2 och 4 när man skriver \<?php wp_cat_posts(4); ?\> så visas nu bara poster i kategori 4.

2\.
Endast de 10 senaste posterna ska visas inte fler.. nu visas alla poster i den kategori man väljer.

3\.
Sortera dem efter nyaste inlägg först i de valda kategorierna. idag så sorteras de efter bokstavsorning på subject.

**Bonus hjälp och guldstjärna!**

4\. Lägga till en funktion som gör att endast x antal tecken visas i ett subject och en ersätts med ... om det finns fler tecken.

Kolla min hemsida så förstår ni var detta ska in <https://www.mcdz.net>

Ett jätte tack på förhand.

Permalänk: https://www.webforum.nu/p/185207

## #2 — zAPF, 2010-11-14T01:27Z

Har inte testat men dessa ändringar borde funka

För att visa posts från flera kategorier så borde det funka med:

```php
<?php wp_cat_posts(array(1,2,4)); ?>
```

Antalet posts ändrar du nog genom att ändra

```php
'numberposts' => -1,
```

till

```php
'numberposts' => 10,
```

Sen för att få dem o sorteras efter när de skapades så ersätt 

```php
'orderby' => title
```

 med 

```php
'orderby' => date
```

Så den slutgiltiga koden blir

```php
<?php 
/* 
Plugin Name: WordPress Category Posts 
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/ 
Description: List the posts in a specific category 
Author: Watershed Studio, LLC 
Version: 2.0 
Author URI: http://watershedstudio.com/ 
*/  

function wp_cat_posts( $catID = 0 ) { 
$catID = (int) $catID; 
$posts = get_posts(array('category' => $catID, 'numberposts' => 10, 'order' => ASC, 'orderby' => date)); 

foreach( (array) $posts as $post ) { 
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />'; 
} 
} 

?>
```

Permalänk: https://www.webforum.nu/p/2259465

## #3 — McD, 2010-11-15T12:29Z

Hej tack för att du försöker hjälpa mig, nu har jag testat, gick inte helt fram tyvärr..

Din kod gick inte att aktivera i wp blev inte några felmeddelanden i wp heller... bar att det inte gick att aktivera.

Testade dock att ädnra antal och sorteringen och det funkar nu.

```php
<?php 
/* 
Plugin Name: WordPress Category Posts 
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/ 
Description: List the posts in a specific category 
Author: Watershed Studio, LLC 
Version: 2.0 
Author URI: http://watershedstudio.com/ 
*/  

function wp_cat_posts( $catID = 0 ) { 
$catID = (int) $catID; 
$posts = get_posts(array('category' => $catID, 'numberposts' => 15, 'order' => DESC, 'orderby' => date)); 

foreach( (array) $posts as $post ) { 
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />'; 
} 
} 

?>
```

Dock så kvarstår fler kategorier i ovanstående och antal tecken i subject :)

Tack tack i förhand.

Permalänk: https://www.webforum.nu/p/2259570

## #4 — zAPF, 2010-11-17T01:49Z

Problemet här är väll min bristande kunskap i php... :p
Men testa denna kod för att få ner längden på titlarna....
Garanterar inte att den funkar, o har faktiskt inte testat denna heller...
(orkar inte lägga upp en test wordpress sajt...)

```php
<?php  
/*  
Plugin Name: WordPress Category Posts  
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/  
Description: List the posts in a specific category  
Author: Watershed Studio, LLC  
Version: 2.0  
Author URI: http://watershedstudio.com/  
*/  

function snippet($text,$length=xXxXxXx,$tail="...") { 
    $text = trim($text); 
    $txtl = strlen($text); 
    if($txtl > $length) { 
        for($i=1;$text[$length-$i]!=" ";$i++) { 
            if($i == $length) { 
                return substr($text,0,$length) . $tail; 
            } 
        } 
        $text = substr($text,0,$length-$i+1) . $tail; 
    } 
    return $text; 
}

 

function wp_cat_posts( $catID = 0 ) {  
$catID = (int) $catID;  
$posts = get_posts(array('category' => $catID, 'numberposts' => 15, 'order' => DESC, 'orderby' => date));  
$post_title = snippet(strip_tags($post->post_title));
foreach( (array) $posts as $post ) {  
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />';  
}  
}  

?>
```

Ta bort xXxXxXx o ersätt med antalet tecken du vill ha.

Permalänk: https://www.webforum.nu/p/2259757

## #5 — McD, 2010-11-17T15:37Z

Det hände inte något när jag la in koden, ändrade xxxx till 5 bara för att testa... samt ändrade antal bara för att se att något hände... antalet ändras men inga ... efter 5 tecken.

```php

<?php   
/*   
Plugin Name: WordPress Category Posts   
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/   
Description: List the posts in a specific category   
Author: Watershed Studio, LLC   
Version: 2.0   
Author URI: http://watershedstudio.com/   
*/   

function snippet($text,$length=5,$tail="...") {  
    $text = trim($text);  
    $txtl = strlen($text);  
    if($txtl > $length) {  
        for($i=1;$text[$length-$i]!=" ";$i++) {  
            if($i == $length) {  
                return substr($text,0,$length) . $tail;  
            }  
        }  
        $text = substr($text,0,$length-$i+1) . $tail;  
    }  
    return $text;  
} 

  

function wp_cat_posts( $catID = 0 ) {   
$catID = (int) $catID;   
$posts = get_posts(array('category' => $catID, 'numberposts' => 5, 'order' => DESC, 'orderby' => date));   
$post_title = snippet(strip_tags($post->post_title)); 
foreach( (array) $posts as $post ) {   
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />';   
}   
}   

?>
```

Permalänk: https://www.webforum.nu/p/2259821

## #6 — tronez, 2010-11-17T16:28Z

```php
<?php
/*
Plugin Name: WordPress Category Posts
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/
Description: List the posts in a specific category
Author: Watershed Studio, LLC
Version: 2.0
Author URI: http://watershedstudio.com/
*/ 

function truncate ($str, $length=10, $trailing='...')
{
/*
** $str -String to truncate
** $length - length to truncate
** $trailing - the trailing character, default: "..."
*/
	  // take off chars for the trailing
	  $length-=mb_strlen($trailing);
	  if (mb_strlen($str)> $length)
	  {
		 // string exceeded length, truncate and add trailing dots
		 return mb_substr($str,0,$length).$trailing;
	  }
	  else
	  {
		 // string was already short enough, return the string
		 $res = $str;
	  }
	  return $res;
}

function wp_cat_posts( $catID = 0 ) {
$catID = (int) $catID;
$posts = get_posts('numberposts=10&category=1,2,'.$catID.'&order=DESC&orderby=date');

foreach( (array) $posts as $post ) {
$title = truncate($post->post_title, 10, '...');
echo '<a href="' . get_permalink($post->ID) . '">' . $title . '</a><br />';
}
}

?>
```

Helt oprövad, men jag tror att jag skulle försöka mig på något sådant. Får la hoppas det funkar :)

Permalänk: https://www.webforum.nu/p/2259828

## #7 — McD, 2010-11-17T21:15Z

Grymt det funkar bra med antal tecken tack tack...:)

kategorierna buggar dock testade

```php
('numberposts=21&category=4,5,6,'.$catID.'&order=DESC&orderby=date');
```

men visar bara poster från kategori 4 dvs läser fortfarande bara 

```php
<?php wp_cat_posts(4); ?>
```

 som jag har i koden på sidan där jag läser ut allt.

tack att ni orkar försöka hälpa mig det uppskattas.

Permalänk: https://www.webforum.nu/p/2259847

## #8 — tronez, 2010-11-20T13:22Z

```php
<?php
/*
Plugin Name: WordPress Category Posts
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/
Description: List the posts in a specific category
Author: Watershed Studio, LLC
Version: 2.0
Author URI: http://watershedstudio.com/
*/ 

function truncate ($str, $length=10, $trailing='...')
{
/*
** $str -String to truncate
** $length - length to truncate
** $trailing - the trailing character, default: "..."
*/
      // take off chars for the trailing
      $length-=mb_strlen($trailing);
      if (mb_strlen($str)> $length)
      {
         // string exceeded length, truncate and add trailing dots
         return mb_substr($str,0,$length).$trailing;
      }
      else
      {
         // string was already short enough, return the string
         $res = $str;
      }
      return $res;
}

function wp_cat_posts( $catID = 0 ) {
$catID = (int) $catID;

query_posts('cat=4,5,6,'.$catID.'&posts_per_page=10&orderby=date&order=DESC');

if ( have_posts() ) : while ( have_posts() ) : the_post();
  $title = truncate($post->post_title, 10, '...');
  echo '<a href="' . get_permalink($post->ID) . '">' . $title . '</a><br />';
endwhile;
 
endif;
wp_reset_query();

}
?>
```

Prova detta och se.

Permalänk: https://www.webforum.nu/p/2260056

## #9 — McD, 2010-11-21T12:31Z

Fick hjälp från annat håll nu fungerar det :)

Tack för er hjälp.

```php
<?php 
/* 
Plugin Name: WordPress Category Posts 
Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/ 
Description: List the posts in a specific category 
Author: Watershed Studio, LLC 
Version: 2.0 
Author URI: http://watershedstudio.com/ 
*/  

function truncate ($str, $length=48, $trailing='...') 
{ 
/* 
** $str -String to truncate 
** $length - length to truncate 
** $trailing - the trailing character, default: "..." 
*/ 
      // take off chars for the trailing 
      $length-=mb_strlen($trailing); 
      if (mb_strlen($str)> $length) 
      { 
         // string exceeded length, truncate and add trailing dots 
         return mb_substr($str,0,$length).$trailing; 
      } 
      else 
      { 
         // string was already short enough, return the string 
         $res = $str; 
      } 
      return $res; 
} 

function wp_cat_posts( $catID = 0 ) { 
$catID = (int) $catID; 
$posts = get_posts(array('category__in' => array(4,5,6), 'numberposts' => 16, 'order' => 
DESC, 'orderby' => date));  

foreach( (array) $posts as $post ) { 
$title = truncate($post->post_title, 48, '...'); 
echo '<a href="' . get_permalink($post->ID) . '">' . $title . '</a><br />'; 
} 
} 

?>
```

// Nille

Permalänk: https://www.webforum.nu/p/2260147

---

Tråden på webben: https://www.webforum.nu/amne/php/185207-wordpress-category-posts-modifiering
