I need to get separated the image and text of the article, the problem is that they are in one field of the database, so how can i make that in the view i can put the image separated from the text? because i can't put the image and the text togheter, is a mess
<? foreach ($list as $item) : ?>
<?
$year = date('Y', strtotime($item->created));
$month = date('m', strtotime($item->created));
$day = date('d', strtotime($item->created));
$shortDescription = cutString($item->introtext, 100);
?>
<div class="footerItem">
<?= $day; ?>-<?= $month; ?>-<?= $year; ?>
<p><?= $item->title; ?></p>
<p>
<?= $shortDescription; ?>
</p>
</div>
<?php endforeach; ?>
so i need to put the image like this $item->image - this is what i want, but in joomla latestnews_mod, there is no field with image, and i don't know if i need to write my own, or edit this module, or i need to use other module for this ? And also i cannot get the field $item->category that i need, there is only the CAT_ID, and how can i get the category field??
I've lost touch with Joomla a bit, so it's entirely possible this is not the easiest way, but could you use regex to pull out the image from the intro-text if it's not specifically given?
You'd need a pattern something like:
<img\b[^>]+?src\s*=\s*['"]?([^\s'"?#>]+)
That's taken from this question: Regex to find the first image in an image tag in an HTML document
EDIT: Example using preg_match:
<?php
// This is the Regex pattern from above.
$pattern = '/<img\b[^>]+?src\s*=\s*[\'"]?([^\s\'"?#>]+)/';
// Perform the search, matches will be stored in $matches
preg_match($pattern, $item->introtext, $matches );
echo "Image src is: " . $matches[1] . "\n";
?>
Do note that this is relatively untested, may not work and if it does, may miss cases with bad markup!
Related
currently I use tags in my wordpress article titles and place them at the beginning.
https://alerte-prolo.fr/les-alertes-alimentaires/
https://alerte-prolo.fr/alimentaire/acheter-ses-fruits-et-legumes-moins-chers/
I want to keep the tags in the category pages, but in the articles (single.php) I want to delete the tags.
currently I use this to lift the first word of the title.
<h2>
$originalTitle = the_title('','',false);
echo substr($originalTitle,strpos($originalTitle, ' '));
</h2>
t works with [Astuce], [gratuit] ect
but when I have tag [bon plan] it would be necessary to be able to delete the first two words.
I need help for this.
thanks
I think this working for you:
<?php
$originalTitle = "[bon plan] Some Title"; // In your case: $originalTitle = the_title('','',false);
$newTitle = explode( "] " , $originalTitle, 2);
echo $newTitle[1];
?>
If I understood You correctly:
$originalTitle = "[wtf] [this one to] Some Title";
$newOriginalTitle = preg_replace("/\[[^)]+\]/","",$originalTitle);
echo $newOriginalTitle;
/\[ - defining start
\] - defining end
I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.
I'm trying to replace a string within a Wordpress title. I need replace two specific keywords in the title. I don't wish to change the title for the whole page only for this specific instance. I can't seem to get this code to work and I'm not sure how to add addition strings to replace. Thank you for the help - I'm a PHP novice.
<?php
$wptitle = the_title();
$wptitle = str_replace('Download', '', $wptitle);?>
<?php echo $wptitle; ?>
This works:
<?php
$wptitle = get_the_title();
$wptitle = str_replace(array('REPLACESTRING1', 'REPLACESTRING2'), '', $wptitle);?>
<?php echo $wptitle; ?>
I'm needing to replace white spacing with a plus sign "+" for the code displayed below.
I'm in the process of modifying some code which generates the label and url for products displayed in my catalog. The problem I face is that my current code doesn't do the replacement. Can someone please modify the code, replacing a spacing for a plus sign "+".
<h5><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h5>
and will return a url something like this:
http://www.efficienttrade.co.nz/catalogsearch/result/?order=relevance&dir=desc&q=potassium nitrate
However, when getName() function is used, names which have a space don't work for the generated search query. So I need to replace the space with a "+" to make the search query url work.
Thanks
As far as i understand your problem, you need to replace spaces by hypens in your product name. This can be achieved by replacing the following code in your href
...<?php echo $this->stripTags($_product->getName(), null, true); ?>...
with
...<?php echo str_replace(' ', '-', $this->stripTags($_product->getName(), null, true)); ?>...
How about the following to make you code slightly nicer (although PHP/HTML soup is never a lot of fun). The first line of PHP is the one that replaces spaces with hyphen
<?php
/*Get product name, stripped of HTML and spaces*/
$productName = str_replace(' ', '-', strip_tags($_product->getName(), null, true));
/*Assign variables rather than using same function multiple times.*/
$productAttribute = $_helper->productAttribute($_product, $_product->getName(), 'name');
/*Concatenate the URL here for easier code fixing later.*/
$url = 'http://www.efficienttrade.co.nz/catalogsearch/result/order=relevance&dir=desc&q=' . $productName;
?>
<h5>
<?php echo $productAttribute ?>
</h5>
I'm trying to add a 'back' link within page.tpl.php (replacing a proper breadcrumb for a certain content type...).
My goal is to create a link that pulls in the current URL, but removes the final argument. So a page mysite/x/y would have a link mysite/x/ (or mysite/x).
Is this possible? (The URLs are aliased).
<?php
$path = ???
$my_link = l('Back', $path);
?>
<?php if (($node->type == 'marketplace_item')): ?>
<div id="breadcrumb" class="nav"><?php print $my_link; ?></div>
<?php endif; ?>
Cheers,
James
if this the case always you can build the URL manually
$my_link = $base_url . arg(0);
or count the args then remove the last one