Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Pages: [1] 2

Author Topic: [MOD] MyPosts, or Vasile's Menu Articles With Intro Text for 1.7 with subcats  (Read 5552 times)

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal

The original of this mod, created for sNews 1.6 by Vasile Rusnac, can be found here: http://snewscms.com/forum/index.php?topic=5780.0
This is a modified version for 1.7, with additions by equilni to make it look in subcategories for articles.

Name: MyPosts
Version: 1
sNews version: 1.7 official
Credits: Vasile Rusnac, Keyrocks, philmoz, (for the original) and equilni (for the subcat modifications). Oh, and little ol' me... :D
.................................................................................. / * / ..................................................................................

What It Does
This mod allows you to pull in a defined number of articles with a defined portion of the article content from categories and subcategories into specific containers outside of the normal article function. You can put them in a side bar, you can use them in extra content. You can basically have them anywhere you want on your site, and as many instances as you want. Usage is very simple. You can see a rudimentary example of how it works at my new site -- the Feature articles in the top section are pulled in from various categories with this mod. /*Edit (I've had to pull the top slot for the time being due to script collisions causing it to look bad in old Firefoxes. The shortened articles will be back soon-ish though.) */

The Mod Code
You can use the following code in two ways: either (a) save it to an external php file and then include that php file at the start of your index.php, like so:
Code: [Select]
<?php include('myposts.php'); include('snews.php'); ?>("myposts.php" would be the mod file, you can call it whatever you want obviously.)
Or (b) paste the mod into snews.php. Both ways work well.

Note: If you paste into snews.php, make sure to remove the opening and closing <?php-tags (at the top and bottom of the mod code)
The following instructions assume you use the external file option.

Ok, the code. But first:

0. Back up your files.

1. Copy this code and save to external file:

Code: [Select]
<?php
// MYPOSTS: MODIFIED MENU ARTICLES WITH INTRO TEXT (Original by Vasile Rusnac, modified by Fred K, philmoz and equilni)

function myposts($cat$start$size) {
$catQuery 'SELECT c.id
FROM '
._PRE.'categories'.' AS c '.$join.'
WHERE c.subcat = '
.$cat.' AND c.published = \'YES\'
GROUP BY c.id
ORDER BY c.catorder,c.id'

$catResult mysql_query($catQuery);
$count mysql_num_rows($catResult);
if ($count 0) {
$cR mysql_fetch_assoc($catResult);
$cR implode(',',$cR);
$cat $cat.','.$cR;
}
$cat ' AND category IN('.$cat.')';
$query '
SELECT
title,a.seftitle AS asef,date,text,
c.name AS name, c.seftitle AS csef,
x.name AS xname,x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON c.subcat =  x.id
WHERE position = 1 '
.$cat.'
AND a.published = 1
AND c.published ="YES"
AND (x.published ="YES" || x.published IS NULL) 
ORDER BY date DESC LIMIT '
."$start$size";
$result mysql_query($query);
$count mysql_num_rows($result);
if ( $count ===0) {echo '<p>'.l('no_articles').'</p>';} else {
while ($r mysql_fetch_array($result)) {
$date date(s('date_format'), strtotime($r['date']));
$link = isset($r['xsef']) ? $r['xsef'].'/'.$r['csef'] : $r['csef'];
$text $r['text'];
$finetext strip($text);
echo  '
<div>
<h3><a href="'
._SITE.$link.'/'.$r['asef'].'/" title="('.$date.')">'.$r['title'].' (in '.$r['name'].')</a></h3>
'
.substr($finetext,0,255).' ...</p>
<p><a href="'
._SITE.$link.'/'.$r['asef'].'/" title="Full story">Permalink</a></p>
</div>
'
;
}}}
?>


2. In index.php, alter the top php string to look like this:
Code: [Select]
<?php include('myposts.php'); include('snews.php'); ?>Make sure that the path to the respective file is correct.

3. Wherever you wish to bring in the shortened articles, use this string:
Code: [Select]
<?php myposts(1,0,5); ?>
Another way to use it is via the function button within an article, page or Extra. Like so:
Code: [Select]
[func]myposts:|:1,0,5[/func]

The first number says which parent category you want to find content in -- this assumes that you know the category id. The second number says where you want to start looking -- "0" means root, e.g you want to find any article within the defined category -- and the last number means how many articles you wish to pull in. You can set a specific subcat id here, ex (1,2,5) which means that you'll try and find the five latest articles in cat 1 and the first subcat (if cat id is 1, its first subcat will be 2, the second will be 3 etc.)

The trickiest part of the usage is cat id. The logic is simple: categories are given sequential id's regardless if they're parent or children. In other words, if we have this setup:
Cat 1
-- Subcat 1
-- Subcat 2
Cat 2
Cat 3
-- Subcat 1
-- Subcat 2

The id's for these categories will look like this:
Cat 1 (id: 1)
-- Subcat 1 (id: 2)
-- Subcat 2 (id: 3)
Cat 2 (id: 4)
Cat 3 (id: 5)
-- Subcat 1 (id: 6)

So, using the mod, if I want to pull one article from Cat 1 and subcat 2, I would do this:
Code: [Select]
<?php myposts(1,3,1); ?>
Worth noting about the output: the mod contains a character count limit (255 chars by default). This is the number of source code characters that will be pulled in. It's important to know this, because if you have links, images or other bits that require long html strings, the volume of text that gets published on the page may not be what you expect. So you can modify the character limit. The mod also uses the strip($text) function in snews.php to strip out html and other code tags (like [break]) from the text that is pulled in. If you understand how strip($text) works, you can modify it to suit your needs.

I think that's all. Let me know if I've missed anything. ;)
« Last Edit: December 03, 2008, 06:13:19 AM by Fred K (agentsmith) »
Logged

centered

  • Guest

Code: [Select]
Actually what you are describing here:
[quote]
So, using the mod, if I want to pull one article from Cat 1 and subcat 2, I would do this:
Code:
<?php myposts(1,3,1); ?>

The mod you posted would work if you knew the top level category and wanted all articles from the category and all it's child categories.

I would use the below instead of the option you posted if you want specific categories and you know the id

Code: [Select]
function myposts($cat, $start, $size) {
$cat = explode('+',$cat);
$cat = implode(',',$cat);
$cat = ' AND category IN('.$cat.')';
$query = '
SELECT
title,a.seftitle AS asef,date,text,
c.name AS name, c.seftitle AS csef,
x.name AS xname,x.seftitle AS xsef
FROM '._PRE.'articles AS a
LEFT OUTER JOIN '._PRE.'categories as c
ON category = c.id
LEFT OUTER JOIN '._PRE.'categories as x
ON c.subcat =  x.id
WHERE position = 1 '.$cat.'
AND a.published = 1
AND c.published ="YES"
AND (x.published ="YES" || x.published IS NULL)
ORDER BY date DESC LIMIT '."$start, $size";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if ( $count ===0) {echo '<p>'.l('no_articles').'</p>';} else {
while ($r = mysql_fetch_array($result)) {
$date = date(s('date_format'), strtotime($r['date']));
$link = isset($r['xsef']) ? $r['xsef'].'/'.$r['csef'] : $r['csef'];
$text = $r['text'];
$finetext = strip($text);
echo  '
<div class="snippet">
<h4><a href="'._SITE.$link.'/'.$r['asef'].'/" title="('.$date.')">'.$r['title'].' (in '.$r['name'].')</a></h4>
'.substr($finetext,0,255).' ...</p>
<p><a href="'._SITE.$link.'/'.$r['asef'].'/" title="Full story">Permalink</a></p>
</div>
';
}}}

Usage - I want the latest 5 articles from category 1 and subcategory 2:
Code: [Select]
myposts(1+2,0,5);
Logged

Slickwilly789

  • Newbie
  • *
  • Karma: 0
  • Posts: 19

Fred,

Thanks for the point to this function! very powerful.

I made some edits to fit my site's needs, to display all "new" articles in order from newest to oldest. Basically I ported some code from the Menu_Articles function, from snews.php, and ported it into your code. I also edited how the html was parsed to fit my needs. Maybe this might help someone else out who wants to list articles, but not only by their category.

Code: [Select]
<?php
// MYPOSTS: MODIFIED MENU ARTICLES WITH INTRO TEXT (Original by Vasile Rusnac, modified by Fred K, philmoz and equilni)

function myposts([color=red]$start 0$size 5$cat_specific 0) {
global $categorySEF$_catID,$subcatSEF;
switch ($cat_specific){
case :
$subcat = !empty($_catID) && empty($subcatSEF) ? 'AND c.subcat = '.$_catID '';
break;
case :
$subcat = !empty($_catID) ? 'AND c.subcat = '.$_catID '';
break;
default:
$subcat '';
}[/color]
$query '
SELECT
title,a.seftitle AS asef,date,text,
c.name AS name, c.seftitle AS csef,
x.name AS xname,x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON c.subcat =  x.id
WHERE position = 1[color=red](took out '
.$cat.')[/color] 
AND a.published = 1
AND c.published ="YES"
AND (x.published ="YES" || x.published IS NULL) 
ORDER BY date DESC LIMIT '
."$start$size";
$result mysql_query($query);
$count mysql_num_rows($result);
if ( $count ===0) {echo '<p>'.l('no_articles').'</p>';} else {
while ($r mysql_fetch_array($result)) {
$date date(s('date_format'), strtotime($r['date']));
$link = isset($r['xsef']) ? $r['xsef'].'/'.$r['csef'] : $r['csef'];
$text $r['text'];
$finetext strip($text);
echo  '
<div>
<h3><a href="'
._SITE.$link.'/'.$r['asef'].'/" title="('.$date.')">'.$r['title'].' </a></h3>
Posted in '
.$r['name'].'<br />
<p>'
.substr($finetext,0,155).' ... <a href="'._SITE.$link.'/'.$r['asef'].'/" title="Full story">Full Article</a></p>

</div>
'
;
}}}
?>

Replace the
Code: [Select]
<?php myposts(1,5,0); ?>
where ($start = 0, $size = 5, $cat_specific = 0) so you still can list categories if you want, but also display all categories! Again awesome function... its usability is very versatile.I can't seem to highlight the changes inside of the code quote box...
« Last Edit: December 02, 2008, 05:53:53 AM by Slickwilly789 »
Logged

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal

Looks nice, slickwilly! I started on that same idea but the need to get articles from subcats grew on me, so I abandoned it for the other solutions. So now we have three! ;)
Logged

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly

Ah, the power of sNews... it can be what you want of it.

Keep the mods coming :D
Logged
Of all the things I have lost, it is my mind that I miss the most.

Slickwilly789

  • Newbie
  • *
  • Karma: 0
  • Posts: 19

Ah, the power of sNews... it can be what you want of it.

Keep the mods coming :D

right on phil! the more I use snews the more I love it!
Logged

Katalonian

  • Newbie
  • *
  • Karma: 0
  • Posts: 38
    • My personal web page

I don't understand... This mod allow to show intro text for article in article_menu?
Logged
non-php guy

MartinJoph

  • Newbie
  • *
  • Karma: 0
  • Posts: 17

Hi Katalone..

(I think that's what this does. I'm gonna try anyway)

*I just implemented the code, but when I view my page, then view source there is all this weird code before the html, then the page starts sort of thing, how can I get rid of this? the code looks like this in the view souce..

//<![CDATA[
   var allowsef = /new|add|_ar|_ca/.test("new");
   var allowpreview = /new|_add|_ar|/.test("");

   // Detect browser and version
   var browser = navigator.appName;
   var get_info = navigator.appVersion;

thanks

Martin
Logged

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly

Hi Katalone..

(I think that's what this does. I'm gonna try anyway)

*I just implemented the code, but when I view my page, then view source there is all this weird code before the html, then the page starts sort of thing, how can I get rid of this? the code looks like this in the view souce..

//<![CDATA[
   var allowsef = /new|add|_ar|_ca/.test("new");
   var allowpreview = /new|_add|_ar|/.test("");

   // Detect browser and version
   var browser = navigator.appName;
   var get_info = navigator.appVersion;

thanks

Martin

Martin, that looks like the admin javascript. Were you still logged in when you viewed the source?
Logged
Of all the things I have lost, it is my mind that I miss the most.

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal

@Katalonian and Martin: if you want to know how to use the mod, it's easiest to read the original mod description. This topic is an "update" version so I didn't think it necessary to go over that again. Besides, there are many ways you can use it (as is the case with most good sNews mods).

You can for example see it in action in any article on this site, in the sidebar carousel thing.
« Last Edit: April 05, 2010, 02:55:15 AM by Fred K (agentsmith) »
Logged

MartinJoph

  • Newbie
  • *
  • Karma: 0
  • Posts: 17

Hi Philmoz,

I'm just catching up with everything,.. I think I was logged in at the time, but I noticed when I closed the browser and re-opened it again, then viewed source, all the var stuff was gone..

and thanks Fred:) I'm gonna go and check it out now.

Thanks

M
Logged

MartinJoph

  • Newbie
  • *
  • Karma: 0
  • Posts: 17

 :-\ Now I'm even more confused..., theres, mods, then theres mods of mods etc, then mods of that.. I read all through the four pages on the other post and then it sent me back here.....sigh*...

I've almost got it to do what I want, as it says it does do in the post, but it's sooo frustrating..

Basically

1) I used the orginal myposts from here http://snewscms.com/forum/index.php?topic=8003.msg56982#msg56982 that works, I made a seperate php file, then used an include in the my index. That works and pulls the latest articles but it doesn't display the date..(which I want)

Soo..

I then tried Keyrocks mod here http://snewscms.com/forum/index.php?topic=5780.msg46814#msg46814

that's exactly how I want it like that in the screenshot, with date and info line, and excerpt etc

I only have 2 categories. News and Welcome, cat 1 and 2

Using Keyrocks code.. I have this at the top of my index.php

<?php session_start(); include('snews.php');?>
<?php include('lead_articles.php');?>

and this where I want the latest news

<?php lead_articles(3); ?>

but it just says "No articles at the moment" even though there is sort of thing...

I'm using v1.7, it's soo frustrating because they both sort of work, but one pulls the posts and doesn't display the date, and the other one doesn't pull anything.. :-[ (bit like me lol)

I really want to get this sorted though, because then I can process, but my brain is hurting..I'm stuck!


Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca

Soo...
I then tried Keyrocks mod here http://snewscms.com/forum/index.php?topic=5780.msg46814#msg46814 ... that's exactly how I want it like that in the screenshot, with date and info line, and excerpt etc. I only have 2 categories. News and Welcome, cat 1 and 2. Using Keyrocks code.. I have this at the top of my index.php
        <?php session_start(); include('snews.php');?>
        <?php include('lead_articles.php');?>
and this where I want the latest news
        <?php lead_articles(3); ?>
but it just says "No articles at the moment" even though there is sort of thing... I'm using v1.7, it's soo frustrating because they both sort of work, but one pulls the posts and doesn't display the date, and the other one doesn't pull anything.. :-[ (bit like me lol) I really want to get this sorted though, because then I can process, but my brain is hurting..I'm stuck!

OK. Remember that the function you are referring to was posted for use with sNews 1.6 Only  and it is a modified version of function menu_articles from the 1.6 snews.php file. It shows the message "No articles at the moment" because the function is not getting any data from the database tables through its queries... the 1.6 queries do not work (as-is) when used in sNews 1.7.

For sNews 1.7, we need to make up a modified version of function menu_articles  from the 1.7 snews.php file.

Replace your current 1.6-compatible function with this one.

Code: [Select]
<?php


// LEAD ARTICLES - for 1.7 - last modified April.17.10 (keys)
function lead_articles($start$size) {
     global 
$_catID$categorySEF$subcatSEF;
# QUERY TO ARTICLES TABLE: fetches article data (title and text) from articles dbase table.
$query 'SELECT a.id AS artid, title, a.seftitle AS asef, date, text, c.name AS name, c.seftitle AS csef, x.name AS xname, x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c ON category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x ON c.subcat =  x.id
WHERE position = 1 AND a.published = 1 AND c.published ="YES" AND (x.published ="YES" || x.published IS NULL)
ORDER BY date DESC LIMIT '
."$start$size";
 
$result mysql_query($query);
if (!$result || !mysql_num_rows($result)) {
    echo '<ul><li>'.l('no_articles').'</li></ul>';
} else {
    while ($r mysql_fetch_array($result)) {
        $id $r['artid']; //article id
$text $r['text']; // article lead text
$date date(s('date_format'), strtotime($r['date'])); // date posted
$link = isset($r['xsef']) ? $r['xsef'].'/'.$r['csef'] : $r['csef']; // title link
# QUERY COMMENTS TABLE: fetches the number of comments associated with each article. 
$comments_query "SELECT * FROM "._PRE."comments WHERE articleid = $id AND approved = 'True'";
$comments_result mysql_query($comments_query);
$comments_num mysql_num_rows($comments_result);
# DISPLAY: title, lead text (85 characters), info-line (date published & # of comments).
echo '<p class="article"><a href="'._SITE.$link.'/'.$r['asef'].'/" title="('.$date.')"><strong>'.$r['title'].'</strong></a></p>';
echo '<div class="artlead">'.substr($text,0,85).'...&nbsp;</div>';
echo '<div class="artinfo">'.$date.'&nbsp;-&nbsp;<span class="uline">'.$comments_num.' '.l('comments').'</span></div>';
echo '<p>&nbsp;</p>';
    }
}
}


?>


And... some CSS (modified a bit from 1.6) to go with it:
Code: [Select]
<?php


/* for 1.7 version of lead_articles */
.artitle margin-top16px; }
.
artitle a text-decorationnoneline-height0; }
.
artlead font12px arialcolor#000; line-height : 1.3em; margin-bottom: 3px; }
.artinfo padding-bottom3pxborder-bottom1px dashed #c9c9c9; font: 11px arial; color: #808080; }
.uline text-decorationunderline; }


?>

« Last Edit: April 17, 2010, 08:46:58 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

MartinJoph

  • Newbie
  • *
  • Karma: 0
  • Posts: 17

YESSS! :)

That worked.. Man that's awesome.. I can see why your called keyrocks now.

1) your the key to making it work, and 2) YOU ROCK!..

thanks keyrocks, that's just purrfect!. It didn't work at first for some reason, but I hand typed instead of copy and paste from my original file and it worked. Brilliant!.

Thank You!...I've been struggeling with this for weeks....

M
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca

You're welcome. Glad to hear it worked.  8)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU
Pages: [1] 2