webForumDet fria alternativet

WordPress Category Posts modifiering

8 svar · 818 visningar · startad av McD

McDMedlem sedan mars 20002 137 inlägg
#1

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
/*
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.

zAPFMedlem sedan nov. 20105 inlägg
#2

Har inte testat men dessa ändringar borde funka

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

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

Antalet posts ändrar du nog genom att ändra

'numberposts' => -1,

till

'numberposts' => 10,

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

'orderby' => title

med

'orderby' => date

Så den slutgiltiga koden blir

<?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 />'; 
} 
} 

?>
McDMedlem sedan mars 20002 137 inlägg
#3

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 
/* 
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.

zAPFMedlem sedan nov. 20105 inlägg
#4

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  
/*  
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.

McDMedlem sedan mars 20002 137 inlägg
#5

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   
/*   
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 />';   
}   
}   

?>
tronezMedlem sedan juli 2001657 inlägg
#6
<?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 :)

McDMedlem sedan mars 20002 137 inlägg
#7

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

kategorierna buggar dock testade

('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 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.

tronezMedlem sedan juli 2001657 inlägg
#8
<?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.

McDMedlem sedan mars 20002 137 inlägg
#9

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

Tack för er hjälp.

<?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

Genererad på 517 ms · cache AV · v20260730165559-full.f96bc7eb