My previous and next posts are set on an infinite loop so there is always a previous and next post. I.E. for the newest article the "next" post would be go to the oldest post. And if we're viewing the oldest post, the "previous" post would go to the newest post.
I need to pull in the featured images of the next and previous posts to be the background for the pagination navigation.
I've got the images pulling in when there truly is a next or previous post. However, when the infinite loop is necessary, i.e. next being the oldest post, or previous being the newest post, my code breaks down.
My code is below. I believe the issue is that I'm not actually capturing the ID of what I need in $first_id and $last_id. I've tried using get_queried_object_id() $first->post->ID and various combinations to get the post ID of what is being pulled in since .previous-title is working correctly.
<div class="post_nav">
<?php if( get_adjacent_post(false, '', true) ) :
$previous_post = get_previous_post(); ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $previous_post->ID );?></div>
<p class="previous-title"><?php previous_post_link('Previous Story<br> %link','%title');?</p>
<?php else:
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
$first_id = $first->ID; ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $first_id );?></div>
<?php echo '<p class="previous-title">Previous Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
<?php if( get_adjacent_post(false, '', false) ) :
$next_post = get_next_post(); ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $next_post->ID ); ?></div>
<p class="next-title"><?php next_post_link('Next Story<br> %link','%title');?></p>
<?php else:
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
$last_id = $last->ID; ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $last_id );?></div>
<?php echo '<p class="next-title">Next Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
You're right, $first and $last are not posts, but queries. You could either use get_the_ID() or grab the ID directly.
$last_id = get_the_ID();
( since you've made it the active loop )
or
$last_id = $last->posts[0]->ID;
which means you wouldn't need to set up the loop for $last, in which case you'd also need to get the permalink and title via functions taking the ID as a parameter. This is a minor optimization, if you get confused here go with the first option.
Also, your next-title and previous-title in the else paths seem to be switched with each other. Or perhaps the entire code blocks are.
Related
I hope you can help me out. (I'm a bit of a newbie.)
I have used functions.php to create two extra product fields and then show the input from these fields as well as a contact link/button below the short description on the product page.
Lets call this added content "mycontent" just to make it easier to explain the problem.
The weird thing is that now, if I go to a category page in admin and add anything in the "Description" field - then "mycontent" shows up above the products on the category page...?!
I have no idea how this is even possible?
But I would very much like it to stop...! ;o)
I have tried adding "mycontent" to my product page via other hooks from this page: https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/, but it doesn't show up. The only way it shows up is if I use woocommerce_short_description. But when I do that, it also shows up on category pages (only if there is a description on the category page).
I am confused on a higher level than normal... What am I doing wrong?
This is the code I use in functions.php to display "mycontent":
/* Show PDF link, PDF name and Contact Us on product pages */
add_filter( 'woocommerce_short_description', 'showpdflink', 3 );
function showpdflink($description){
global $product;
$link1='';
// Get the custom field value
$mypdfcol = get_post_meta( $product->get_id(), 'cop_pdflink', true );
$mypdfnam = get_post_meta( $product->get_id(), 'cop_pdfname', true );
// Display
//Build the links for the pdeffers
if( ! empty($mypdfcol) ){
$link1='<hr />
<div class="pdflink">
<div class="et_pb_row et_pb_row_13 et_pb_equal_columns">
<div class="et_pb_column et_pb_column_1_4"><img src="/wp-content/uploads/2022/12/pdf-icon.png" /></div>
<div class="et_pb_column et_pb_column_3_4">
<a href="'.$mypdfcol.'" title="Download pdf';
if( ! empty($mypdfnam) ){
$link1=$link1.' for '.$mypdfnam.'';
}
$link1=$link1.'" target="_blank" rel="noopener">Download pdf';
if( ! empty($mypdfnam) ){
$link1=$link1.' for '.$mypdfnam.'';
}
if( ! empty($mypdfcol) ){
$link1=$link1.'</a></div>
</div>
</div>';
}
}
/* Show CONTACT US button */
$link1=$link1.' <div class="contactbutton">
<a href="/kontakt-os" title="Kontakt CopyColor">
<span class="large">Ring for pris</span><br />
<span class="small">Klik her for kontakt</span>
</a>
</div>';
/* Show everything on the product page */
return $description.$link1;
}
/* End of Show PDF link */
I found the solution if anyone else can use it:
It turns out that woocommerce_short_description is used in several different places and then outputted in different hooks - so it showed up in both the product and the category description.
The correct place for me was woocommerce_single_product_summary.
My problem was that it wouldn't output the code. But by changing
return $description.$link1;
to
echo $description.$link1;
I got it to work.
How to i get News previous and next news in contao ?
f.e
< Previous News | Current news ( id - 4) | Next News >
I want Previous and next news link in detail page..
You can use the following extension: [newspagination]
In case you are not using the default mod_newsreader template, you need to add the following line:
<?php echo $this->addNewsPagination() ?>
or just
<?= $this->addNewsPagination() ?>
with PHP 5.4+
I have a text like this:
<p><strong>Lorem</strong> ipsur </p>
To see a text without html tags in Zend Framework 2 how can I do?
I tried to put:
<?php echo $this->escapehtml($array['text']); ?>
I read that for security is not good to do:
<?php echo $array['descrizione']; ?>
Im not sure if i understand the question, since i dont know the contents of your arrays.
basicaly if you want to scape the contents of a variable, let's say, $input, you have to call, as you mentioned
$this>escapeHtml($input)
Actually, the PhpRenderer includes a selection of helpers you can use for this purpose: EscapeHtml, EscapeHtmlAttr, EscapeJs, EscapeCss, and EscapeUrl.
You can read about this here
Also, if you want more control, you can use Zend\Escaper, that in 2 lines of code allow you to escape html, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtml($input);
or escape attributes, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtmlAttr($input);
I recommend you read the 3 links, they are very short and would give you a better undestanding of what you are doing.
At this moment there is no option in escapeHtml to allow certain tags. What you have to do in these specific cases is to modify the returned value:
<?php
$str = $this->escapehtml($array['text']); ?>
$str = preg_replace(
array('#href="(.*)"#', '#<(/?(?:pre|a|b|br|em|u|ul|li|ol|p|strong)(\shref=".*")?/?)>#' ),
array( 'href="\1"', '<\1>' ),
$str
);
echo $str;
You can add/remove tags from the part pre|a|b|br|em|u|ul|li|ol|p|strong to your needs. Also this code will allow anchor tags with href only.
I'm using Zend Framework 2.
I'd like test if a link is the active page in my view script.
For example :
on my view script : view/application/account/user.phtml
<a href="$this->url("account")"
<?echo **if($this->isActive()**){?> class="active"<?php } ?>link</a>
I don't want to set menu with Zend\Mvc\Navigation
Thank you all.
You could do like this:
<?php $current_url = $this->url(); ?>
<a href="<?php echo $this->url('account'); ?>" <?php if ($current_url == $this->url('account')) { echo 'class="active"'; } ?>>link</a>
I have set up a Custom Post Type called 'RELEASES' - think music cd release.
This post type has fields named 'release_artist', 'release_title', 'release_date', 'release_artwork' and 'release_tracklisting' for entering all of the relevant music cd information.
I am having real trouble actually displaying this information in my Wordpress template.
I have really only had luck outputting a list of the titles and none of the other data.
Any idea what I put in the LOOP to display all of the information? Preferably each in its own LIST item so I can style each separately?
Any thoughts greatly appreciated.
You can use get_post_meta to pull in your fields as needed. Inside your loop, you can start with the following:
<?php
$release_artist = get_post_meta($post->ID, 'release_artist', true);
$release_title = get_post_meta($post->ID, 'release_title', true);
?>
<ul>
<li class="release_artist">
<?php echo $release_artist; ?>
</li>
<li class="release_title">
<?php echo $release_title; ?>
</li>
</ul>
Are those custom fields? If yes, try what codex.wordpress.org is saying. Better yet, try ACF plugin.
-- edit
If you want to display parts of your pages on other ones (eg. on your home), you need to use query_posts. This is fairly simple function. For your loop, try something like this:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'releases'
));
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'Opis nazwy'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
$key is a single value, here set to release_artists. It's purely for testing. If it works - feel free to define your own variables.
You should use:
<?php the_field('field_name') ?>
inside your loop. Hope it helps!
From most of the documentation I've seen online, query_posts shouldn't be the go-to function for creating custom queries and loops. The following code snippet might be a good starting point. You should be able to use this inside or outside of the main loop of your themes template files.
$args = array(
'post_type' => 'release', //remember this is-case sensitive
'posts_per_page' => -1,
);
$releaseQuery = new WP_Query( $args );
if ( $releaseQuery->have_posts() ) :
while ( $releaseQuery->have_posts() ) :
$releaseQuery->the_post();
// Fetching the post ID for demonstration and for use later
$c_id = get_the_ID();
// After running the_post(), alot of the Wordpress functions (not all) can now be used without supplying the post ID.
echo get_the_title();
// You could also have used get_the_title($c_id);
// Then:
echo get_post_meta($c_id, 'release_title', true);
echo get_post_meta($c_id, 'release_artist', true);
endwhile;
endif;
// Return to the current page's main query
wp_reset_query();
// This should now display the page's title
the_title();
About your ID's question:
Every item, like posts and pages in WordPress have an "ID", but they are not normally shown in the lists of them. There are a number of plugins that will add an "ID" column in your admin. Search Wordpress.org for "show ids" and pick one you like. Install it. Activate it. You'll see the ids.
https://wordpress.org/plugins/search.php?q=show+id