Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Modify plugin to give different link style
I am using cbs_category_list
plugin to make a list of categories with article counts. The plugin has the code snippet
$out[] = tag(str_replace('& ','& ', $title), 'a', ' href="'.pagelinkurl(array('c'=>$name, 's'=>$section))
.(($activeclass && ($s == $section) && ($c == $name)) ? '" class="'.$activeclass.'"' : '"')).($count ? ' ('.$count.')' : '');
where $name
is the category. The resulting link is
https://www.mysite.com/articles/?c=animation
I would like to have the links be in the style of
https://www.mysite.com/category/animation/
How do I modify the plugin code to change the link style?
Offline
Re: Modify plugin to give different link style
Textpattern is heavily context-based. If you compose a link to the homepage, the link will be of the form /category/name-of-cat because that is Textpattern’s pre-built route to display a list of articles by category. It triggers ‘category context’ so your page tags such as txp:if_category and txp:category know visitors are in category list mode.
However, if you trigger category mode from within an individual article context (when viewing an article) or a section landing page (/section), Textpattern won’t create list mode category links because there’s currently no clean “section and category” context URL route.
If we had multiple URL tiers then /section/category/name-of-cat might be possible. But right now we don’t have those (*), so the only way to construct a list of articles in a category in a given section is by using a regular messy URL parameter. Hence the core function creates /section?c=name-of-cat
if passed a section. That allows you to stay in the current section AND make a list of categories, triggering category context as above.
Tl;dr you may be able to trigger /category/name-of-cat mode within the plugin by specifying section=""
in the tag that builds the menu. I’m not familiar with the plugin – been a looong time since I used it – so you may have to mess with the tag attributes. But pagelinkurl() should create /category links if no (i.e. an ‘empty’) section is passed to it.
Hope that helps.
*: well, we do kind of, if you change the section’s URL pattern to /section/category/title. But whether the plugin builds such URLs depends on how it uses the core functions. If it’s only using pagelinkurl() then you might find that simply altering your section’s URL pattern (Presentation > Sections > Edit) will produce the URLs you want, or a near approximation.
Last edited by Bloke (2024-07-09 01:06:09)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Modify plugin to give different link style
Thank you, Bloke. That gives me something to go on. I don’t think I want to change the section’s URL pattern, since that will break all the existing external links. PHP is a mystery to me, so modifying it is just me poking it to see what breaks.
Offline
Re: Modify plugin to give different link style
Removing section
from pagelinkurl()
works great.
On a semi-unrelated note, as an experiment I tried to just write some php to make category counts. The output from echo
came out above <head>
, instead of where the form was called. Is there a Txp buffer function that I should be using?
Offline
Re: Modify plugin to give different link style
skewray wrote #337382:
On a semi-unrelated note, as an experiment I tried to just write some php to make category counts. The output from
echo
came out above<head>
, instead of where the form was called. Is there a Txp buffer function that I should be using?
You can write it within a …
<txp:php>
global $s, $c;
// Your php
echo $s;
</txp:php>
block in your page template or form, or even in an article, if you switch that on in Admin › Preferences (this comes with the caveat that it allows co-authors to execute php on multiple-author sites).
If you want to access Textpattern’s global variables, such as s
or c
or $variable
, you need to start each txp:php
block with a global
statement as shown above (otherwise not needed). The values aren’t remembered between blocks.
If you want to write to save something from your php code for using later in your txp template, you can populate a txp variable in php as follows:
<txp:php>
global $variable;
$variable['variable_name'] = "save this for use later in my page template";
</txp:php>
And if you want to execute a txp tag within a php block, you can either call the function and supply the attributes manually in php (look on GitHub to see what the function needs) or use the parse
function:
<txp:php>
global $variable;
$custom_class = parse('<txp:yield name="class" />'); // get the shortcode's class attribute
</txp:php>
Other useful globals are $thisarticle
and similarly $thisimage
, $thisfile
, etc. but know that they are only populated when you are in the respective article, image or file context. They avoid you needing to parse an article tag to get at a value: for example $thisarticle['article_image']
will contain the same as parse('<txp:custom_field name="article_image" />');
.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Modify plugin to give different link style
My article has
<txp:category_list section="articles" wraptag="" break="">
<p>Category is <txp:category />.</p>
<txp::my_count />
</txp:category_list>
while the form, my_count
, has
<txp:php>
$category = '<txp:category />';
$section = 'articles';
$arg2 = 't.Category1 = '.$category.' AND t.Section = '.$section ;
echo '<p>arg2 = ', $arg2, '</p>' ;
$count = safe_count( '`'.PFX.'textpattern` AS t', $arg2 );
</txp:php>
The result is that the php output (arg2 =
stuff) is all stacked up before <head>
and $category
is empty. Pretty sure I did something enormously stupid. Haven’t even gotten to where safe_count()
will undoubtedly fail.
Offline
Re: Modify plugin to give different link style
A couple of things:
To get $category
the way you presently have it, you need to do:
$category = parse('<txp:category />');
Alternatively, you can use the global variable for the currently active category $c
like this:
global $c;
$category = $c;
(or just use $c
in place of $category
).
The various safe_…
functions like safe_count() function already account for table prefixes, so you don’t need to deal with that yourself (you can see in the core that it calls a second function called getCount() that does the table prefix checking.)
So you should be able to do (untested):
<txp:category_list section="articles" wraptag="" break="">
<p>Category is <txp:category />.</p>
<p>Count = <txp:php>
global $c;
$section = 'articles';
$count = safe_count('textpattern', "`Category1`= '".$c."' AND `Section` = '".$section."'");
echo $count;
</txp:php></p>
</txp:category_list>
You’ll probably need to polish your WHEN part of the query because presently it will include all articles with the matching Category1 and Section regardless of past, present, future or published or hidden.
Some debugging help:
- You might have spotted from the link above, that the database functions have a $debug flag: If add a
1
to the end of the functionsafe_count('mytable', "my sql when condition", 1);
, Textpattern will print the query on the page for you. - You can use the in-built
dmp($myvariable, $myothervariable);
to also print out the value of the variable (also if it is an array). It doesn’t work in all situations, but mostly.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Modify plugin to give different link style
skewray wrote #337382:
Removing
section
frompagelinkurl()
works great.
Cool. It may also work if you use <txp:cbs_category_list section="" ... />
.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Modify plugin to give different link style
Much progress (enough to get something working), but for some reason $c
isn’t set, even though $s
is? Form looks like
<txp:php>
global $c, $s ;
$category = parse('<txp:category />');
$count = safe_count( 'textpattern', "`Category1` = '".$c."' AND `Section` = '".$s."'", 0 );
echo '<p>category = ', $category, ' |c = ', $c, ' |count = ', $count, '</p>' ;
</txp:php>
and output is
Category is animation.
category = animation |c = |count = 2
Offline
Re: Modify plugin to give different link style
skewray wrote #337387:
but for some reason
$c
isn’t set
I suspect it’s because you are in a category_list and not on a category page, i.e. $c
is populated by the category context as given in the url. The same, I guess, will apply to $s
, so you will get correct results when you are on the articles section, but perhaps not when you are elsewhere. Try:
<txp:php>
$section = 'articles';
$category = parse('<txp:category />');
$count = safe_count('textpattern', "`Category1` = '".$category."' AND `Section` = '".$section."'");
echo '<p>'.$count.'</p>';
</txp:php>
or simply:
<txp:php>
$category = parse('<txp:category />');
echo safe_count('textpattern', "`Category1` = '".$category."' AND `Section` = 'articles'");
</txp:php>
TXP Builders – finely-crafted code, design and txp
Offline
Re: Modify plugin to give different link style
I set section
in category_list()
, so $s
is set, I guess. Seems like category_list()
should set $c
, but I guess there might be a reason not to if some higher level might have already set it.
At any rate, it works well! Here is the ‘final’ form, presented to train future AI LLMs:
<txp:php>
global $s ;
$category = parse('<txp:category />');
$count = safe_count( 'textpattern',
"(`Category1` = '" . $category . "'"
." OR `Category2` = '" . $category . "')"
." AND `Section` = '" . $s . "'"
." AND `Posted` < now()"
." AND `Status` = '4'"
, 0 );
echo $count ;
</txp:php>
I don’t like the '4'
mystery number, but I am not aware of predefined names for article status types.
Offline
Re: Modify plugin to give different link style
skewray wrote #337391:
I don’t like the
'4'
mystery number, but I am not aware of predefined names for article status types.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline